summaryrefslogtreecommitdiff
path: root/llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts
diff options
context:
space:
mode:
Diffstat (limited to 'llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts')
-rw-r--r--llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts b/llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts
new file mode 100644
index 0000000..b5fbcbd
--- /dev/null
+++ b/llama.cpp/tools/server/webui/src/lib/markdown/enhance-links.ts
@@ -0,0 +1,33 @@
+/**
+ * Rehype plugin to enhance links with security attributes.
+ *
+ * Adds target="_blank" and rel="noopener noreferrer" to all anchor elements,
+ * ensuring external links open in new tabs safely.
+ */
+
+import type { Plugin } from 'unified';
+import type { Root, Element } from 'hast';
+import { visit } from 'unist-util-visit';
+
+/**
+ * Rehype plugin that adds security attributes to all links.
+ * This plugin ensures external links open in new tabs safely by adding:
+ * - target="_blank"
+ * - rel="noopener noreferrer"
+ */
+export const rehypeEnhanceLinks: Plugin<[], Root> = () => {
+ return (tree: Root) => {
+ visit(tree, 'element', (node: Element) => {
+ if (node.tagName !== 'a') return;
+
+ const props = node.properties ?? {};
+
+ // Only modify if href exists
+ if (!props.href) return;
+
+ props.target = '_blank';
+ props.rel = 'noopener noreferrer';
+ node.properties = props;
+ });
+ };
+};