Skip to content

Commit 237af61

Browse files
authored
修复markdown编辑器[TOC]标签生成的标题树不兼容问题。 (#1041)
1 parent 6ef419a commit 237af61

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

static/editor.md/editormd.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,21 +4054,21 @@
40544054
text = trim(text);
40554055

40564056
var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-");
4057+
var isChinese = /^[\u4e00-\u9fa5]+$/.test(text);
4058+
var id = (isChinese) ? encodeURIComponent(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-");
4059+
40574060
var toc = {
40584061
text : text,
40594062
level : level,
40604063
slug : escapedText,
40614064
id : id
40624065
};
40634066

4064-
var isChinese = /^[\u4e00-\u9fa5]+$/.test(text);
4065-
var id = (isChinese) ? encodeURIComponent(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-");
4066-
// var id = Math.floor(Math.random() * 1000000000 ).toString(36);
4067-
40684067
markdownToC.push(toc);
40694068

4070-
var headingHTML = "<h" + level + " id=\"h"+ level + "-" + this.options.headerPrefix + id +"\">";
4069+
var headingHTML = "<h" + level + " id=\"h"+ level + "-" + this.options.headerPrefix + id +"\" class=\"markdown-heading\">";
40714070

4071+
headingHTML += "<a name=\"" + id + "\" class=\"reference-link\"></a>";
40724072
headingHTML += "<span class=\"header-link octicon octicon-link\"></span>";
40734073
headingHTML += (hasLinkReg) ? this.atLink(this.emoji(linkText)) : this.atLink(this.emoji(text));
40744074
headingHTML += "</h" + level + ">";

static/js/kancloud.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,55 @@ $(function () {
333333
} catch (e) {
334334
console.log(e);
335335
}
336-
}).on("click", ".markdown-toc-list a", function () {
336+
}).on("click", ".markdown-toc-list a", function (e) {
337+
e.preventDefault();
337338
var $this = $(this);
339+
var href = $this.attr("href");
340+
if (href) {
341+
var name = href.replace(/^#/, "");
342+
var target = null;
343+
// 1. 按 name 属性查找
344+
var found = $("a[name='" + name + "']");
345+
if (found.length > 0) {
346+
target = found;
347+
}
348+
// 2. 按 id 查找
349+
if (!target) {
350+
var el = document.getElementById(name);
351+
if (el) target = $(el);
352+
}
353+
// 3. 尝试带 h{n}- 前缀的id格式
354+
if (!target) {
355+
$("h1,h2,h3,h4,h5,h6").each(function() {
356+
if (this.id && this.id.replace(/^h\d-/, "") === name) {
357+
target = $(this);
358+
return false;
359+
}
360+
});
361+
}
362+
// 4. 按标题文本匹配(兼容旧文档无锚点的情况)
363+
if (!target) {
364+
var linkText = $this.text().trim();
365+
$(".markdown-heading, h1, h2, h3, h4, h5, h6", "#page-content").each(function() {
366+
var headingText = $(this).clone().children("a, span").remove().end().text().trim();
367+
if (!headingText) headingText = $(this).text().trim();
368+
if (headingText === linkText) {
369+
target = $(this);
370+
return false;
371+
}
372+
});
373+
}
374+
if (target && target.length > 0) {
375+
var container = $(".manual-right");
376+
var scrollTo = target.offset().top - container.offset().top + container.scrollTop();
377+
container.animate({ scrollTop: scrollTo }, 200);
378+
if (history.replaceState) {
379+
history.replaceState(null, null, href);
380+
} else {
381+
location.hash = href;
382+
}
383+
}
384+
}
338385
setTimeout(function () {
339386
$(".markdown-toc-list li").removeClass("directory-item-active");
340387
$this.parents("li").addClass("directory-item-active");

0 commit comments

Comments
 (0)