]> Lady’s Gitweb - Blog/commitdiff
Add highlighting support for code blocks
authorLady <redacted>
Wed, 3 May 2023 01:33:34 +0000 (18:33 -0700)
committerLady <redacted>
Wed, 3 May 2023 02:25:22 +0000 (19:25 -0700)
This code is somewhat messy because it has to work around the extreme
limitations of the xmldom window polyfill provided by Lemon.

- Updates the dependency on Beorn to 0.2.1 and adds a new one for
  highlight.js at 11.8.0.

- Defines a wrapper build script to set up a transformation hook and
  then dynamically run Beorn through an import.

- Updates .rsync-filter to exclude these new files.

.rsync-filter
GNUmakefile
build.js [new file with mode: 0755]
deno.json [new file with mode: 0644]

index b238190d64feb31845c7cf076c3d8965a2e6a3ca..01b3406a3b2ba4064a78ee8e9c68122ac98d8f9d 100644 (file)
@@ -1,6 +1,8 @@
 -s .DS_Store
 -s .git
 -s .gitignore
+-s deno.json
+-s /build.js
 -s /GNUmakefile
 -s /.grass
 - /.rsync-filter
index 3095070b6d0f64ec880db300a74ffb64fc7819dd..54916363b1cd835762ade7215ee636d341ea0cc9 100644 (file)
@@ -6,9 +6,6 @@ SHELL = /bin/sh
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
 
-BEORN_VERSION = 0.1.0
-BEORN = https://git.ladys.computer/Beorn/blob_plain/$(BEORN_VERSION):/build.js
-
 # This Makefile requires rsync 3 or newer.
 RSYNC = rsync
 RSYNC_FLAGS = -Oclmrtvz
@@ -32,7 +29,7 @@ DESTINATION = $(DESTINATION_HOST):$(DESTINATION_PATH)
 GIT_FORCE =
 
 build:
-       deno run --allow-read=. --allow-write=. $(BEORN)
+       deno run --allow-read=. --allow-write=. ./build.js
        touch .grass
 
 ensure-build:
@@ -45,7 +42,6 @@ ensure-branch-up-to-date:
 
 ensure-clean:
        @if output=$$(git status --porcelain) && [ ! -z "$$output" ]; then echo 'Error: There are uncommitted changes!' >&2; echo 'Commit changes and run `make` before syncing.' >&2; exit 1; fi
-       @if output=$$(git status --porcelain) && [ ! -z "$$output" ]; then echo 'Error: There are uncommitted changes!' >&2; echo 'Commit changes and run `make` before syncing.' >&2; exit 1; fi
 
 dry-sync: ensure-clean$(if $(GIT_FORCE),, ensure-branch-up-to-date) ensure-build
        $(RSYNC) $(RSYNC_FLAGS) --del --dry-run --filter=". $(RSYNC_FILTER)" --iconv=$(SOURCE_CHARSET),$(DESTINATION_CHARSET) $(RSYNC_OPTIONS) . $(DESTINATION)
diff --git a/build.js b/build.js
new file mode 100755 (executable)
index 0000000..641bd94
--- /dev/null
+++ b/build.js
@@ -0,0 +1,62 @@
+#!/usr/bin/env -S deno run --allow-read --allow-write
+
+// Copyright © 2023 Lady [@ Lady’s Computer].
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
+
+import hljs from "npm:highlight.js@11.8.0";
+
+const processContent = (content) => {
+  if (content == null || Object(content) instanceof String) {
+    /* do nothing */
+  } else if (Array.isArray(content)) {
+    // The provided content is an array.
+    content.forEach(processContent);
+  } else {
+    for (const child of Array.from(content)) {
+      applyHighlighting(child);
+    }
+  }
+};
+
+const applyHighlighting = (node) => {
+  if (
+    node.localName == "pre" && node.childNodes.length == 1 &&
+    node.firstChild.localName == "code"
+  ) {
+    const code = node.firstChild;
+    const language = /language-(.*)/u.exec(code.getAttribute("class"))
+      ?.[1];
+    if (language) {
+      try {
+        const { value: highlight } = hljs.highlight(
+          node.firstChild.textContent,
+          { language },
+        );
+        const nodes = new DOMParser().parseFromString(
+          `<!DOCTYPE html><html><body>${highlight}</body></html>`,
+          "text/html",
+        ).documentElement.lastChild.childNodes;
+        code.textContent = "";
+        for (const node of Array.from(nodes)) {
+          code.appendChild(code.ownerDocument.importNode(node, true));
+        }
+      } catch (e) {
+        console.warn(e);
+      }
+    }
+  } else if (node.nodeType == 1) {
+    Array.from(node.childNodes).forEach(applyHighlighting);
+  }
+};
+
+globalThis.bjørnTransformMetadata = (metadata, _type) => {
+  processContent(metadata.content);
+  processContent(metadata.summary);
+};
+
+await import(
+  "https://git.ladys.computer/Beorn/blob_plain/0.2.1:/build.js"
+);
diff --git a/deno.json b/deno.json
new file mode 100644 (file)
index 0000000..05375a3
--- /dev/null
+++ b/deno.json
@@ -0,0 +1 @@
+{ "fmt": { "lineWidth": 71 }, "lock": false }
This page took 0.054115 seconds and 4 git commands to generate.