From 14ddb2fb5c8d3ae1969bb569e364a50da41a2749 Mon Sep 17 00:00:00 2001 From: Skywt2003 Date: Fri, 12 Nov 2021 16:56:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E7=9B=AE=E5=BD=95=20TOC=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/style.css | 4 ++++ functions.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/assets/style.css b/assets/style.css index d7463e7..196e6b5 100644 --- a/assets/style.css +++ b/assets/style.css @@ -71,4 +71,8 @@ code { .friend { height: 80px; +} + +html{ + scroll-behavior: smooth; } \ No newline at end of file diff --git a/functions.php b/functions.php index 5e58aa8..8729b67 100644 --- a/functions.php +++ b/functions.php @@ -136,6 +136,32 @@ function exContent($content){ // $2'; // $content = preg_replace($pattern, $replacement, $content); + // 文章 TOC 功能 + // [0]: 完整的匹配

conTent

+ // [1]: 匹配中的数字 2 + // [2]: 匹配中的内容 conTent + if (preg_match_all('/(.*)<\/h\d>/isU', $content, $outarr)){ + $toc_out = ""; + $minlevel = 6; + for ($key=0; $key{$outarr[2][$key]}". substr($ta, strlen($outarr[0][$key])+$tb); + $content = substr($ta, 0, $tb). "". substr($ta, $tb); + // 用伪锚点实现链接偏移。Safari 居然不支持!! + if ($level > $curlevel) $toc_out.=str_repeat("
    \n", $level-$curlevel); + elseif ($level < $curlevel) $toc_out.=str_repeat("
\n", $curlevel-$level); + $curlevel = $level; + $toc_out .= "
  • {$outarr[2][$key]}
  • \n"; + } + + $content = "
    {$toc_out}
    ". $content; + } + return $content; }