-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoc.js
More file actions
51 lines (44 loc) · 1.56 KB
/
Copy pathtoc.js
File metadata and controls
51 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** 长页自动侧栏目录:page-wrap 内 ≥2 个 .sec-title 且尚无 sticky-toc 时启用 */
(function () {
function slugify(text, index) {
return (
'sec-' +
String(index + 1) +
'-' +
text
.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]+/g, '-')
.replace(/^-|-$/g, '')
.slice(0, 24)
.toLowerCase() || index
);
}
function initAutoToc() {
if (document.body.dataset.noAutoToc === 'true') return;
const wrap = document.querySelector('.page-wrap:not(.page-cols)');
if (!wrap || wrap.querySelector('.sticky-toc')) return;
const titles = [...wrap.querySelectorAll('.sec-title')];
if (titles.length < 2) return;
titles.forEach((el, i) => {
if (!el.id) el.id = slugify(el.textContent.trim(), i);
});
const toc = document.createElement('nav');
toc.className = 'sticky-toc';
toc.setAttribute('aria-label', '本章目录');
toc.innerHTML =
'<h5>本章目录</h5>' +
titles
.map((t) => {
const label = t.textContent.trim().replace(/\s+/g, ' ');
const short = label.length > 22 ? label.slice(0, 22) + '…' : label;
return `<a href="#${t.id}" title="${label.replace(/"/g, '"')}">${short}</a>`;
})
.join('');
const main = document.createElement('div');
main.className = 'page-main';
while (wrap.firstChild) main.appendChild(wrap.firstChild);
wrap.classList.add('page-cols');
wrap.appendChild(toc);
wrap.appendChild(main);
}
document.addEventListener('DOMContentLoaded', initAutoToc);
})();