Skip to content

Commit 6717343

Browse files
fix(web): make Windows file links clickable
1 parent 52e5a75 commit 6717343

6 files changed

Lines changed: 93 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
7676
import {
7777
normalizeMarkdownLinkDestination,
78+
remarkRewriteWindowsFileLinks,
7879
resolveInlineCodeFileLinkMeta,
7980
resolveMarkdownFileLinkMeta,
8081
rewriteMarkdownFileUriHref,
@@ -162,6 +163,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
162163

163164
const CHAT_MARKDOWN_REMARK_PLUGINS = [
164165
remarkGfm,
166+
remarkRewriteWindowsFileLinks,
165167
remarkGithubAlerts,
166168
remarkNormalizeListItemIndentation,
167169
remarkPreserveCodeMeta,
@@ -170,6 +172,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS = [
170172

171173
const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
172174
remarkGfm,
175+
remarkRewriteWindowsFileLinks,
173176
remarkGithubAlerts,
174177
remarkNormalizeListItemIndentation,
175178
remarkBreaks,

‎apps/web/src/filePathDisplay.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ describe("formatWorkspaceRelativePath", () => {
2121
).toBe("t3code/apps/web/src/session-logic.ts:501");
2222
});
2323

24+
it("keeps absolute windows paths outside the workspace unchanged", () => {
25+
expect(
26+
formatWorkspaceRelativePath(
27+
"C:/Users/mike/dev-stuff/other-project/src/main.ts",
28+
"C:/Users/mike/dev-stuff/t3code",
29+
),
30+
).toBe("C:/Users/mike/dev-stuff/other-project/src/main.ts");
31+
});
32+
2433
it("keeps paths already rooted at the workspace label stable", () => {
2534
expect(
2635
formatWorkspaceRelativePath(

‎apps/web/src/filePathDisplay.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function stripRelativePrefixes(path: string): string {
2121
return path.replace(/^\.\/+/, "").replace(/^\/+/, "");
2222
}
2323

24+
function isAbsolutePath(path: string): boolean {
25+
return path.startsWith("/") || /^[A-Za-z]:\//.test(path);
26+
}
27+
2428
export function formatWorkspaceRelativePath(
2529
pathWithPosition: string,
2630
workspaceRoot: string | undefined,
@@ -44,7 +48,7 @@ export function formatWorkspaceRelativePath(
4448
} else if (pathForCompare.startsWith(workspaceWithSeparator)) {
4549
const relativeSuffix = normalizedPath.slice(normalizedWorkspaceRoot.length + 1);
4650
displayPath = `${workspaceLabel}/${relativeSuffix}`;
47-
} else if (!normalizedPath.startsWith("/")) {
51+
} else if (!isAbsolutePath(normalizedPath)) {
4852
const relativePath = stripRelativePrefixes(normalizedPath);
4953
displayPath = pathForCompare.startsWith(workspaceLabelWithSeparator)
5054
? normalizedPath
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { renderToStaticMarkup } from "react-dom/server";
2+
import ReactMarkdown, { defaultUrlTransform } from "react-markdown";
3+
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
4+
import { describe, expect, it } from "vite-plus/test";
5+
6+
import { remarkRewriteWindowsFileLinks, rewriteMarkdownFileUriHref } from "./markdown-links";
7+
8+
const sanitizeSchema = {
9+
...defaultSchema,
10+
protocols: {
11+
...defaultSchema.protocols,
12+
href: [...(defaultSchema.protocols?.href ?? []), "file"],
13+
},
14+
} satisfies Parameters<typeof rehypeSanitize>[0];
15+
16+
function renderMarkdown(markdown: string): string {
17+
return renderToStaticMarkup(
18+
<ReactMarkdown
19+
remarkPlugins={[remarkRewriteWindowsFileLinks]}
20+
rehypePlugins={[[rehypeSanitize, sanitizeSchema]]}
21+
urlTransform={(href) => rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href)}
22+
>
23+
{markdown}
24+
</ReactMarkdown>,
25+
);
26+
}
27+
28+
describe("Windows markdown file link rendering", () => {
29+
it("preserves a drive-path href through HTML sanitization", () => {
30+
const path = "C:/Users/mike/dev-stuff/t3code/apps/web/src/markdown-links.ts";
31+
32+
expect(renderMarkdown(`[markdown-links.ts](${path}) is open above.`)).toContain(
33+
`<a href="${path}">markdown-links.ts</a>`,
34+
);
35+
});
36+
37+
it("still removes unsafe schemes", () => {
38+
expect(renderMarkdown("[unsafe](javascript:alert(1))")).not.toContain("href=");
39+
});
40+
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ describe("rewriteMarkdownFileUriHref", () => {
3636
});
3737

3838
describe("resolveMarkdownFileLinkTarget", () => {
39+
it("resolves windows drive paths", () => {
40+
expect(
41+
resolveMarkdownFileLinkTarget(
42+
"C:/Users/mike/dev-stuff/t3code/apps/web/src/markdown-links.ts",
43+
),
44+
).toBe("C:/Users/mike/dev-stuff/t3code/apps/web/src/markdown-links.ts");
45+
});
46+
3947
it("resolves absolute posix file paths", () => {
4048
expect(resolveMarkdownFileLinkTarget("/Users/julius/project/AGENTS.md")).toBe(
4149
"/Users/julius/project/AGENTS.md",

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,34 @@ export function rewriteMarkdownFileUriHref(href: string | undefined): string | n
108108
return `${target.path}${target.hash}`;
109109
}
110110

111+
interface MarkdownLinkNode {
112+
type?: string;
113+
url?: unknown;
114+
children?: MarkdownLinkNode[];
115+
}
116+
117+
/**
118+
* rehype-sanitize reads the drive letter in `C:/path` as a URL scheme and
119+
* removes the href before react-markdown's URL transform can normalize it.
120+
* Convert only Windows drive-path destinations into the already-allowed file
121+
* URI form while the document is still mdast.
122+
*/
123+
export function remarkRewriteWindowsFileLinks() {
124+
return (tree: MarkdownLinkNode) => {
125+
const visit = (node: MarkdownLinkNode) => {
126+
if ((node.type === "link" || node.type === "definition") && typeof node.url === "string") {
127+
const normalizedUrl = normalizeMarkdownLinkDestination(node.url);
128+
if (WINDOWS_DRIVE_PATH_PATTERN.test(normalizedUrl)) {
129+
node.url = `file:///${normalizedUrl.replaceAll("\\", "/")}`;
130+
}
131+
}
132+
node.children?.forEach(visit);
133+
};
134+
135+
visit(tree);
136+
};
137+
}
138+
111139
function looksLikePosixFilesystemPath(path: string): boolean {
112140
if (!path.startsWith("/")) return false;
113141
if (POSIX_FILE_ROOT_PREFIXES.some((prefix) => path.startsWith(prefix))) return true;

0 commit comments

Comments
 (0)