]> Lady’s Gitweb - Blog/blobdiff - build.js
Add links & icons; fix wrapping around code
[Blog] / build.js
index 0260323ec3afd2aa253c5c0a15d500b75594340a..be96bcdb52dc0407f61212ae80a8a35085a4a0e2 100755 (executable)
--- a/build.js
+++ b/build.js
@@ -26,6 +26,8 @@ const processContent = function (content) {
 };
 
 const applyTransforms = function (node) {
+  const document = node.ownerDocument;
+  const LMN = Lemon.bind(document);
   if (
     node.localName == "pre" && node.childNodes.length == 1 &&
     node.firstChild.localName == "code"
@@ -66,6 +68,50 @@ const applyTransforms = function (node) {
   }
   if (node.nodeType == 1) {
     // This is an element; process its children.
+    if (node.localName == "code") {
+      // This is a `<code>` element; wrap it in a `Word-Wrap: Pre` with
+      // any previous/next characters to prevent line‐breaking due to
+      // it being rendered as an inline block.
+      const prev = node.previousSibling;
+      const prevText = prev?.nodeType == 3 ? prev.textContent : "";
+      const prevWrap = prevText.match(/\S*$/u)[0];
+      const next = node.nextSibling;
+      const nextText = next?.nodeType == 3 ? next.textContent : "";
+      const nextWrap = nextText.match(/^\S*/u)[0];
+      if (prevWrap || nextWrap) {
+        const span = LMN.span.style("White-Space: Pre")``;
+        node.parentNode.replaceChild(span, node);
+        if (prevWrap) {
+          prev.parentNode.replaceChild(
+            document.createTextNode(
+              prevText.substring(0, prevText.length - prevWrap.length),
+            ),
+            prev,
+          );
+          span.appendChild(
+            document.createTextNode(
+              prevText.substring(prevText.length - prevWrap.length),
+            ),
+          );
+        }
+        span.appendChild(node);
+        if (nextWrap) {
+          const nexts = [
+            document.createTextNode(
+              nextText.substring(0, nextWrap.length),
+            ),
+            document.createTextNode(
+              nextText.substring(nextWrap.length),
+            ),
+          ];
+          span.appendChild(nexts[0]);
+          next.parentNode.replaceChild(nexts[1], next);
+          for (const nextNode of nexts) {
+            applyTransforms.call(this, nextNode);
+          }
+        }
+      }
+    }
     Array.from(node.childNodes).forEach(applyTransforms.bind(this));
   } else if (node.nodeType == 3) {
     // This is a text node; apply replacements.
@@ -110,5 +156,5 @@ globalThis.bjørnTransformEntryHTML = (document, metadata) => {
 };
 
 await import(
-  "https://git.ladys.computer/Beorn/blob_plain/0.2.3:/build.js"
+  "https://git.ladys.computer/Beorn/blob_plain/0.2.4:/build.js"
 );
This page took 0.131703 seconds and 4 git commands to generate.