Skip to content

Commit c27ad73

Browse files
committed
Normalize local editor hrefs so mixed-case schemes survive sanitizing
1 parent 35133b8 commit c27ad73

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎packages/shared/src/markdownPipeline.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ it("keeps local editor file links but not other schemes through the shared Markd
121121
]);
122122
});
123123

124+
it("keeps mixed-case local editor file links by lowercasing the scheme before sanitizing", () => {
125+
const processor = unified()
126+
.use(remarkParse)
127+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
128+
.use(remarkRehype, { allowDangerousHtml: true })
129+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
130+
const tree = processor.runSync(
131+
processor.parse(
132+
[
133+
"[upper](VSCODE://file/Users/me/repo/file.md:3:7)",
134+
"[mixed](Cursor://file/Users/me/repo/file.md:3)",
135+
"[other](FOO://file/Users/me/repo/file.md)",
136+
].join(" "),
137+
),
138+
);
139+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
140+
const hrefs = ((tree.children[0] as Node).children ?? [])
141+
.filter((node) => node.tagName === "a")
142+
.map((node) => node.properties?.href);
143+
expect(hrefs).toEqual([
144+
"vscode://file/Users/me/repo/file.md:3:7",
145+
"cursor://file/Users/me/repo/file.md:3",
146+
undefined,
147+
]);
148+
});
149+
124150
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
125151
const processor = unified()
126152
.use(remarkParse)

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts";
1+
import { isLocalEditorFileUrl, LOCAL_FILE_LINK_SCHEMES } from "@t3tools/contracts";
22
import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
33
import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";
44
import { type PluggableList, type Processor, unified } from "unified";
@@ -130,6 +130,26 @@ function rehypePreserveImageSourceMeta() {
130130
};
131131
}
132132

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+
133153
const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
134154
...defaultSchema,
135155
attributes: {
@@ -263,6 +283,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
263283
rehypePreserveBareAnchorPlaceholders,
264284
rehypeRawWhenPresent,
265285
rehypePreserveImageSourceMeta,
286+
rehypeNormalizeLocalEditorLinks,
266287
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
267288
];
268289

0 commit comments

Comments
 (0)