Skip to content

Commit 69cc230

Browse files
committed
Normalize local editor hrefs so mixed-case schemes survive sanitizing
1 parent 089d032 commit 69cc230

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
@@ -68,6 +68,32 @@ it("keeps local editor file links but not other schemes through the shared Markd
6868
]);
6969
});
7070

71+
it("keeps mixed-case local editor file links by lowercasing the scheme before sanitizing", () => {
72+
const processor = unified()
73+
.use(remarkParse)
74+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
75+
.use(remarkRehype, { allowDangerousHtml: true })
76+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
77+
const tree = processor.runSync(
78+
processor.parse(
79+
[
80+
"[upper](VSCODE://file/Users/me/repo/file.md:3:7)",
81+
"[mixed](Cursor://file/Users/me/repo/file.md:3)",
82+
"[other](FOO://file/Users/me/repo/file.md)",
83+
].join(" "),
84+
),
85+
);
86+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
87+
const hrefs = ((tree.children[0] as Node).children ?? [])
88+
.filter((node) => node.tagName === "a")
89+
.map((node) => node.properties?.href);
90+
expect(hrefs).toEqual([
91+
"vscode://file/Users/me/repo/file.md:3:7",
92+
"cursor://file/Users/me/repo/file.md:3",
93+
undefined,
94+
]);
95+
});
96+
7197
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
7298
const processor = unified()
7399
.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, unified } from "unified";
@@ -129,6 +129,26 @@ function rehypePreserveImageSourceMeta() {
129129
};
130130
}
131131

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+
132152
const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
133153
...defaultSchema,
134154
attributes: {
@@ -192,6 +212,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
192212
rehypePreserveBareAnchorPlaceholders,
193213
rehypeRaw,
194214
rehypePreserveImageSourceMeta,
215+
rehypeNormalizeLocalEditorLinks,
195216
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
196217
];
197218

0 commit comments

Comments
 (0)