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 | |
| parent | 9c757ad0efc8d0dfa4026940381f01f7fe75200c (diff) | |
:zap: Add: 初步实现文章目录功能
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | functions.php | 1 | ||||
| -rw-r--r-- | libs/Contents.php | 24 | ||||
| -rw-r--r-- | libs/TableContents.php | 0 | ||||
| -rw-r--r-- | themeConfig.php | 9 |
5 files changed, 33 insertions, 2 deletions
@@ -211,6 +211,7 @@ $ git clone https://github.com/BigCoke233/miracles.git - [details-element-polyfill](https://github.com/javan/details-element-polyfill) - 优化 details 标签兼容性 - [hint.css](https://kushagra.dev/lab/hint/) - 工具提示 Tooltip - [alertify](https://github.com/MohammadYounes/AlertifyJS) - 对话框和 snackbar(notifier) +- [TableContents](https://github.com/lutdev/table-contents) - 生成文章目录 - ~~[LazyLoad](https://github.com/tuupola/lazyload) - 图片懒加载~~(开发版已用新的方案替换) - ~~[FancyBox](https://github.com/fancyapps/fancybox) - 文章图片灯箱~~(开发版已用新的方案替换) diff --git a/functions.php b/functions.php index e8f5271..3a61c87 100644 --- a/functions.php +++ b/functions.php @@ -9,6 +9,7 @@ require_once("libs/Contents.php"); require_once("libs/Comments.php"); require_once("libs/Options.php"); require_once("libs/Language.php"); +require_once('libs/TableContents.php'); //引入语言文件 require_once("libs/lang/".$GLOBALS['miraclesLang'].".php"); 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>" : + ""; + } + /* == 其他内容 == */ /** diff --git a/libs/TableContents.php b/libs/TableContents.php new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/libs/TableContents.php diff --git a/themeConfig.php b/themeConfig.php index 21f82bf..02f6287 100644 --- a/themeConfig.php +++ b/themeConfig.php @@ -46,4 +46,11 @@ $GLOBALS['miraclesTimThumbAllowList'] = array ( //是否压缩 HTML 源代码 //压缩能优化页面传输速度,但可能会给服务器增加一定负担(不过也还好 -$GLOBALS['miraclesIfCompressHTML'] = 'on';
\ No newline at end of file +$GLOBALS['miraclesIfCompressHTML'] = 'on'; + +/** ========= NOTICE ========== * + * 每次更新前都请备份您的 themeConfig.php 文件 + * 新 release 发布时会提示此文件是否有变动 + * 若无,则无需更新 themeConfig.php,覆盖其他文件即可; + * 若有,则会在更新内容之前用注释分割, + * 请复制源文件的所有内容并覆盖新文件注释之前的内容 */
\ No newline at end of file |
