]> Lady’s Gitweb - Blog/blob - build.js
0260323ec3afd2aa253c5c0a15d500b75594340a
[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 contentLinks = new WeakMap();
12
13 const processContent = function (content) {
14 if (content == null || Object(content) instanceof String) {
15 // The provided content is nullish or a string.
16 /* do nothing */
17 } else if (Array.isArray(content)) {
18 // The provided content is an array.
19 content.forEach(processContent.bind(this));
20 } else {
21 // The provided content is a NodeList.
22 for (const child of Array.from(content)) {
23 applyTransforms.call(this, child);
24 }
25 }
26 };
27
28 const applyTransforms = function (node) {
29 if (
30 node.localName == "pre" && node.childNodes.length == 1 &&
31 node.firstChild.localName == "code"
32 ) {
33 // This is a code block and should be highlighted.
34 const code = node.firstChild;
35 const language = /language-(.*)/u.exec(code.getAttribute("class"))
36 ?.[1];
37 if (language) {
38 try {
39 const { value: highlight } = hljs.highlight(
40 node.firstChild.textContent,
41 { language },
42 );
43 const nodes = new DOMParser().parseFromString(
44 `<!DOCTYPE html><html><body>${highlight}</body></html>`,
45 "text/html",
46 ).documentElement.lastChild.childNodes;
47 code.setAttribute(
48 "class",
49 `hljs ${code.getAttribute("class")}`,
50 );
51 code.textContent = "";
52 for (const node of Array.from(nodes)) {
53 code.appendChild(code.ownerDocument.importNode(node, true));
54 }
55 return; // don’t continue processing this node
56 } catch (e) {
57 console.warn(e);
58 }
59 }
60 } else if (node.localName == "a") {
61 const href = node.getAttribute("href");
62 const title = node.getAttribute("title");
63 if (title && href) {
64 contentLinks.get(this)[title] = href;
65 }
66 }
67 if (node.nodeType == 1) {
68 // This is an element; process its children.
69 Array.from(node.childNodes).forEach(applyTransforms.bind(this));
70 } else if (node.nodeType == 3) {
71 // This is a text node; apply replacements.
72 node.textContent = node.textContent.replaceAll(":—", ":\u2060—");
73 }
74 };
75
76 globalThis.bjørnTransformMetadata = (metadata, _type) => {
77 contentLinks.set(metadata, {});
78 processContent.call(metadata, metadata.content);
79 processContent.call(metadata, metadata.summary);
80 };
81
82 globalThis.bjørnTransformFeedHTML = (document, _metadata) => {
83 const LMN = Lemon.bind({ document });
84 const h1 = document.getElementsByTagNameNS(
85 "http://www.w3.org/1999/xhtml",
86 "h1",
87 )[0];
88 const link = LMN.a.href("/")``;
89 for (const child of Array.from(h1.childNodes)) {
90 link.appendChild(child);
91 }
92 h1.appendChild(link);
93 };
94
95 globalThis.bjørnTransformEntryHTML = (document, metadata) => {
96 const LMN = Lemon.bind({ document });
97 const links = Object.entries(contentLinks.get(metadata));
98 if (links.length) {
99 document.getElementById("entry.content").appendChild(
100 LMN.footer`${LMN.nav`${[
101 LMN.h2`This post contains links.`,
102 LMN.ul`${
103 links.map(([name, href]) =>
104 LMN.li`${LMN.a.href(href)`${name}`}`
105 )
106 }`,
107 ]}`}`,
108 );
109 }
110 };
111
112 await import(
113 "https://git.ladys.computer/Beorn/blob_plain/0.2.3:/build.js"
114 );
This page took 0.052006 seconds and 3 git commands to generate.