|
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, type Transformer, unified } from "unified"; |
@@ -165,6 +165,26 @@ function rehypePreserveImageSourceMeta() { |
165 | 165 |
|
166 | 166 | const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"]; |
167 | 167 |
|
| 168 | +// The sanitizer compares schemes case-sensitively, so lowercase accepted editor links first. |
| 169 | +function rehypeNormalizeLocalEditorLinks() { |
| 170 | + return (tree: MarkdownImageHastNode) => { |
| 171 | + const visit = (node: MarkdownImageHastNode) => { |
| 172 | + const href = node.properties?.href; |
| 173 | + if (node.type === "element" && node.tagName === "a" && typeof href === "string") { |
| 174 | + try { |
| 175 | + const url = new URL(href); |
| 176 | + if (isLocalEditorFileUrl(url)) node.properties = { ...node.properties, href: url.href }; |
| 177 | + } catch { |
| 178 | + // Not an absolute URL; the sanitizer decides. |
| 179 | + } |
| 180 | + } |
| 181 | + node.children?.forEach(visit); |
| 182 | + }; |
| 183 | + |
| 184 | + visit(tree); |
| 185 | + }; |
| 186 | +} |
| 187 | + |
168 | 188 | const CHAT_MARKDOWN_SANITIZE_SCHEMA = { |
169 | 189 | ...defaultSchema, |
170 | 190 | attributes: { |
@@ -304,6 +324,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [ |
304 | 324 | rehypePreserveBareAnchorPlaceholders, |
305 | 325 | rehypeRawWhenPresent, |
306 | 326 | rehypePreserveImageSourceMeta, |
| 327 | + rehypeNormalizeLocalEditorLinks, |
307 | 328 | [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA], |
308 | 329 | ]; |
309 | 330 |
|
|
0 commit comments