2021-12-29 01:55:43 +00:00
|
|
|
for (const el of document.querySelectorAll("[id]")) {
|
2022-11-18 04:49:39 +00:00
|
|
|
if (el.tagName === "marker") continue;
|
2022-11-17 17:53:00 +00:00
|
|
|
el.classList.add("haspara");
|
2021-12-29 01:55:43 +00:00
|
|
|
const pilcrow = document.createElement("a");
|
2022-11-17 17:53:00 +00:00
|
|
|
pilcrow.className = "pilcrow";
|
2021-12-29 01:55:43 +00:00
|
|
|
pilcrow.href = "#" + el.id;
|
2022-11-17 17:53:00 +00:00
|
|
|
pilcrow.innerHTML = "¶";
|
|
|
|
el.prepend(pilcrow);
|
2021-12-29 01:55:43 +00:00
|
|
|
}
|
2022-11-17 17:53:00 +00:00
|
|
|
|
|
|
|
const foldable = (root, children) => {
|
|
|
|
let state = true;
|
|
|
|
root.addEventListener("click", (e) => {
|
|
|
|
if (e.target.classList.contains("pilcrow")) state = true;
|
|
|
|
else state = !state;
|
|
|
|
|
2022-11-18 04:49:39 +00:00
|
|
|
if (state) children.classList.remove("closed");
|
|
|
|
else children.classList.add("closed");
|
2022-11-17 17:53:00 +00:00
|
|
|
if (state) root.classList.remove("closed");
|
|
|
|
else root.classList.add("closed");
|
|
|
|
});
|
|
|
|
root.classList.add("toggle-root");
|
|
|
|
};
|
|
|
|
|
|
|
|
const make_foldable = (root) => {
|
|
|
|
const child_stacks = new Array(10).fill(null).map(() => ({ children: [], root: null }));
|
|
|
|
|
|
|
|
const flush_header = (this_level, sibling) => {
|
|
|
|
for (let level = 9; level >= this_level; level--) {
|
|
|
|
const stack = child_stacks[level];
|
|
|
|
|
|
|
|
if (!stack.root) continue;
|
|
|
|
|
|
|
|
const new_e = document.createElement("div");
|
2022-11-18 04:49:39 +00:00
|
|
|
new_e.classList.add("toggle-section");
|
2022-11-17 17:53:00 +00:00
|
|
|
for (const old_e of stack.children) {
|
|
|
|
old_e.remove();
|
|
|
|
new_e.appendChild(old_e);
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:55:15 +00:00
|
|
|
if (stack.root.tagName !== "H1") foldable(stack.root, new_e);
|
2022-11-17 17:53:00 +00:00
|
|
|
let parent_level;
|
|
|
|
for (parent_level = level - 1; parent_level > 0; parent_level--) if (child_stacks[parent_level].root) break;
|
|
|
|
|
|
|
|
if (parent_level === -1) {
|
|
|
|
if (sibling) root.insertBefore(new_e, sibling);
|
|
|
|
else root.appendChild(new_e);
|
|
|
|
} else {
|
|
|
|
stack.root.remove();
|
|
|
|
child_stacks[parent_level].children.push(stack.root);
|
|
|
|
child_stacks[parent_level].children.push(new_e);
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.root = null;
|
|
|
|
stack.children.length = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-18 04:49:39 +00:00
|
|
|
let end = null;
|
2022-11-17 17:53:00 +00:00
|
|
|
for (const child of [...root.children]) {
|
2022-11-18 04:49:39 +00:00
|
|
|
if (child.tagName === "FOOTER") {
|
|
|
|
end = child;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:53:00 +00:00
|
|
|
if (/^H\d$/.test(child.tagName)) {
|
|
|
|
const this_level = parseInt(child.tagName[1]) - 1;
|
|
|
|
|
|
|
|
flush_header(this_level, child);
|
|
|
|
|
|
|
|
child_stacks[this_level].root = child;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let level = 9; level >= 0; level--) {
|
|
|
|
if (child_stacks[level].root) {
|
|
|
|
child_stacks[level].children.push(child);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let level = 9; level >= 0; level--) {
|
2022-11-18 04:49:39 +00:00
|
|
|
flush_header(level, end);
|
2022-11-17 17:53:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
make_foldable(document.body);
|