-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.php
49 lines (49 loc) · 2.07 KB
/
toc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
function generate_toc($content) {
$toc = '<aside class="sidebar right-sidebar sticky">
<section class="widget archives">
<div class="widget-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z"/>
<line x1="5" y1="9" x2="19" y2="9" />
<line x1="5" y1="15" x2="19" y2="15" />
<line x1="11" y1="4" x2="7" y2="20" />
<line x1="17" y1="4" x2="13" y2="20" />
</svg>
</div>
<h2 class="widget-title section-title">文章目录</h2>
<div class="widget--toc"><nav id="TableOfContents">';
// 初始化变量
$has_toc = false;
// 使用正则表达式提取标题
if (preg_match_all('/<h([1-6])>(.*?)<\/h\1>/', $content, $matches, PREG_SET_ORDER)) {
$current_level = 0;
$last_level = 0;
foreach ($matches as $match) {
$level = intval($match[1]);
$title = strip_tags($match[2]);
$id = $title; // 保留标题原有格式
if ($level > $current_level) {
$toc.= '<ol>';
} elseif ($level < $current_level) {
$toc.= str_repeat('</ol>', $current_level - $level);
}
$toc.= '<li><a href="#'. htmlspecialchars($id). '">'. htmlspecialchars($title). '</a>';
if ($level <= $current_level) {
if ($current_level > $last_level) {
$toc.= '</li>';
}
}
$last_level = $current_level;
$current_level = $level;
$has_toc = true;
}
// 关闭所有打开的列表标签
if ($current_level > 0) {
$toc.= str_repeat('</ol>', $current_level );
}
}
$toc.= '</nav></div></section></aside>';
return $has_toc? $toc : '';
}
?>