]> Lady’s Gitweb - GitWikiWeb/blobdiff - build.js
Render footnotes inside of doc content
[GitWikiWeb] / build.js
index ca07283ba29bb747f7b353661da5b224607e554c..3d3a862c3f09af3e5ddc72dc9a89934a4c5263c2 100644 (file)
--- a/build.js
+++ b/build.js
@@ -235,7 +235,6 @@ class GitWikiWebPage {
             const links_section = [];
             if (internalLinks.size || externalLinks.size) {
               links_section.push(
-                rawBlock`<footer>`,
                 rawBlock`<nav id="links">`,
                 {
                   tag: "heading",
@@ -294,35 +293,16 @@ class GitWikiWebPage {
               }
               links_section.push(
                 rawBlock`</nav>`,
-                rawBlock`</footer>`,
               );
             } else {
               /* do nothing */
             }
-            e.children.push(...links_section);
-          },
-        },
-        emph: {
-          enter: (_) => {},
-          exit: (e) => {
-            const attributes = e.attributes ?? NIL;
-            const { as } = attributes;
-            if (as) {
-              delete attributes.as;
-              if (
-                as == "b" || as == "cite" || as == "i" || as == "u"
-              ) {
-                return [
-                  rawInline`<${as}>`,
-                  ...e.children,
-                  rawInline`</${as}>`,
-                ];
-              } else {
-                /* do nothing */
-              }
-            } else {
-              /* do nothing */
-            }
+            e.children.push(
+              rawBlock`<footer>`,
+              rawBlock`${"\uFFFF"}`, // footnote placeholder
+              ...links_section,
+              rawBlock`</footer>`,
+            );
           },
         },
         hard_break: {
@@ -513,6 +493,38 @@ class GitWikiWebPage {
   }
 }
 
+{
+  // Patches for Djot HTML renderer.
+  const { HTMLRenderer: { prototype: htmlRendererPrototype } } = djot;
+  const { inTags: upstreamInTags } = htmlRendererPrototype;
+  htmlRendererPrototype.inTags = function (
+    tag,
+    node,
+    newlines,
+    extraAttrs = undefined,
+  ) {
+    const attributes = node.attributes ?? NIL;
+    if ("as" in attributes) {
+      const newTag = attributes.as;
+      delete attributes.as;
+      return upstreamInTags.call(
+        this,
+        newTag,
+        node,
+        newlines,
+        extraAttrs,
+      );
+    } else {
+      return upstreamInTags.call(
+        this,
+        tag,
+        node,
+        newlines,
+        extraAttrs,
+      );
+    }
+  };
+}
 {
   const config = await getRemoteContent("config.yaml").then((yaml) =>
     parseYaml(yaml, { schema: JSON_SCHEMA })
@@ -912,8 +924,25 @@ class GitWikiWebPage {
           },
         };
       });
+      const renderedAST = djot.renderAST(ast);
       const doc = getDOM(template);
-      const result = getDOM(`${djot.renderHTML(ast)}`);
+      const result = getDOM(djot.renderHTML(ast, {
+        overrides: {
+          raw_block: (node, context) => {
+            if (node.format == "html" && node.text == "\uFFFF") {
+              if (context.nextFootnoteIndex > 1) {
+                const result = context.renderNotes(ast.footnotes);
+                context.nextFootnoteIndex = 1;
+                return result;
+              } else {
+                return "";
+              }
+            } else {
+              return context.renderAstNodeDefault(node);
+            }
+          },
+        },
+      }));
       const headElement = domutils.findOne(
         (node) => node.name == "head",
         doc,
@@ -953,7 +982,7 @@ class GitWikiWebPage {
           "GitWikiWeb: Template did not include a <gitwikiweb-content> element.",
         );
       } else {
-        for (const node of result) {
+        for (const node of [...result]) {
           domutils.prepend(contentElement, node);
         }
         domutils.removeElement(contentElement);
@@ -970,6 +999,13 @@ class GitWikiWebPage {
           { createNew: true },
         ),
       );
+      promises.push(
+        Deno.writeTextFile(
+          `${DESTINATION}/${pageRef}/index.ast`,
+          renderedAST,
+          { createNew: true },
+        ),
+      );
       promises.push(
         Deno.writeTextFile(
           `${DESTINATION}/${pageRef}/source.djot`,
This page took 0.062665 seconds and 4 git commands to generate.