@xiee/utils 1.3.7 → 1.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/js/toc.js +12 -7
- package/js/toc.min.js +1 -1
- package/package.json +1 -1
package/js/toc.js
CHANGED
|
@@ -8,10 +8,13 @@
|
|
|
8
8
|
const hs = b.querySelectorAll([1, 2, 3, 4, 5, 6].map(i => `:scope > h${i}`).join(','));
|
|
9
9
|
if (hs.length === 0) return;
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
let toc = d.getElementById('TOC');
|
|
12
|
+
if (toc) {
|
|
13
|
+
toc.innerHTML = ''; // empty and rebuild TOC if it has been generated (e.g., by Pandoc)
|
|
14
|
+
} else {
|
|
15
|
+
toc = d.createElement('div');
|
|
16
|
+
toc.id = 'TOC';
|
|
17
|
+
}
|
|
15
18
|
|
|
16
19
|
let li, ul;
|
|
17
20
|
let p = toc; // the current parent into which we insert child TOC items
|
|
@@ -44,9 +47,11 @@
|
|
|
44
47
|
p.appendChild(a);
|
|
45
48
|
t0 = t1;
|
|
46
49
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
if (!toc.parentNode) {
|
|
51
|
+
// if there is <header> in the article body, insert TOC after it
|
|
52
|
+
const header = b.querySelector('header');
|
|
53
|
+
header ? header.after(toc) : b.insertBefore(toc, b.firstChild);
|
|
54
|
+
}
|
|
50
55
|
|
|
51
56
|
// check if headings are numbered
|
|
52
57
|
toc.querySelector('span.section-number') && toc.firstElementChild.classList.add('numbered');
|
package/js/toc.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(e=>{let t;[".article",".body","article","body"].forEach((r=>{t||(t=e.querySelector(r))}));const r=t.querySelectorAll([1,2,3,4,5,6].map((e=>`:scope > h${e}`)).join(","));if(0===r.length)return;
|
|
1
|
+
(e=>{let t;[".article",".body","article","body"].forEach((r=>{t||(t=e.querySelector(r))}));const r=t.querySelectorAll([1,2,3,4,5,6].map((e=>`:scope > h${e}`)).join(","));if(0===r.length)return;let n,i,l=e.getElementById("TOC");l?l.innerHTML="":(l=e.createElement("div"),l.id="TOC");let a,d=l,o=0;if(r.forEach((t=>{if(a=parseInt(t.tagName.replace(/^h/i,"")),n=e.createElement("li"),a>o)i=e.createElement("ul"),i.appendChild(n),d.appendChild(i);else if(a<o)for(let e=0;e<o-a;e++)d=d.parentNode.parentNode;a<=o&&d.parentNode.appendChild(n),d=n;const r=e.createElement("a");if(r.innerHTML=t.innerHTML,t.id)r.href="#"+t.id;else{const e=t.parentNode;e.classList.contains("section")&&e.id&&(r.href="#"+e.id)}d.appendChild(r),o=a})),!l.parentNode){const e=t.querySelector("header");e?e.after(l):t.insertBefore(l,t.firstChild)}l.querySelector("span.section-number")&&l.firstElementChild.classList.add("numbered")})(document);
|