|
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, type Processor, unified } from "unified"; |
@@ -130,6 +130,26 @@ function rehypePreserveImageSourceMeta() { |
130 | 130 | }; |
131 | 131 | } |
132 | 132 |
|
| 133 | +// The sanitizer compares schemes case-sensitively, so lowercase accepted editor links first. |
| 134 | +function rehypeNormalizeLocalEditorLinks() { |
| 135 | + return (tree: MarkdownImageHastNode) => { |
| 136 | + const visit = (node: MarkdownImageHastNode) => { |
| 137 | + const href = node.properties?.href; |
| 138 | + if (node.type === "element" && node.tagName === "a" && typeof href === "string") { |
| 139 | + try { |
| 140 | + const url = new URL(href); |
| 141 | + if (isLocalEditorFileUrl(url)) node.properties = { ...node.properties, href: url.href }; |
| 142 | + } catch { |
| 143 | + // Not an absolute URL; the sanitizer decides. |
| 144 | + } |
| 145 | + } |
| 146 | + node.children?.forEach(visit); |
| 147 | + }; |
| 148 | + |
| 149 | + visit(tree); |
| 150 | + }; |
| 151 | +} |
| 152 | + |
133 | 153 | const CHAT_MARKDOWN_SANITIZE_SCHEMA = { |
134 | 154 | ...defaultSchema, |
135 | 155 | attributes: { |
@@ -263,6 +283,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [ |
263 | 283 | rehypePreserveBareAnchorPlaceholders, |
264 | 284 | rehypeRawWhenPresent, |
265 | 285 | rehypePreserveImageSourceMeta, |
| 286 | + rehypeNormalizeLocalEditorLinks, |
266 | 287 | [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA], |
267 | 288 | ]; |
268 | 289 |
|
|
0 commit comments