Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apps/web/src/components/ChatMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ const highlightedCodeCache = new LRUCache<string>(
MAX_HIGHLIGHT_CACHE_MEMORY_BYTES,
);

const WINDOWS_DRIVE_LINK_PROTOCOLS = Array.from(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
);

function findTaskListMarkerOffset(markdown: string, listItemStart: number): number | null {
const firstLineEnd = markdown.indexOf("\n", listItemStart);
const firstLine = markdown.slice(
Expand All @@ -147,7 +151,10 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
},
protocols: {
...defaultSchema.protocols,
href: [...(defaultSchema.protocols?.href ?? []), "file"],
// rehype-sanitize runs before react-markdown's urlTransform. A Windows
// drive letter looks like a protocol (`C:`), so allow drive letters here;
// urlTransform only preserves values with a drive-path shape afterward.
href: [...(defaultSchema.protocols?.href ?? []), "file", ...WINDOWS_DRIVE_LINK_PROTOCOLS],
},
} satisfies Parameters<typeof rehypeSanitize>[0];

Expand Down
26 changes: 24 additions & 2 deletions apps/web/src/markdown-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,39 @@ describe("rewriteMarkdownFileUriHref", () => {
});

it("normalizes file uri hrefs for windows drive paths", () => {
const canonicalHref = "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69";
expect(
rewriteMarkdownFileUriHref(
"file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
),
).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
).toBe(canonicalHref);
expect(rewriteMarkdownFileUriHref(canonicalHref)).toBe(canonicalHref);
});

it("unwraps angle-bracketed file uri hrefs", () => {
expect(
rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
).toBe("/D:/Programme/t3code/apps/web/src/markdown-links.ts");
});

it("canonicalizes windows drive hrefs for the custom markdown file renderer", () => {
expect(rewriteMarkdownFileUriHref("C:/Users/mike/project/src/main.ts:42")).toBe(
"/C:/Users/mike/project/src/main.ts:42",
);
expect(rewriteMarkdownFileUriHref("C:\\Users\\mike\\project\\src\\main.ts:42")).toBe(
"/C:/Users/mike/project/src/main.ts:42",
);
expect(rewriteMarkdownFileUriHref("C:%5CUsers%5Cmike%5Cproject%5Csrc%5Cmain.ts:42")).toBe(
"/C:/Users/mike/project/src/main.ts:42",
);
expect(rewriteMarkdownFileUriHref("C://Users/mike/project/src/main.ts:42")).toBe(
"/C:/Users/mike/project/src/main.ts:42",
);
});

it("ignores non-file hrefs", () => {
expect(rewriteMarkdownFileUriHref("https://example.com/docs")).toBeNull();
expect(rewriteMarkdownFileUriHref("x:command")).toBeNull();
});
});

Expand Down
11 changes: 10 additions & 1 deletion apps/web/src/markdown-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@ function parseFileUrlHref(
export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
if (!href) return null;
const normalizedHref = normalizeMarkdownLinkDestination(href);
// Markdown parsing can encode backslashes or duplicate the drive separator.
const windowsDriveHref = normalizedHref
.replaceAll(/%5c/gi, "/")
.replaceAll(/\\/g, "/")
.replace(/^\/?([A-Za-z]):\/+/, "/$1:/");
// A leading slash keeps the drive letter from being treated as a URL scheme.
if (/^\/[A-Za-z]:\//.test(windowsDriveHref)) return windowsDriveHref;

Comment thread
cursor[bot] marked this conversation as resolved.
const target = parseFileUrlHref(normalizedHref, { decodePath: false });
if (!target) return null;
return `${target.path}${target.hash}`;
const path = WINDOWS_DRIVE_PATH_PATTERN.test(target.path) ? `/${target.path}` : target.path;
return `${path}${target.hash}`;
}

function looksLikePosixFilesystemPath(path: string): boolean {
Expand Down
Loading