summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/ndoc
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-04-02 00:11:46 +0900
committernsfisis <nsfisis@gmail.com>2025-04-02 00:11:46 +0900
commitc0c73379890f1a4ee6ee07b1aee188c33ef66ab0 (patch)
tree62ad848a658fa0dc8a43bc106e05aadcd4a70b8f /vhosts/blog/nuldoc-src/ndoc
parentf325479ddb8ccdbe0e832160e9c7fb0155a90f47 (diff)
downloadnsfisis.dev-c0c73379890f1a4ee6ee07b1aee188c33ef66ab0.tar.gz
nsfisis.dev-c0c73379890f1a4ee6ee07b1aee188c33ef66ab0.tar.zst
nsfisis.dev-c0c73379890f1a4ee6ee07b1aee188c33ef66ab0.zip
feat(blog/nuldoc): dynamically join nested section ids
Diffstat (limited to 'vhosts/blog/nuldoc-src/ndoc')
-rw-r--r--vhosts/blog/nuldoc-src/ndoc/to_html.ts42
1 files changed, 37 insertions, 5 deletions
diff --git a/vhosts/blog/nuldoc-src/ndoc/to_html.ts b/vhosts/blog/nuldoc-src/ndoc/to_html.ts
index d4630d59..a82f0333 100644
--- a/vhosts/blog/nuldoc-src/ndoc/to_html.ts
+++ b/vhosts/blog/nuldoc-src/ndoc/to_html.ts
@@ -94,14 +94,46 @@ function transformLinkLikeToAnchorElement(doc: Document) {
}
function transformSectionIdAttribute(doc: Document) {
- forEachChildRecursively(doc.root, (n) => {
- if (n.kind !== "element" || n.name !== "section") {
+ const sectionStack: string[] = [];
+ const usedIds = new Set<string>();
+
+ const processNode = (n: Node) => {
+ if (n.kind !== "element") {
return;
}
- const idAttr = n.attributes.get("id");
- n.attributes.set("id", `section--${idAttr}`);
- });
+ if (n.name === "section") {
+ const idAttr = n.attributes.get("id");
+ if (!idAttr) {
+ return;
+ }
+
+ let newId: string;
+ if (sectionStack.length === 0) {
+ newId = `section--${idAttr}`;
+ } else {
+ newId = `section--${sectionStack.join("--")}--${idAttr}`;
+ }
+
+ if (usedIds.has(newId)) {
+ throw new NuldocError(
+ `[nuldoc.tohtml] Duplicate section ID: ${newId}`,
+ );
+ }
+
+ usedIds.add(newId);
+ n.attributes.set("id", newId);
+ sectionStack.push(idAttr);
+
+ forEachChild(n, processNode);
+
+ sectionStack.pop();
+ } else {
+ forEachChild(n, processNode);
+ }
+ };
+
+ forEachChild(doc.root, processNode);
}
function setSectionTitleAnchor(doc: Document) {