Skip to content

Commit 4b81613

Browse files
committed
Normalize local editor hrefs so mixed-case schemes survive sanitizing
1 parent cd8817c commit 4b81613

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

237+
it("keeps mixed-case local editor file links by lowercasing the scheme before sanitizing", () => {
238+
const processor = unified()
239+
.use(remarkParse)
240+
.use(CHAT_MARKDOWN_REMARK_PLUGINS)
241+
.use(remarkRehype, { allowDangerousHtml: true })
242+
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
243+
const tree = processor.runSync(
244+
processor.parse(
245+
[
246+
"[upper](VSCODE://file/Users/me/repo/file.md:3:7)",
247+
"[mixed](Cursor://file/Users/me/repo/file.md:3)",
248+
"[other](FOO://file/Users/me/repo/file.md)",
249+
].join(" "),
250+
),
251+
);
252+
type Node = { tagName?: string; properties?: { href?: string }; children?: Node[] };
253+
const hrefs = ((tree.children[0] as Node).children ?? [])
254+
.filter((node) => node.tagName === "a")
255+
.map((node) => node.properties?.href);
256+
expect(hrefs).toEqual([
257+
"vscode://file/Users/me/repo/file.md:3:7",
258+
"cursor://file/Users/me/repo/file.md:3",
259+
undefined,
260+
]);
261+
});
262+
237263
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
238264
const processor = unified()
239265
.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, type Transformer, unified } from "unified";
@@ -165,6 +165,26 @@ function rehypePreserveImageSourceMeta() {
165165

166166
const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"];
167167

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+
168188
const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
169189
...defaultSchema,
170190
attributes: {
@@ -304,6 +324,7 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
304324
rehypePreserveBareAnchorPlaceholders,
305325
rehypeRawWhenPresent,
306326
rehypePreserveImageSourceMeta,
327+
rehypeNormalizeLocalEditorLinks,
307328
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
308329
];
309330

0 commit comments

Comments
 (0)