]> Lady’s Gitweb - Blog/blobdiff - build.js
Support autogenerating an entry links footer
[Blog] / build.js
index a9d2bafc0bb5f624d5654dd3a7f950586e1e5657..0260323ec3afd2aa253c5c0a15d500b75594340a 100755 (executable)
--- a/build.js
+++ b/build.js
@@ -8,24 +8,29 @@
 
 import hljs from "npm:highlight.js@11.8.0";
 
-const processContent = (content) => {
+const contentLinks = new WeakMap();
+
+const processContent = function (content) {
   if (content == null || Object(content) instanceof String) {
+    // The provided content is nullish or a string.
     /* do nothing */
   } else if (Array.isArray(content)) {
     // The provided content is an array.
-    content.forEach(processContent);
+    content.forEach(processContent.bind(this));
   } else {
+    // The provided content is a NodeList.
     for (const child of Array.from(content)) {
-      applyHighlighting(child);
+      applyTransforms.call(this, child);
     }
   }
 };
 
-const applyHighlighting = (node) => {
+const applyTransforms = function (node) {
   if (
     node.localName == "pre" && node.childNodes.length == 1 &&
     node.firstChild.localName == "code"
   ) {
+    // This is a code block and should be highlighted.
     const code = node.firstChild;
     const language = /language-(.*)/u.exec(code.getAttribute("class"))
       ?.[1];
@@ -47,20 +52,31 @@ const applyHighlighting = (node) => {
         for (const node of Array.from(nodes)) {
           code.appendChild(code.ownerDocument.importNode(node, true));
         }
+        return; // don’t continue processing this node
       } catch (e) {
         console.warn(e);
       }
     }
-  } else if (node.nodeType == 1) {
-    Array.from(node.childNodes).forEach(applyHighlighting);
+  } else if (node.localName == "a") {
+    const href = node.getAttribute("href");
+    const title = node.getAttribute("title");
+    if (title && href) {
+      contentLinks.get(this)[title] = href;
+    }
+  }
+  if (node.nodeType == 1) {
+    // This is an element; process its children.
+    Array.from(node.childNodes).forEach(applyTransforms.bind(this));
   } else if (node.nodeType == 3) {
+    // This is a text node; apply replacements.
     node.textContent = node.textContent.replaceAll(":—", ":\u2060—");
   }
 };
 
 globalThis.bjørnTransformMetadata = (metadata, _type) => {
-  processContent(metadata.content);
-  processContent(metadata.summary);
+  contentLinks.set(metadata, {});
+  processContent.call(metadata, metadata.content);
+  processContent.call(metadata, metadata.summary);
 };
 
 globalThis.bjørnTransformFeedHTML = (document, _metadata) => {
@@ -76,6 +92,23 @@ globalThis.bjørnTransformFeedHTML = (document, _metadata) => {
   h1.appendChild(link);
 };
 
+globalThis.bjørnTransformEntryHTML = (document, metadata) => {
+  const LMN = Lemon.bind({ document });
+  const links = Object.entries(contentLinks.get(metadata));
+  if (links.length) {
+    document.getElementById("entry.content").appendChild(
+      LMN.footer`${LMN.nav`${[
+        LMN.h2`This post contains links.`,
+        LMN.ul`${
+          links.map(([name, href]) =>
+            LMN.li`${LMN.a.href(href)`${name}`}`
+          )
+        }`,
+      ]}`}`,
+    );
+  }
+};
+
 await import(
   "https://git.ladys.computer/Beorn/blob_plain/0.2.3:/build.js"
 );
This page took 0.025455 seconds and 4 git commands to generate.