-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
26 lines (20 loc) · 784 Bytes
/
script.js
File metadata and controls
26 lines (20 loc) · 784 Bytes
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
const headers = [...document.querySelectorAll("main article :is(h1, h2, h3)")];
const toc = document.querySelector(".table-of-contents-items");
headers.map((heading, index) => {
const level = Number(heading.tagName.slice(1)) - 1;
const text = heading.textContent;
const id = index.toString();
const anchor = document.createElement("a");
anchor.textContent = text;
anchor.id = id;
heading.replaceChildren(anchor);
return [level, id, text];
}).forEach(([level, id, text]) => {
const tocEntry = document.createElement("li");
tocEntry.style.marginLeft = `${1.5 * level}em`;
const link = document.createElement("a");
link.href = `#${id}`;
link.textContent = text;
tocEntry.appendChild(link);
toc.appendChild(tocEntry);
});