|
1 | | -import { LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts"; |
| 1 | +import { isLocalEditorFileUrl, LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts"; |
2 | 2 | import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast"; |
3 | 3 | import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts"; |
4 | 4 | import { type PluggableList, unified } from "unified"; |
@@ -129,6 +129,26 @@ function rehypePreserveImageSourceMeta() { |
129 | 129 | }; |
130 | 130 | } |
131 | 131 |
|
| 132 | +// The sanitizer compares schemes case-sensitively, so lowercase accepted editor links first. |
| 133 | +function rehypeNormalizeLocalEditorLinks() { |
| 134 | + return (tree: MarkdownImageHastNode) => { |
| 135 | + const visit = (node: MarkdownImageHastNode) => { |
| 136 | + const href = node.properties?.href; |
| 137 | + if (node.type === "element" && node.tagName === "a" && typeof href === "string") { |
| 138 | + try { |
| 139 | + const url = new URL(href); |
| 140 | + if (isLocalEditorFileUrl(url)) node.properties = { ...node.properties, href: url.href }; |
| 141 | + } catch { |
| 142 | + // Not an absolute URL; the sanitizer decides. |
| 143 | + } |
| 144 | + } |
| 145 | + node.children?.forEach(visit); |
| 146 | + }; |
| 147 | + |
| 148 | + visit(tree); |
| 149 | + }; |
| 150 | +} |
| 151 | + |
132 | 152 | const CHAT_MARKDOWN_SANITIZE_SCHEMA = { |
133 | 153 | ...defaultSchema, |
134 | 154 | attributes: { |
@@ -192,6 +212,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [ |
192 | 212 | rehypePreserveBareAnchorPlaceholders, |
193 | 213 | rehypeRaw, |
194 | 214 | rehypePreserveImageSourceMeta, |
| 215 | + rehypeNormalizeLocalEditorLinks, |
195 | 216 | [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA], |
196 | 217 | ]; |
197 | 218 |
|
|
0 commit comments