summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/ndoc/to_html.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-01-26 15:23:17 +0900
committernsfisis <nsfisis@gmail.com>2025-01-26 15:23:17 +0900
commit9127d1c420420b3c80414d8222840e96e4dd3f18 (patch)
treea804e9f7b3d22c762b1c8abc1be5ba0e0f5b93fa /vhosts/blog/nuldoc-src/ndoc/to_html.ts
parentad87bf61edab31921a3ce216225a7a33e2112746 (diff)
downloadnsfisis.dev-9127d1c420420b3c80414d8222840e96e4dd3f18.tar.gz
nsfisis.dev-9127d1c420420b3c80414d8222840e96e4dd3f18.tar.zst
nsfisis.dev-9127d1c420420b3c80414d8222840e96e4dd3f18.zip
feat(blog/nuldoc): add `target=_blank` and `rel=noreferrer` to external links
Diffstat (limited to 'vhosts/blog/nuldoc-src/ndoc/to_html.ts')
-rw-r--r--vhosts/blog/nuldoc-src/ndoc/to_html.ts17
1 files changed, 17 insertions, 0 deletions
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")) {