Open History to see the last commit
1import { workspaceRelativeFilePath } from "./path.ts";
2import { describe, expect, it } from "vite-plus/test";
3import { serializeComposerFileLink } from "./composerTrigger.ts";
4import {
5 extractInlineCodeSpans,
6 extractMarkdownLinkHrefs,
7 resolveMarkdownFileLink,
8 resolveMarkdownFileLinkTarget,
9} from "./markdownLinks.ts";
10
11import {
12 inlineCodeFilePathCandidate,
13 isMarkdownFileLinkLabel,
14 parseMarkdownFileLink,
15} from "./markdownLinks.ts";
16
17describe("isMarkdownFileLinkLabel", () => {
18 // regression: #15509 descriptive labels are not mistaken for the linked file name
19 it.each([
20 ["validates the input", "/repo/src/example.ts:12", false],
21 ["example.ts:12", "/repo/src/example.ts:12", true],
22 ["example.ts:99", "/repo/src/example.ts:12", false],
23 ["example.ts:12-20", "/repo/src/example.ts:12-20", true],
24 ["example.ts:12-20", "/repo/src/example.ts:12", false],
25 ["src/main.ts#L10-L20", "/repo/src/main.ts#L10-L20", true],
26 ["main.ts#L42C7", "/repo/src/main.ts:42:7", true],
27 ["src/main.ts#L10-L20", "/repo/src/main.ts#L10", false],
28 ["src/main.ts:10#L20", "/repo/src/main.ts:10", false],
29 ["C#/Program.cs#L10", "./C%23/Program.cs#L10", true],
30 ["src/main.ts#L10", "./src/main.ts%23L10", true],
31 ["draft.md ", "/tmp/draft.md%20", true],
32 [" draft.md ", "./%20draft.md%20%20", true],
33 [" draft.md ", "/tmp/draft.md", true],
34 ])("classifies %s for %s", (label, href, expected) => {
35 expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
36 });
37});
38
39describe("inlineCodeFilePathCandidate", () => {
40 // regression: #13909 model ids in inline code do not become file chips
41 it.each([
42 ["src\\main.ts", "src/main.ts"],
43 ["allocator.cc:217", "allocator.cc:217"],
44 ["src/http.ts:42-64", "src/http.ts:42-64"],
45 ["http.ts:42-64", "http.ts:42-64"],
46 ["src/a.ts#section", null],
47 ["C#/a.cs#L10", null],
48 ["src/a.ts#L10-L20-extra", null],
49 [String.raw`C:\My Files\a.ts#section`, null],
50 ["origin/main", null],
51 ["z-ai/glm-5.3", null],
52 ["share/man/ls.1", "share/man/ls.1"],
53 ["docker/Dockerfile", "docker/Dockerfile"],
54 ["src/main.ts/", null],
55 ["./patches/", "./patches/"],
56 // regression: #30757 an agent's shortened path is not a file
57 ["/tmp/claude-1000/...", null],
58 ["/tmp/claude-1000/...:12", null],
59 ["/tmp/.../scratchpad/notes.md", null],
60 ["~/projects/…/notes.md", null],
61 ["src/.../main.ts", null],
62 // regression: #22181 a `:line` suffix does not make an extensionless basename a file
63 ["I/W217:220", null],
64 ["bin/run:12", null],
65 ["docs/README:12", "docs/README:12"],
66 ["./bin/run:12", "./bin/run:12"],
67 ["src/資料.備份:12", "src/資料.備份:12"],
68 ["src/foo.c++:12", "src/foo.c++:12"],
69 // regression: #37740 absolute paths with spaces are files, commands are not
70 ["/Users/me/Path Test/notes with spaces.md", "/Users/me/Path Test/notes with spaces.md"],
71 ["~/Library/Mobile Documents/notes.md", "~/Library/Mobile Documents/notes.md"],
72 [
73 "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md",
74 "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md",
75 ],
76 ["cd /tmp && ls", null],
77 ["/usr/bin/python3 -m venv .venv", null],
78 ["/usr/local/bin/node server.js", null],
79 ["/usr/bin/env python3 foo.py", null],
80 ["/opt/homebrew/bin/python3 manage.py", null],
81 ["/bin/echo hello world.txt", null],
82 ["~/bin/run tests.sh", null],
83 ["/Users/me/notes with spaces.md", "/Users/me/notes with spaces.md"],
84 [
85 "\\\\server\\share\\Project\\Guide Notes.md:10-20",
86 "\\\\server\\share\\Project\\Guide Notes.md:10-20",
87 ],
88 ["\\\\server\\Shared Docs\\Guide.md", "\\\\server\\Shared Docs\\Guide.md"],
89 // regression: #46241 consecutive spaces stay inside one path
90 [String.raw`C:\My Folder\notes.md#L10-L20`, String.raw`C:\My Folder\notes.md#L10-L20`],
91 [String.raw`\\server\Team Share\notes.md`, String.raw`\\server\Team Share\notes.md`],
92 ["/Users/me/Path Test/notes.md", "/Users/me/Path Test/notes.md"],
93 ["/Users/me/tool --fast notes.md", null],
94 ["\\\\server name\\share\\Guide Notes.md", null],
95 ["\\\\server\\Guide Notes.md", null],
96 ["~/Videos/clips/<thread-slug>/shot.png", null],
97 ["clips/<name>.mp4", null],
98 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
99 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
100 });
101
102 it.each(["src/a.ts", String.raw`C:\My Files\a.ts`])(
103 "recognizes inline GitHub line ranges in %s like colon ranges",
104 (path) => {
105 const anchored = `${path}#L10-L20`;
106 expect(inlineCodeFilePathCandidate(anchored)).toBe(anchored);
107 expect(resolveMarkdownFileLink(anchored, "/work")).toEqual(
108 resolveMarkdownFileLink(`${path}:10-20`, "/work"),
109 );
110 expect(resolveMarkdownFileLink(anchored, "/work")).toMatchObject({
111 position: { line: 10, endLine: 20 },
112 });
113 },
114 );
115});
116
117describe("parseMarkdownFileLink", () => {
118 it.each([
119 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
120 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
121 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
122 ["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"],
123 ["src/routes/+page.svelte", "src/routes/+page.svelte"],
124 ["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"],
125 ["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],
126 ["+page.svelte", "+page.svelte"],
127 ["[id].tsx", "[id].tsx"],
128 ["/\\server\\share\\x.ts", "/\\server\\share\\x.ts"],
129 ])("recognizes %s as a file", (href, path) => {
130 expect(parseMarkdownFileLink(href)?.path).toBe(path);
131 });
132
133 it.each([
134 "app/routes/posts.$postId.tsx",
135 "pages/posts/[id].tsx",
136 "docs/My File (draft).md",
137 "@scope/package.json",
138 "$postId.tsx",
139 ])("reads back the composer's link to %s", (path) => {
140 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
141 expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);
142 });
143
144 // regression: #41223 names the parser rejects bare; the composer adds `./` to them
145 it.each([
146 "C#/Program.cs",
147 "docs/R&D/notes.md",
148 "docs/Bob's notes.md",
149 "docs/a,b.ts",
150 "docs/100%.md",
151 "docs/why?.md",
152 "icon@2x.png",
153 ])("resolves the composer's link to %s", (path) => {
154 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
155 expect(resolveMarkdownFileLinkTarget(destination ?? "", "/workspace/project")).toBe(
156 `/workspace/project/${path}`,
157 );
158 });
159
160 it.each([
161 "https://example.com/docs",
162 "/chat/settings#L3",
163 "TODO:12",
164 "patches",
165 "me@example.com",
166 "foo(bar)",
167 "$HOME",
168 "/tmp/claude-1000/...",
169 "/tmp/claude-1000/....:12",
170 "/tmp/claude-1000/%2E%2E%2E",
171 "\\foo.ts",
172 "sandbox://server/share/notes.txt",
173 "sandbox:/%2Fserver/share/notes.txt",
174 "sandbox:/%5Cserver/share/notes.txt",
175 "sandbox:/tmp/bad%00name.txt",
176 ])("does not treat %s as a file", (href) => {
177 expect(parseMarkdownFileLink(href)).toBeNull();
178 });
179
180 it("resolves a sandbox link as an absolute host path, even outside the workspace", () => {
181 const href = "Sandbox:/Users/me/agent-output/example/brief%20%23final.txt#L3C2";
182 expect(parseMarkdownFileLink(href)).toEqual({
183 path: "/Users/me/agent-output/example/brief #final.txt",
184 line: 3,
185 column: 2,
186 });
187 expect(resolveMarkdownFileLinkTarget(href, "/Users/me/repo")).toBe(
188 "/Users/me/agent-output/example/brief #final.txt:3:2",
189 );
190 });
191
192 // regression: #48823 Codex writes Windows sandbox links without a slash before the drive
193 it("resolves a sandbox link to a Windows drive path written without a leading slash", () => {
194 const expected = { path: "C:/t3-repro/sandbox-link-probe.md" };
195 expect(parseMarkdownFileLink("sandbox:/C:/t3-repro/sandbox-link-probe.md")).toEqual(expected);
196 expect(parseMarkdownFileLink("sandbox:C:/t3-repro/sandbox-link-probe.md")).toEqual(expected);
197 });
198
199 it("recognizes a relative folder that ends in a slash", () => {
200 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
201 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
202 });
203
204 // regression: #16096 relative links with non-ASCII names open as files
205 it("recognizes relative paths with non-ASCII names", () => {
206 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
207 "pastexam/線性代數/exam_易志偉.pdf",
208 );
209 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
210 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
211 });
212
213 it("accepts conventional extensionless names with or without a position", () => {
214 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
215 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
216 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
217 });
218
219 it("reads line ranges and opens them at their first line", () => {
220 expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
221 path: "/repo/src/http.ts",
222 line: 42,
223 endLine: 64,
224 });
225 expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
226 path: "/repo/src/http.ts",
227 line: 42,
228 endLine: 64,
229 });
230 });
231
232 it("reads positions from suffixes and line anchors", () => {
233 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
234 path: "/Users/julius/project/src/main.ts",
235 line: 42,
236 column: 7,
237 });
238 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
239 path: "\\\\server\\share\\src\\main.ts",
240 line: 42,
241 column: 7,
242 });
243 });
244});
245
246describe("workspaceRelativeFilePath", () => {
247 // regression: #12449 workspace root links resolve to "."
248 it.each([
249 ["/repo/project", "/repo/project", "."],
250 ["C:/", "c:/", "."],
251 ["/repo/project/a.ts", undefined, null],
252 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
253 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
254 });
255});
256
257describe("extractInlineCodeSpans", () => {
258 it.each([
259 ['[example](https://example.com "`/tmp/other/main.ts`")', ["/tmp/current/main.ts"]],
260 ["[`example`](https://example.com/`/tmp/other/main.ts`)", ["example", "/tmp/current/main.ts"]],
261 ])("ignores backticks in accepted link tails: %s", (link, spans) => {
262 expect(extractInlineCodeSpans(`${link} and \`/tmp/current/main.ts\``)).toEqual(spans);
263 });
264
265 it.each([
266 ["``./a/shared.ts`` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
267 ["``a ` b`` and `./b/shared.ts`", ["a ` b", "./b/shared.ts"]],
268 ["``unclosed and `./b/shared.ts`", ["./b/shared.ts"]],
269 ["\\``./a/shared.ts` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
270 ["`a\\` and `./b/shared.ts`", ["a\\", "./b/shared.ts"]],
271 ["`unclosed\n`./b/shared.ts`", ["./b/shared.ts"]],
272 ])("matches complete backtick delimiter runs in %s", (markdown, spans) => {
273 expect(extractInlineCodeSpans(markdown)).toEqual(spans);
274 });
275
276 it.each([
277 [
278 "Use `` ``` `` to open a fence, then edit `./a/shared.ts` and `./b/shared.ts`",
279 ["```", "./a/shared.ts", "./b/shared.ts"],
280 ],
281 ["```x``` then `./a/shared.ts`", ["x", "./a/shared.ts"]],
282 ["~~~\n`./a/shared.ts`\n~~~\n`./b/shared.ts`", ["./b/shared.ts"]],
283 ["````md\n```ts\n`./a/shared.ts`\n```\n````\n`./b/shared.ts`", ["./b/shared.ts"]],
284 ["```\n ```\n`./a/shared.ts`\n```\n`./b/shared.ts`", ["./b/shared.ts"]],
285 [
286 "```diff\n- ```\n```\nEdit `./a/shared.ts` and `./b/shared.ts`",
287 ["./a/shared.ts", "./b/shared.ts"],
288 ],
289 ["> ```\n> `./a/shared.ts`\nEdit `./b/shared.ts`", ["./b/shared.ts"]],
290 [
291 "- ~~~\n example\n\nEdit `./a/shared.ts` and `./b/shared.ts`",
292 ["./a/shared.ts", "./b/shared.ts"],
293 ],
294 ["Step\n2. ~~~\n `./a/shared.ts`", ["./a/shared.ts"]],
295 ["1. Step\n ```\r\n `./a/shared.ts`\r\n ```\r\n`./b/shared.ts`", ["./b/shared.ts"]],
296 [" ```\n `./a/shared.ts`\n ```\n\n`./b/shared.ts`", ["./b/shared.ts"]],
297 ["Example\n\n `src/a/index.ts`\n\nOpen `src/b/index.ts`", ["src/b/index.ts"]],
298 ["\r\t`src/a/index.ts`\r\rOpen `src/b/index.ts`", ["src/b/index.ts"]],
299 ["Example\n `src/a/index.ts`\n\n `src/b/index.ts`", ["src/a/index.ts"]],
300 ])("skips code spans inside code blocks in %j", (markdown, spans) => {
301 expect(extractInlineCodeSpans(markdown)).toEqual(spans);
302 });
303});
304
305describe("extractMarkdownLinkHrefs", () => {
306 it.each(["\n\n", "\n \t\n", "\r\r"])(
307 "ends an unfinished label before a blank line %j after a backslash",
308 (breaks) => {
309 expect(
310 extractMarkdownLinkHrefs(
311 "Example [unfinished\\" + breaks + "](./old/a.ts)\n\nOpen [a.ts](./new/a.ts).",
312 ),
313 ).toEqual(["./new/a.ts"]);
314 },
315 );
316
317 it.each([
318 ["[".repeat(32_000), []],
319 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
320 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
321 ["](img.png) [a.ts](lib/a.ts)", ["img.png", "lib/a.ts"]],
322 ["[a\n# heading\nb](src/a.ts) [a.ts](lib/a.ts)", ["lib/a.ts"]],
323 [
324 "[](src/index.ts) [](lib/index.ts)",
325 ["a.png", "src/index.ts", "b.png", "lib/index.ts"],
326 ],
327 [String.raw`[\](src/index.ts) [label!](ok)`, ["a.png", "ok"]],
328 ["See [the [docs] guide](docs/guide.md) and [[1]](src/a.ts)", ["docs/guide.md", "src/a.ts"]],
329 ["[a [b](x) [c \\[d](y) [e\\]](z)", ["x", "y", "z"]],
330 ["[a [b]\n\nc](src/a.ts) [outer [inner]()](src/b.ts)", []],
331 ["[literal](\n\n./old/a.ts)\n\n[actual](./new/a.ts)", ["./new/a.ts"]],
332 ["[a]( \n \n<./old/a.ts>) [b](\n./new/a.ts)", ["./new/a.ts"]],
333 [`See ${serializeComposerFileLink("pages/posts/[id].tsx")} now`, ["pages/posts/%5Bid%5D.tsx"]],
334 ["[a](</tmp/one/a.ts >) [b]( /tmp/two/b.ts )", ["/tmp/one/a.ts ", "/tmp/two/b.ts"]],
335 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
336 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
337 });
338
339 it.each([
340 ["```md\n- [Setup](./a/README.md)\n```\n\nSee [README.md](./b/README.md)", ["./b/README.md"]],
341 ["Write `[Setup](./a/README.md)` as [README.md](./b/README.md)", ["./b/README.md"]],
342 ["[a `b](./a/README.md)` [c](./b/README.md)", ["./b/README.md"]],
343 ["[`code`](./a/README.md) and [x](<./b/a`b`.md>)", ["./a/README.md", "./b/a`b`.md"]],
344 ["`unclosed [a](./a/README.md)\n\nand` [b](./b/README.md)", ["./a/README.md", "./b/README.md"]],
345 ["~~~\n[a](./a/README.md)\n\n~~~\n[b](./b/README.md)", ["./b/README.md"]],
346 ["Type `` ``` `` then [a](./a/README.md)", ["./a/README.md"]],
347 ["\\``[a](./a/README.md)` [b](./b/README.md)", ["./b/README.md"]],
348 ["```\n```js\n[a](./a/README.md)\n```\n[b](./b/README.md)", ["./b/README.md"]],
349 ["- ```\n [a](./a/README.md)\n ```\n\n[b](./b/README.md)", ["./b/README.md"]],
350 ["> ```\n> [a](./a/README.md)\n> ```\n\n[b](./b/README.md)", ["./b/README.md"]],
351 ["- ```\n [a](./a/README.md)\n\n[b](./b/README.md)", ["./b/README.md"]],
352 ])("skips links inside code in %s", (markdown, hrefs) => {
353 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
354 });
355
356 it.each([
357 ["\t[a](src/a.ts)\r\n\r\n [b](src/b.ts)\r\n[c](src/c.ts)", ["src/c.ts"]],
358 ["- Item\n\n [a](src/a.ts)\n\n [b](src/b.ts)", ["src/b.ts"]],
359 ["> [a](src/a.ts)\n>\n> [b](src/b.ts)", ["src/b.ts"]],
360 ["Paragraph\n [a](src/a.ts)\n\n [b](src/b.ts)", ["src/a.ts"]],
361 [" [a](src/a.ts)\r\rOpen [b](src/b.ts)", ["src/b.ts"]],
362 ["Intro\r\r [a](src/a.ts)\r\nOpen [b](src/b.ts)", ["src/b.ts"]],
363 [
364 "[^n]: Intro\n\n [a](src/a.ts)\n\n [code](src/code.ts)\n\nSee [^n] and [b](src/b.ts)",
365 ["src/a.ts", "src/b.ts"],
366 ],
367 ["a | b\n-- | --\n [a](src/a.ts) | hi\n\n[b](src/b.ts)", ["src/b.ts"]],
368 ["a | b\n-- | --\nrow | row\n [a](src/a.ts) | hi\n\n[b](src/b.ts)", ["src/b.ts"]],
369 [
370 "- [code](src/code.ts)\n- [a](src/a.ts)\n- \t\t[b](src/b.ts)",
371 ["src/a.ts", "src/b.ts"],
372 ],
373 [
374 "- Example\n\n [a](src/a.ts)\n\n [code](src/code.ts)\n- [b](src/b.ts)",
375 ["src/a.ts", "src/b.ts"],
376 ],
377 [
378 "- [index.ts](src/a/index.ts) | heading\n -- | --\n\n[index.ts](src/b/index.ts)\n\n- [notes](src/notes.ts)",
379 ["src/b/index.ts", "src/notes.ts"],
380 ],
381 [
382 "- first\n\n - [index.ts](src/a/index.ts) | heading\n -- | --\n\n[index.ts](src/b/index.ts)",
383 ["src/b/index.ts"],
384 ],
385 [
386 "> - [index.ts](src/a/index.ts)\n> heading continuation\n> ===\n\n[index.ts](src/b/index.ts)",
387 ["src/b/index.ts"],
388 ],
389 [
390 "[^a\u00a0b]: Intro\n\n [index.ts](src/a/index.ts)\n\nSee [^a\u00a0b] and [index.ts](src/b/index.ts)",
391 ["src/a/index.ts", "src/b/index.ts"],
392 ],
393 ])("excludes indented code destinations in %j", (markdown, hrefs) => {
394 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
395 });
396
397 it.each([
398 [
399 String.raw`[one](<./a/doc\> final.ts>) [two](<./b/doc\> final.ts>)`,
400 [String.raw`./a/doc\> final.ts`, String.raw`./b/doc\> final.ts`],
401 ],
402 [
403 String.raw`[one](<./a/doc\\> "title") [two](<./b/doc\\> (title))`,
404 [String.raw`./a/doc\\`, String.raw`./b/doc\\`],
405 ],
406 [
407 String.raw`[bad](<./x/dir\> text) [one](<./a/doc\\\> final.ts> "title") [two](<./b/doc\< final.ts>)`,
408 [String.raw`./a/doc\\\> final.ts`, String.raw`./b/doc\< final.ts`],
409 ],
410 [
411 "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
412 ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
413 ],
414 ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
415 ["[a.ts](a/a.ts (first)) [a.ts](b/a.ts (second))", ["a/a.ts", "b/a.ts"]],
416 [
417 String.raw`[a](<a/a.ts> (escaped \(first\) and \\)) [b](b/a.ts (second))`,
418 ["a/a.ts", "b/a.ts"],
419 ],
420 [
421 "[a](a.ts (nested (title))) [b](b.ts (missing close) [c](c.ts (blank\n\nline)) [d](ok)",
422 ["ok"],
423 ],
424 [`[a](a.ts "it's a title") [b](b.ts 'a "quoted" title') [c](ok)`, ["a.ts", "b.ts", "ok"]],
425 [`[a.ts](src/a.ts "say \\"hello\\"") [a.ts](lib/a.ts)`, ["src/a.ts", "lib/a.ts"]],
426 [`[a](a.ts 'it\\'s') [b](b.ts "ends in \\\\") [c](ok)`, ["a.ts", "b.ts", "ok"]],
427 [`[a](a.ts "unclosed \\") [b](b.ts)`, ["b.ts"]],
428 [`[a](a.ts "mixed') [b](b.ts)`, ["b.ts"]],
429 [`[a](a.ts "it's\n\n[b](lib/a.ts)")`, ["lib/a.ts"]],
430 ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
431 ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
432 ["[a](unclosed(paren) [b](ok)", ["ok"]],
433 ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
434 ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
435 ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
436 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
437 });
438
439 it.each([
440 [
441 "[left](./left\u00a0folder/a.ts) and [right](./right/a.ts)",
442 ["./left\u00a0folder/a.ts", "./right/a.ts"],
443 ],
444 ["[a](./a.ts\f) [b](./b.ts\v) [c](ok)", ["ok"]],
445 [
446 "[left](./left/a.ts\u00a0) and [right](./right/a.ts\u00a0)",
447 ["./left/a.ts\u00a0", "./right/a.ts\u00a0"],
448 ],
449 ["[x](./a\0b.ts)", ["./a\0b.ts"]],
450 ])("ends bare destinations only at ASCII whitespace in %j", (markdown, hrefs) => {
451 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
452 });
453});
454
455describe("resolveMarkdownFileLinkTarget", () => {
456 it("resolves a relative folder with a trailing slash against cwd", () => {
457 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
458 "/repo/project/patches/",
459 );
460 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
461 });
462
463 // regression: #33625 backslash-separated relative links open the workspace file
464 it.each([
465 ["src/Example.cs", "C:\\repo\\src\\Example.cs"],
466 ["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
467 ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
468 ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
469 ])("resolves the relative link %s against a windows cwd", (href, target) => {
470 expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
471 });
472
473 it("keeps a backslash in a relative link that already names a file", () => {
474 expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/foo\\bar.ts");
475 });
476
477 it("keeps a final nonbreaking space in the path, before a line anchor too", () => {
478 expect(resolveMarkdownFileLinkTarget("./a.ts\u00a0", "/repo")).toBe("/repo/a.ts\u00a0");
479 expect(resolveMarkdownFileLinkTarget("./a.ts\u00a0#L3", "/repo")).toBe("/repo/a.ts\u00a0:3");
480 });
481
482 it("does not treat filename line references as external schemes", () => {
483 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
484 "/Users/julius/project/script.ts:10",
485 );
486 });
487
488 it("maps #L line anchors to editor line suffixes", () => {
489 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
490 "/Users/julius/project/src/main.ts:42:7",
491 );
492 });
493
494 it("ignores external urls", () => {
495 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
496 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
497 });
498
499 it("does not double-decode file URLs", () => {
500 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
501 "/Users/julius/project/file%20name.md",
502 );
503 });
504
505 it("decodes escaped spaces in a file URL that also holds a literal percent", () => {
506 expect(
507 resolveMarkdownFileLink("file:///C:/My%20Folder/100%/notes.md#L10-L20", "C:/"),
508 ).toMatchObject({
509 targetPath: "C:/My Folder/100%/notes.md:10",
510 position: { path: "C:/My Folder/100%/notes.md", line: 10, endLine: 20 },
511 });
512 expect(resolveMarkdownFileLinkTarget("file:///tmp/My%20Folder/100%25%/a%2520b.md")).toBe(
513 "/tmp/My Folder/100%%/a%20b.md",
514 );
515 expect(resolveMarkdownFileLinkTarget("file:///tmp/100%/notes%FF.md#L10")).toBe(
516 "/tmp/100%/notes%FF.md:10",
517 );
518 });
519
520 it("opens an explicit file URL to an extensionless file outside known roots", () => {
521 expect(resolveMarkdownFileLink("file:///nix/store/test/bin/tool#L10-L20")).toMatchObject({
522 targetPath: "/nix/store/test/bin/tool:10",
523 position: { path: "/nix/store/test/bin/tool", line: 10, endLine: 20 },
524 });
525 });
526
527 it("links a network path only inside the project's share", () => {
528 const cwd = "\\\\server\\share";
529 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
530 "\\\\server\\share\\workspace-image.svg",
531 );
532 // Opening these sent the Windows user's NTLM credentials to attacker.test.
533 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
534 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
535 }
536 });
537
538 it("keeps an encoded final space in the absolute target", () => {
539 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
540 "/tmp/repo/file.ts ",
541 );
542 });
543
544 it("normalizes slash-prefixed windows drive paths before resolving", () => {
545 expect(
546 resolveMarkdownFileLinkTarget(
547 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
548 ),
549 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
550 });
551
552 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
553 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
554 "c:/Users/x/shot.png",
555 );
556 });
557});