Skip to content

Commit 2e03244

Browse files
fix(web): normalize encoded Windows link keys
1 parent 71371ee commit 2e03244

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import {
7474
} from "../markdown-clipboard";
7575
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
7676
import {
77-
normalizeMarkdownLinkDestination,
77+
normalizeMarkdownFileLinkHrefKey,
7878
remarkRewriteWindowsFileLinks,
7979
resolveInlineCodeFileLinkMeta,
8080
resolveMarkdownFileLinkMeta,
@@ -887,11 +887,6 @@ function extractMarkdownLinkHrefs(text: string): string[] {
887887
return hrefs;
888888
}
889889

890-
function normalizeMarkdownLinkHrefKey(href: string): string {
891-
const normalizedHref = normalizeMarkdownLinkDestination(href);
892-
return rewriteMarkdownFileUriHref(normalizedHref) ?? normalizedHref;
893-
}
894-
895890
const MARKDOWN_LINK_FAVICON_CLASS_NAME = "block size-full shrink-0 select-none";
896891

897892
/** Hosts whose favicon request already failed this session — skip straight to the globe. */
@@ -1352,7 +1347,7 @@ function ChatMarkdown({
13521347
NonNullable<ReturnType<typeof resolveMarkdownFileLinkMeta>>
13531348
>();
13541349
for (const href of extractMarkdownLinkHrefs(text)) {
1355-
const normalizedHref = normalizeMarkdownLinkHrefKey(href);
1350+
const normalizedHref = normalizeMarkdownFileLinkHrefKey(href);
13561351
if (metaByHref.has(normalizedHref)) continue;
13571352
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd);
13581353
if (meta) {
@@ -1546,7 +1541,7 @@ function ChatMarkdown({
15461541
);
15471542
},
15481543
a({ node, href, children, ...props }) {
1549-
const normalizedHref = href ? normalizeMarkdownLinkHrefKey(href) : "";
1544+
const normalizedHref = href ? normalizeMarkdownFileLinkHrefKey(href) : "";
15501545
const fileLinkMeta = normalizedHref ? markdownFileLinkMetaByHref.get(normalizedHref) : null;
15511546
if (!fileLinkMeta) {
15521547
const faviconHost = resolveExternalWebLinkHost(href);

‎apps/web/src/markdown-links-rendering.test.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ describe("Windows markdown file link rendering", () => {
4242
);
4343
});
4444

45+
it("percent-encodes a unicode drive-path href through HTML sanitization", () => {
46+
const path = "C:/Users/mike/dev-stuff/文档/apps/web/src/markdown-links.ts";
47+
48+
expect(renderMarkdown(`[markdown-links.ts](${path}) is open above.`)).toContain(
49+
'<a href="C:/Users/mike/dev-stuff/%E6%96%87%E6%A1%A3/apps/web/src/markdown-links.ts">markdown-links.ts</a>',
50+
);
51+
});
52+
4553
it("still removes unsafe schemes", () => {
4654
expect(renderMarkdown("[unsafe](javascript:alert(1))")).not.toContain("href=");
4755
});

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
normalizeMarkdownFileLinkHrefKey,
45
normalizeMarkdownLinkDestination,
56
resolveInlineCodeFileLinkMeta,
67
resolveMarkdownFileLinkMeta,
@@ -18,6 +19,27 @@ describe("normalizeMarkdownLinkDestination", () => {
1819
});
1920
});
2021

22+
describe("normalizeMarkdownFileLinkHrefKey", () => {
23+
it("uses the same key for raw and encoded unicode drive paths", () => {
24+
const encodedPath = "C:/Users/mike/dev-stuff/%E6%96%87%E6%A1%A3/apps/web/src/markdown-links.ts";
25+
26+
expect(
27+
normalizeMarkdownFileLinkHrefKey(
28+
"C:\\Users\\mike\\dev-stuff\\文档\\apps\\web\\src\\markdown-links.ts",
29+
),
30+
).toBe(encodedPath);
31+
expect(normalizeMarkdownFileLinkHrefKey(encodedPath)).toBe(encodedPath);
32+
});
33+
34+
it("preserves existing encoded octets", () => {
35+
expect(
36+
normalizeMarkdownFileLinkHrefKey(
37+
"C:/Users/mike/dev-stuff/t3code/apps/web/src/file%2520name.ts",
38+
),
39+
).toBe("C:/Users/mike/dev-stuff/t3code/apps/web/src/file%2520name.ts");
40+
});
41+
});
42+
2143
describe("rewriteMarkdownFileUriHref", () => {
2244
it("rewrites file uri hrefs into direct path hrefs", () => {
2345
expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ export function rewriteMarkdownFileUriHref(href: string | undefined): string | n
111111
return `${target.path}${target.hash}`;
112112
}
113113

114+
export function normalizeMarkdownFileLinkHrefKey(href: string): string {
115+
const normalizedHref = normalizeMarkdownLinkDestination(href);
116+
const rewrittenHref = rewriteMarkdownFileUriHref(normalizedHref) ?? normalizedHref;
117+
if (!WINDOWS_DRIVE_PATH_PATTERN.test(rewrittenHref)) return rewrittenHref;
118+
119+
const target = parseFileUrlHref(`file:///${rewrittenHref}`, { decodePath: false });
120+
return target ? `${target.path}${target.hash}` : rewrittenHref;
121+
}
122+
114123
interface MarkdownLinkNode {
115124
type?: string;
116125
url?: unknown;

0 commit comments

Comments
 (0)