]> Lady’s Gitweb - Blog/blob - build.js
641bd94a3a2983b4d9f28a6b8fd6883a3fbe31cc
[Blog] / build.js
1 #!/usr/bin/env -S deno run --allow-read --allow-write
2
3 // Copyright © 2023 Lady [@ Lady’s Computer].
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
8
9 import hljs from "npm:highlight.js@11.8.0";
10
11 const processContent = (content) => {
12 if (content == null || Object(content) instanceof String) {
13 /* do nothing */
14 } else if (Array.isArray(content)) {
15 // The provided content is an array.
16 content.forEach(processContent);
17 } else {
18 for (const child of Array.from(content)) {
19 applyHighlighting(child);
20 }
21 }
22 };
23
24 const applyHighlighting = (node) => {
25 if (
26 node.localName == "pre" && node.childNodes.length == 1 &&
27 node.firstChild.localName == "code"
28 ) {
29 const code = node.firstChild;
30 const language = /language-(.*)/u.exec(code.getAttribute("class"))
31 ?.[1];
32 if (language) {
33 try {
34 const { value: highlight } = hljs.highlight(
35 node.firstChild.textContent,
36 { language },
37 );
38 const nodes = new DOMParser().parseFromString(
39 `<!DOCTYPE html><html><body>${highlight}</body></html>`,
40 "text/html",
41 ).documentElement.lastChild.childNodes;
42 code.textContent = "";
43 for (const node of Array.from(nodes)) {
44 code.appendChild(code.ownerDocument.importNode(node, true));
45 }
46 } catch (e) {
47 console.warn(e);
48 }
49 }
50 } else if (node.nodeType == 1) {
51 Array.from(node.childNodes).forEach(applyHighlighting);
52 }
53 };
54
55 globalThis.bjørnTransformMetadata = (metadata, _type) => {
56 processContent(metadata.content);
57 processContent(metadata.summary);
58 };
59
60 await import(
61 "https://git.ladys.computer/Beorn/blob_plain/0.2.1:/build.js"
62 );
This page took 0.044958 seconds and 3 git commands to generate.