]> Lady’s Gitweb - GitWikiWeb/blobdiff - build.js
Don’t add .sig links to the internal links list
[GitWikiWeb] / build.js
index bd8ff27ba2c0fe3743ba4132236fd65237c8cfbc..3b40c3a347fa98ab45f0dd59a67fb317e03b5ca6 100644 (file)
--- a/build.js
+++ b/build.js
@@ -147,8 +147,8 @@ const logErrorsAndCollectResults = (results) =>
   });
 
 const getReferenceFromPath = (path) =>
-  /Sources\/([A-Z][0-9A-Za-z]*\/[A-Z][0-9A-Za-z]*)\.djot$/u.exec(path)
-    ?.[1]?.replace?.("/", ":");
+  /Sources\/([A-Z][0-9A-Za-z]*(?:\/[A-Z][0-9A-Za-z]*)+)\.djot$/u
+    .exec(path)?.[1]?.replace?.("/", ":"); // only replaces first slash
 
 const listOfInternalLinks = (references, wrapper = ($) => $) => ({
   tag: "bullet_list",
@@ -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: {
@@ -351,7 +331,7 @@ class GitWikiWebPage {
             e.attributes ??= {};
             const { attributes, reference, destination } = e;
             if (
-              /^(?:[A-Z][0-9A-Za-z]*|[@#])?:(?:[A-Z][0-9A-Za-z]*)?$/u
+              /^(?:[A-Z][0-9A-Za-z]*|[@#])?:(?:[A-Z][0-9A-Za-z]*(?:\/[A-Z][0-9A-Za-z]*)*)?$/u
                 .test(reference ?? "")
             ) {
               const [namespacePrefix, pageName] = splitReference(
@@ -365,11 +345,20 @@ class GitWikiWebPage {
               const resolvedReference = pageName == ""
                 ? `Namespace:${expandedNamespace}`
                 : `${expandedNamespace}:${pageName}`;
-              this.#internalLinks.add(resolvedReference);
               e.reference = resolvedReference;
               attributes["data-realm"] = "internal";
               attributes["data-pagename"] = pageName;
               attributes["data-namespace"] = expandedNamespace;
+              if (
+                resolvedReference.startsWith("Editor:") &&
+                (attributes.class ?? "").split(/\s/gu).includes("sig")
+              ) {
+                // This is a special internal link; do not record it.
+                /* do nothing */
+              } else {
+                // This is a non‐special internal link; record it.
+                internalLinks.add(resolvedReference);
+              }
             } else {
               attributes["data-realm"] = "external";
               const remote = destination ??
@@ -513,6 +502,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 })
@@ -582,6 +603,7 @@ class GitWikiWebPage {
                 `GitWikiWeb: git cat-file returned nonzero exit code: ${catstatus.code}.`,
               );
             } else {
+              const reference = `${namespace}:${pageName}`;
               const page = new GitWikiWebPage(
                 namespace,
                 pageName,
@@ -592,7 +614,6 @@ class GitWikiWebPage {
                 source,
                 config,
               );
-              const reference = `${namespace}:${pageName}`;
               pages.set(reference, page);
               requiredButMissingPages.delete(reference);
             }
@@ -664,7 +685,8 @@ class GitWikiWebPage {
         }
         const results = new Array(6);
         const seen = new Set();
-        let recency = 5;
+        const maxRecency = Math.max(config.max_recency | 0, 0);
+        let recency = maxRecency;
         let current;
         do {
           const show = new Deno.Command("git", {
@@ -672,7 +694,7 @@ class GitWikiWebPage {
               "show",
               "-s",
               "--format=%H%x00%cI%x00%cD",
-              recency ? `HEAD~${5 - recency}` : commit,
+              recency ? `HEAD~${maxRecency - recency}` : commit,
             ],
             stdout: "piped",
             stderr: "piped",
@@ -911,8 +933,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,
@@ -952,7 +991,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);
@@ -969,6 +1008,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.030596 seconds and 4 git commands to generate.