diff options
| author | BigCoke233 <hi@guhub.cn> | 2020-07-16 23:38:54 +0800 |
|---|---|---|
| committer | BigCoke233 <hi@guhub.cn> | 2020-07-16 23:38:54 +0800 |
| commit | 86168e77a6e2aa3de006952bcdb646819f41fbf1 (patch) | |
| tree | 1657b555b4c057bd3aa7eeb4e44c9b3864e8b2d6 /libs/Contents.php | |
| parent | 9c757ad0efc8d0dfa4026940381f01f7fe75200c (diff) | |
:zap: Add: 初步实现文章目录功能
Diffstat (limited to 'libs/Contents.php')
| -rw-r--r-- | libs/Contents.php | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/libs/Contents.php b/libs/Contents.php index da7d321..567c3b2 100644 --- a/libs/Contents.php +++ b/libs/Contents.php @@ -30,7 +30,9 @@ class Contents //owo $text = Contents::parseEmo($text); //Links - $text = Contents::parseLink($text); + $text = Contents::parseLink($text); + //toc + $text = preg_replace('/{:toc:}/s',Contents::tableOfContents($text),$text); } return $text; } @@ -230,6 +232,8 @@ class Contents return $text; } + /* == 文章目录 == */ + /** * 解析章节链接 */ @@ -250,6 +254,24 @@ class Contents return '<h'.$matchs[1].$matchs[2].' id="'.$id.'">'.$matchs[3].'<a href="#'.$id.'" title="'.$GLOBALS['postTexts']['post_anchor'].'" class="post-toc-link no-line"><i class="iconfont icon-paragraph"></i></a></h'.$matchs[1].'>'; } + /** + * 文章目录 + */ + public static function tableOfContents($html) { + $pattern = '/<h([2-5]) id=["\'](.*?)["\'].*?>(.*?)<\/h\1>/'; + preg_match_all($pattern, $html, $matches, PREG_SET_ORDER); + $output = ""; + foreach ($matches as $item) { + $output .= ' + <li class="toc-level-' . $item[1] . '"> + <a href="#' . $item[2] . '">' . $item[3] . '</a> + </li>'; + } + return (!empty($output)) ? + "<ul class='toc'>" . $output . "</ul>" : + ""; + } + /* == 其他内容 == */ /** |
