Skip to content

Commit 76625da

Browse files
author
thiscallnet
committed
fix: open Windows file links in chat
1 parent 6f04a5c commit 76625da

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ const highlightedCodeCache = new LRUCache<string>(
128128
MAX_HIGHLIGHT_CACHE_MEMORY_BYTES,
129129
);
130130

131+
const WINDOWS_DRIVE_LINK_PROTOCOLS = Array.from(
132+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
133+
);
134+
131135
function findTaskListMarkerOffset(markdown: string, listItemStart: number): number | null {
132136
const firstLineEnd = markdown.indexOf("\n", listItemStart);
133137
const firstLine = markdown.slice(
@@ -147,7 +151,10 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
147151
},
148152
protocols: {
149153
...defaultSchema.protocols,
150-
href: [...(defaultSchema.protocols?.href ?? []), "file"],
154+
// rehype-sanitize runs before react-markdown's urlTransform. A Windows
155+
// drive letter looks like a protocol (`C:`), so allow drive letters here;
156+
// urlTransform only preserves values with a drive-path shape afterward.
157+
href: [...(defaultSchema.protocols?.href ?? []), "file", ...WINDOWS_DRIVE_LINK_PROTOCOLS],
151158
},
152159
} satisfies Parameters<typeof rehypeSanitize>[0];
153160

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,39 @@ describe("rewriteMarkdownFileUriHref", () => {
2121
});
2222

2323
it("normalizes file uri hrefs for windows drive paths", () => {
24+
const canonicalHref = "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69";
2425
expect(
2526
rewriteMarkdownFileUriHref(
2627
"file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
2728
),
28-
).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
29+
).toBe(canonicalHref);
30+
expect(rewriteMarkdownFileUriHref(canonicalHref)).toBe(canonicalHref);
2931
});
3032

3133
it("unwraps angle-bracketed file uri hrefs", () => {
3234
expect(
3335
rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
34-
).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
36+
).toBe("/D:/Programme/t3code/apps/web/src/markdown-links.ts");
37+
});
38+
39+
it("canonicalizes windows drive hrefs for the custom markdown file renderer", () => {
40+
expect(rewriteMarkdownFileUriHref("C:/Users/mike/project/src/main.ts:42")).toBe(
41+
"/C:/Users/mike/project/src/main.ts:42",
42+
);
43+
expect(rewriteMarkdownFileUriHref("C:\\Users\\mike\\project\\src\\main.ts:42")).toBe(
44+
"/C:/Users/mike/project/src/main.ts:42",
45+
);
46+
expect(rewriteMarkdownFileUriHref("C:%5CUsers%5Cmike%5Cproject%5Csrc%5Cmain.ts:42")).toBe(
47+
"/C:/Users/mike/project/src/main.ts:42",
48+
);
49+
expect(rewriteMarkdownFileUriHref("C://Users/mike/project/src/main.ts:42")).toBe(
50+
"/C:/Users/mike/project/src/main.ts:42",
51+
);
52+
});
53+
54+
it("ignores non-file hrefs", () => {
55+
expect(rewriteMarkdownFileUriHref("https://example.com/docs")).toBeNull();
56+
expect(rewriteMarkdownFileUriHref("x:command")).toBeNull();
3557
});
3658
});
3759

‎apps/web/src/markdown-links.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,18 @@ function parseFileUrlHref(
103103
export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
104104
if (!href) return null;
105105
const normalizedHref = normalizeMarkdownLinkDestination(href);
106+
// Markdown parsing can encode backslashes or duplicate the drive separator.
107+
const windowsDriveHref = normalizedHref
108+
.replaceAll(/%5c/gi, "/")
109+
.replaceAll(/\\/g, "/")
110+
.replace(/^\/?([A-Za-z]):\/+/, "/$1:/");
111+
// A leading slash keeps the drive letter from being treated as a URL scheme.
112+
if (/^\/[A-Za-z]:\//.test(windowsDriveHref)) return windowsDriveHref;
113+
106114
const target = parseFileUrlHref(normalizedHref, { decodePath: false });
107115
if (!target) return null;
108-
return `${target.path}${target.hash}`;
116+
const path = WINDOWS_DRIVE_PATH_PATTERN.test(target.path) ? `/${target.path}` : target.path;
117+
return `${path}${target.hash}`;
109118
}
110119

111120
function looksLikePosixFilesystemPath(path: string): boolean {

0 commit comments

Comments
 (0)