table of contents now generate readable header IDs

This commit is contained in:
2025-12-18 21:34:31 +00:00
parent 1c096b4e92
commit eda8f2849e

View File

@@ -33,10 +33,15 @@
return document.querySelectorAll("h2, h3, h4, h5");
}
// Generates an ID for a given header node. Does not override
// any existing ID.
let getHeaderId = function(header: Node): string {
var id = (header as HTMLElement).id;
if (!id) {
id = `header-${idCounter}`;
var text = (header as HTMLElement).innerText.toLowerCase();
text = text.replaceAll(/[^a-zA-Z0-9]/gi, "-");
id = text;
(header as HTMLElement).id = id;
idCounter += 1;
}