From 9127d1c420420b3c80414d8222840e96e4dd3f18 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 26 Jan 2025 15:23:17 +0900 Subject: feat(blog/nuldoc): add `target=_blank` and `rel=noreferrer` to external links --- vhosts/blog/nuldoc-src/ndoc/to_html.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'vhosts/blog/nuldoc-src/ndoc/to_html.ts') diff --git a/vhosts/blog/nuldoc-src/ndoc/to_html.ts b/vhosts/blog/nuldoc-src/ndoc/to_html.ts index ce55dbf9..bf556655 100644 --- a/vhosts/blog/nuldoc-src/ndoc/to_html.ts +++ b/vhosts/blog/nuldoc-src/ndoc/to_html.ts @@ -21,6 +21,7 @@ export default function toHtml(doc: Document): Document { transformSectionTitleElement(doc); transformCodeBlockElement(doc); transformNoteElement(doc); + addAttributesToExternalLinkElement(doc); setDefaultLangAttribute(doc); traverseFootnotes(doc); highlightPrograms(doc); @@ -211,6 +212,22 @@ function transformNoteElement(doc: Document) { }); } +function addAttributesToExternalLinkElement(doc: Document) { + forEachChildRecursively(doc.root, (n) => { + if (n.kind !== "element" || n.name !== "a") { + return; + } + + const href = n.attributes.get("href") ?? ""; + if (!href.startsWith("http")) { + return; + } + n.attributes + .set("target", "_blank") + .set("rel", "noreferrer"); + }); +} + function setDefaultLangAttribute(_doc: Document) { // TODO // if (!e.attributes.has("lang")) { -- cgit v1.2.3-70-g09d2