mirror of
https://github.com/vale981/DiscoTOC
synced 2025-03-04 17:11:42 -05:00
Sane id generation
This commit is contained in:
parent
a7a444eec2
commit
3d26988257
1 changed files with 34 additions and 7 deletions
|
@ -19,21 +19,17 @@
|
||||||
const SMOOTH_SCROLL_SPEED = 300;
|
const SMOOTH_SCROLL_SPEED = 300;
|
||||||
const TOC_ANIMATION_SPEED = 300;
|
const TOC_ANIMATION_SPEED = 300;
|
||||||
|
|
||||||
const cleanUp = item => {
|
const cleanUp = item => item
|
||||||
const cleanItem = item
|
|
||||||
.trim()
|
.trim()
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.replace(
|
.replace(
|
||||||
/[\{\}\[\]\\\/\<\>\(\)\|\+\?\*\^\'\`\'\"\.\_\$\d\s~!@#%&,;:=]/gi,
|
/[\{\}\[\]\\\/\<\>\(\)\|\+\?\*\^\'\`\'\"\.\_\$\s~!@#%&,;:=]/gi,
|
||||||
"-"
|
"-"
|
||||||
)
|
)
|
||||||
.replace(/\-\-+/g, "-")
|
.replace(/\-\-+/g, "-")
|
||||||
.replace(/^\-/, "")
|
.replace(/^\-/, "")
|
||||||
.replace(/\-$/, "");
|
.replace(/\-$/, "");
|
||||||
|
|
||||||
return cleanItem;
|
|
||||||
};
|
|
||||||
|
|
||||||
const createAnchors = id => {
|
const createAnchors = id => {
|
||||||
const link = $("<a/>", {
|
const link = $("<a/>", {
|
||||||
href: `#${id}`,
|
href: `#${id}`,
|
||||||
|
@ -262,9 +258,11 @@
|
||||||
if ($elem.hasClass("d-editor-preview")) return;
|
if ($elem.hasClass("d-editor-preview")) return;
|
||||||
if (!$elem.parents("article#post_1").length) return;
|
if (!$elem.parents("article#post_1").length) return;
|
||||||
|
|
||||||
const dToc = $elem.parents('.wiki');
|
let dToc = $elem.find(`[data-theme-toc="true"]`);
|
||||||
|
|
||||||
|
if (!dToc.length) dToc = $elem.parents('.wiki');
|
||||||
if (!dToc.length) return this;
|
if (!dToc.length) return this;
|
||||||
|
|
||||||
const body = $elem;
|
const body = $elem;
|
||||||
body.find("div, aside, blockquote, article, details").each(function() {
|
body.find("div, aside, blockquote, article, details").each(function() {
|
||||||
$(this)
|
$(this)
|
||||||
|
@ -293,6 +291,10 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ids = {};
|
||||||
|
path = [];
|
||||||
|
lastLevel = 1;
|
||||||
|
|
||||||
body.find(dTocHeadingSelectors).each(function() {
|
body.find(dTocHeadingSelectors).each(function() {
|
||||||
if ($(this).hasClass("d-toc-ignore")) return;
|
if ($(this).hasClass("d-toc-ignore")) return;
|
||||||
const heading = $(this);
|
const heading = $(this);
|
||||||
|
@ -302,6 +304,31 @@
|
||||||
if (!id.length) {
|
if (!id.length) {
|
||||||
id = cleanUp(heading.text());
|
id = cleanUp(heading.text());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
level = parseInt(heading.prop("tagName")[1])
|
||||||
|
if (level == 1) {
|
||||||
|
path = [id];
|
||||||
|
} else if (level > lastLevel) {
|
||||||
|
path.push(id);
|
||||||
|
} else {
|
||||||
|
if (level < lastLevel) {
|
||||||
|
path.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
path[path.length - 1] = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastLevel = level;
|
||||||
|
id = path.join('-');
|
||||||
|
|
||||||
|
|
||||||
|
if (id in ids) {
|
||||||
|
id = id + '-' + ids[id]++;
|
||||||
|
} else {
|
||||||
|
ids[id] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
heading
|
heading
|
||||||
.attr({
|
.attr({
|
||||||
|
|
Loading…
Add table
Reference in a new issue