diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-05-06 15:25:27 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-05-06 15:25:27 +0900 |
| commit | 83c58924bfd9f22ef4ff84fd3439535e4753ec53 (patch) | |
| tree | 90034e4106965433eb587610edfe4356853b96a2 /vhosts/blog/nuldoc-src/djot/to_html.ts | |
| parent | ce9308b47d5eba482862ae24968c198bbf466e21 (diff) | |
| download | nsfisis.dev-83c58924bfd9f22ef4ff84fd3439535e4753ec53.tar.gz nsfisis.dev-83c58924bfd9f22ef4ff84fd3439535e4753ec53.tar.zst nsfisis.dev-83c58924bfd9f22ef4ff84fd3439535e4753ec53.zip | |
feat(blog/nuldoc): implement footnote
Diffstat (limited to 'vhosts/blog/nuldoc-src/djot/to_html.ts')
| -rw-r--r-- | vhosts/blog/nuldoc-src/djot/to_html.ts | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/vhosts/blog/nuldoc-src/djot/to_html.ts b/vhosts/blog/nuldoc-src/djot/to_html.ts index 3c95c14b..cc74e538 100644 --- a/vhosts/blog/nuldoc-src/djot/to_html.ts +++ b/vhosts/blog/nuldoc-src/djot/to_html.ts @@ -250,21 +250,84 @@ function setDefaultLangAttribute(_doc: Document) { } function traverseFootnotes(doc: Document) { + let footnoteCounter = 0; + const footnoteMap = new Map<string, number>(); + + forEachChildRecursively(doc.root, (n) => { + if (n.kind !== "element" || n.name !== "footnoteref") { + return; + } + + const reference = n.attributes.get("reference"); + if (!reference) { + return; + } + + let footnoteNumber: number; + if (footnoteMap.has(reference)) { + footnoteNumber = footnoteMap.get(reference)!; + } else { + footnoteNumber = ++footnoteCounter; + footnoteMap.set(reference, footnoteNumber); + } + + n.name = "sup"; + n.attributes.delete("reference"); + n.attributes.set("class", "footnote"); + n.children = [ + { + kind: "element", + name: "a", + attributes: new Map([ + ["id", `footnoteref--${reference}`], + ["class", "footnote"], + ["href", `#footnote--${reference}`], + ]), + children: [ + { + kind: "text", + content: `[${footnoteNumber}]`, + raw: false, + }, + ], + }, + ]; + }); + forEachChildRecursively(doc.root, (n) => { if (n.kind !== "element" || n.name !== "footnote") { return; } - // TODO - // <footnote>x</footnote> - // - // <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1">1</a>]</sup> - // - // <div class="footnote" id="_footnotedef_1"> - // <a href="#_footnoteref_1">1</a>. RAS syndrome - // </div> - n.name = "span"; - n.children = []; + const id = n.attributes.get("id"); + if (!id || !footnoteMap.has(id)) { + n.name = "span"; + n.children = []; + return; + } + + const footnoteNumber = footnoteMap.get(id)!; + + n.name = "div"; + n.attributes.delete("id"); + n.attributes.set("class", "footnote"); + n.attributes.set("id", `footnote--${id}`); + + n.children = [ + { + kind: "element", + name: "a", + attributes: new Map([["href", `#footnoteref--${id}`]]), + children: [ + { + kind: "text", + content: `${footnoteNumber}. `, + raw: false, + }, + ], + }, + ...n.children, + ]; }); } |
