From 922c6749b7f74bf3bb2a916b28b759d2bc4a5b5e Mon Sep 17 00:00:00 2001 From: haimait Date: Fri, 26 Jun 2026 17:18:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmarkdown=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=99=A8[TOC]=E6=A0=87=E7=AD=BE=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E6=A0=87=E9=A2=98=E6=A0=91=E4=B8=8D=E5=85=BC=E5=AE=B9=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/editor.md/editormd.js | 10 ++++---- static/js/kancloud.js | 49 +++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/static/editor.md/editormd.js b/static/editor.md/editormd.js index 3f084cee..cf9098e2 100755 --- a/static/editor.md/editormd.js +++ b/static/editor.md/editormd.js @@ -4054,6 +4054,9 @@ text = trim(text); var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-"); + var isChinese = /^[\u4e00-\u9fa5]+$/.test(text); + var id = (isChinese) ? encodeURIComponent(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-"); + var toc = { text : text, level : level, @@ -4061,14 +4064,11 @@ id : id }; - var isChinese = /^[\u4e00-\u9fa5]+$/.test(text); - var id = (isChinese) ? encodeURIComponent(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-"); - // var id = Math.floor(Math.random() * 1000000000 ).toString(36); - markdownToC.push(toc); - var headingHTML = ""; + var headingHTML = ""; + headingHTML += ""; headingHTML += ""; headingHTML += (hasLinkReg) ? this.atLink(this.emoji(linkText)) : this.atLink(this.emoji(text)); headingHTML += ""; diff --git a/static/js/kancloud.js b/static/js/kancloud.js index 981ebaf8..7f077f22 100644 --- a/static/js/kancloud.js +++ b/static/js/kancloud.js @@ -333,8 +333,55 @@ $(function () { } catch (e) { console.log(e); } - }).on("click", ".markdown-toc-list a", function () { + }).on("click", ".markdown-toc-list a", function (e) { + e.preventDefault(); var $this = $(this); + var href = $this.attr("href"); + if (href) { + var name = href.replace(/^#/, ""); + var target = null; + // 1. 按 name 属性查找 + var found = $("a[name='" + name + "']"); + if (found.length > 0) { + target = found; + } + // 2. 按 id 查找 + if (!target) { + var el = document.getElementById(name); + if (el) target = $(el); + } + // 3. 尝试带 h{n}- 前缀的id格式 + if (!target) { + $("h1,h2,h3,h4,h5,h6").each(function() { + if (this.id && this.id.replace(/^h\d-/, "") === name) { + target = $(this); + return false; + } + }); + } + // 4. 按标题文本匹配(兼容旧文档无锚点的情况) + if (!target) { + var linkText = $this.text().trim(); + $(".markdown-heading, h1, h2, h3, h4, h5, h6", "#page-content").each(function() { + var headingText = $(this).clone().children("a, span").remove().end().text().trim(); + if (!headingText) headingText = $(this).text().trim(); + if (headingText === linkText) { + target = $(this); + return false; + } + }); + } + if (target && target.length > 0) { + var container = $(".manual-right"); + var scrollTo = target.offset().top - container.offset().top + container.scrollTop(); + container.animate({ scrollTop: scrollTo }, 200); + if (history.replaceState) { + history.replaceState(null, null, href); + } else { + location.hash = href; + } + } + } setTimeout(function () { $(".markdown-toc-list li").removeClass("directory-item-active"); $this.parents("li").addClass("directory-item-active");