summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-01-15 09:47:18 +0900
committernsfisis <nsfisis@gmail.com>2025-01-15 09:47:18 +0900
commit0fa75a6237a58d22b63dfa1c6ff1742c69a3f322 (patch)
tree806a328a1b27441897f8a000a913417fd0f4c851 /vhosts/blog/nuldoc-src
parentd46df528ef37a4d85c7700ae1b2d683eecd0b37c (diff)
downloadnsfisis.dev-0fa75a6237a58d22b63dfa1c6ff1742c69a3f322.tar.gz
nsfisis.dev-0fa75a6237a58d22b63dfa1c6ff1742c69a3f322.tar.zst
nsfisis.dev-0fa75a6237a58d22b63dfa1c6ff1742c69a3f322.zip
feat(blog/nuldoc): implement autolink feature
Diffstat (limited to 'vhosts/blog/nuldoc-src')
-rw-r--r--vhosts/blog/nuldoc-src/ndoc/to_html.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/vhosts/blog/nuldoc-src/ndoc/to_html.ts b/vhosts/blog/nuldoc-src/ndoc/to_html.ts
index 2ca11a93..ce55dbf9 100644
--- a/vhosts/blog/nuldoc-src/ndoc/to_html.ts
+++ b/vhosts/blog/nuldoc-src/ndoc/to_html.ts
@@ -15,6 +15,7 @@ import {
export default function toHtml(doc: Document): Document {
removeUnnecessaryTextNode(doc);
+ transformLinkLikeToAnchorElement(doc);
transformSectionIdAttribute(doc);
setSectionTitleAnchor(doc);
transformSectionTitleElement(doc);
@@ -55,6 +56,44 @@ function removeUnnecessaryTextNode(doc: Document) {
});
}
+function transformLinkLikeToAnchorElement(doc: Document) {
+ forEachChildRecursively(doc.root, (n) => {
+ if (
+ n.kind !== "element" || n.name === "a" || n.name === "code" ||
+ n.name === "codeblock"
+ ) {
+ return;
+ }
+
+ const newChildren: Node[] = [];
+ for (const child of n.children) {
+ if (child.kind !== "text") {
+ newChildren.push(child);
+ continue;
+ }
+ let restContent = child.content;
+ while (restContent !== "") {
+ const match = /^(.*?)(https?:\/\/[^ \n]+)(.*)$/s.exec(restContent);
+ if (!match) {
+ newChildren.push({ kind: "text", content: restContent, raw: false });
+ restContent = "";
+ break;
+ }
+ const [_, prefix, url, suffix] = match;
+ newChildren.push({ kind: "text", content: prefix, raw: false });
+ newChildren.push({
+ kind: "element",
+ name: "a",
+ attributes: new Map([["href", url]]),
+ children: [{ kind: "text", content: url, raw: false }],
+ });
+ restContent = suffix;
+ }
+ }
+ n.children = newChildren;
+ });
+}
+
function transformSectionIdAttribute(doc: Document) {
forEachChildRecursively(doc.root, (n) => {
if (n.kind !== "element" || n.name !== "section") {