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 it("recognizes a relative folder that ends in a slash", () => {
193 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
194 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
195 });
196
197 // regression: #16096 relative links with non-ASCII names open as files
198 it("recognizes relative paths with non-ASCII names", () => {
199 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
200 "pastexam/線性代數/exam_易志偉.pdf",
201 );
202 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
203 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
204 });
205
206 it("accepts conventional extensionless names with or without a position", () => {
207 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
208 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
209 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
210 });
211
212 it("reads line ranges and opens them at their first line", () => {
213 expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
214 path: "/repo/src/http.ts",
215 line: 42,
216 endLine: 64,
217 });
218 expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
219 path: "/repo/src/http.ts",
220 line: 42,
221 endLine: 64,
222 });
223 });
224
225 it("reads positions from suffixes and line anchors", () => {
226 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
227 path: "/Users/julius/project/src/main.ts",
228 line: 42,
229 column: 7,
230 });
231 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
232 path: "\\\\server\\share\\src\\main.ts",
233 line: 42,
234 column: 7,
235 });
236 });
237});
238
239describe("workspaceRelativeFilePath", () => {
240 // regression: #12449 workspace root links resolve to "."
241 it.each([
242 ["/repo/project", "/repo/project", "."],
243 ["C:/", "c:/", "."],
244 ["/repo/project/a.ts", undefined, null],
245 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
246 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
247 });
248});
249
250describe("extractInlineCodeSpans", () => {
251 it.each([
252 ['[example](https://example.com "`/tmp/other/main.ts`")', ["/tmp/current/main.ts"]],
253 ["[`example`](https://example.com/`/tmp/other/main.ts`)", ["example", "/tmp/current/main.ts"]],
254 ])("ignores backticks in accepted link tails: %s", (link, spans) => {
255 expect(extractInlineCodeSpans(`${link} and \`/tmp/current/main.ts\``)).toEqual(spans);
256 });
257
258 it.each([
259 ["``./a/shared.ts`` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
260 ["``a ` b`` and `./b/shared.ts`", ["a ` b", "./b/shared.ts"]],
261 ["``unclosed and `./b/shared.ts`", ["./b/shared.ts"]],
262 ["\\``./a/shared.ts` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
263 ["`a\\` and `./b/shared.ts`", ["a\\", "./b/shared.ts"]],
264 ["`unclosed\n`./b/shared.ts`", ["./b/shared.ts"]],
265 ])("matches complete backtick delimiter runs in %s", (markdown, spans) => {
266 expect(extractInlineCodeSpans(markdown)).toEqual(spans);
267 });
268
269 it.each([
270 [
271 "Use `` ``` `` to open a fence, then edit `./a/shared.ts` and `./b/shared.ts`",
272 ["```", "./a/shared.ts", "./b/shared.ts"],
273 ],
274 ["```x``` then `./a/shared.ts`", ["x", "./a/shared.ts"]],
275 ["~~~\n`./a/shared.ts`\n~~~\n`./b/shared.ts`", ["./b/shared.ts"]],
276 ["````md\n```ts\n`./a/shared.ts`\n```\n````\n`./b/shared.ts`", ["./b/shared.ts"]],
277 ["```\n ```\n`./a/shared.ts`\n```\n`./b/shared.ts`", ["./b/shared.ts"]],
278 [
279 "```diff\n- ```\n```\nEdit `./a/shared.ts` and `./b/shared.ts`",
280 ["./a/shared.ts", "./b/shared.ts"],
281 ],
282 ["> ```\n> `./a/shared.ts`\nEdit `./b/shared.ts`", ["./b/shared.ts"]],
283 [
284 "- ~~~\n example\n\nEdit `./a/shared.ts` and `./b/shared.ts`",
285 ["./a/shared.ts", "./b/shared.ts"],
286 ],
287 ["Step\n2. ~~~\n `./a/shared.ts`", ["./a/shared.ts"]],
288 ["1. Step\n ```\r\n `./a/shared.ts`\r\n ```\r\n`./b/shared.ts`", ["./b/shared.ts"]],
289 [" ```\n `./a/shared.ts`\n ```\n\n`./b/shared.ts`", ["./b/shared.ts"]],
290 ["Example\n\n `src/a/index.ts`\n\nOpen `src/b/index.ts`", ["src/b/index.ts"]],
291 ["\r\t`src/a/index.ts`\r\rOpen `src/b/index.ts`", ["src/b/index.ts"]],
292 ["Example\n `src/a/index.ts`\n\n `src/b/index.ts`", ["src/a/index.ts"]],
293 ])("skips code spans inside code blocks in %j", (markdown, spans) => {
294 expect(extractInlineCodeSpans(markdown)).toEqual(spans);
295 });
296});
297
298describe("extractMarkdownLinkHrefs", () => {
299 it.each(["\n\n", "\n \t\n", "\r\r"])(
300 "ends an unfinished label before a blank line %j after a backslash",
301 (breaks) => {
302 expect(
303 extractMarkdownLinkHrefs(
304 "Example [unfinished\\" + breaks + "](./old/a.ts)\n\nOpen [a.ts](./new/a.ts).",
305 ),
306 ).toEqual(["./new/a.ts"]);
307 },
308 );
309
310 it.each([
311 ["[".repeat(32_000), []],
312 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
313 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
314 [
315 "[](src/index.ts) [](lib/index.ts)",
316 ["a.png", "src/index.ts", "b.png", "lib/index.ts"],
317 ],
318 [String.raw`[\](src/index.ts) [label!](ok)`, ["a.png", "ok"]],
319 ["See [the [docs] guide](docs/guide.md) and [[1]](src/a.ts)", ["docs/guide.md", "src/a.ts"]],
320 ["[a [b](x) [c \\[d](y) [e\\]](z)", ["x", "y", "z"]],
321 ["[a [b]\n\nc](src/a.ts) [outer [inner]()](src/b.ts)", []],
322 ["[literal](\n\n./old/a.ts)\n\n[actual](./new/a.ts)", ["./new/a.ts"]],
323 ["[a]( \n \n<./old/a.ts>) [b](\n./new/a.ts)", ["./new/a.ts"]],
324 [`See ${serializeComposerFileLink("pages/posts/[id].tsx")} now`, ["pages/posts/%5Bid%5D.tsx"]],
325 ["[a](</tmp/one/a.ts >) [b]( /tmp/two/b.ts )", ["/tmp/one/a.ts ", "/tmp/two/b.ts"]],
326 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
327 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
328 });
329
330 it.each([
331 ["```md\n- [Setup](./a/README.md)\n```\n\nSee [README.md](./b/README.md)", ["./b/README.md"]],
332 ["Write `[Setup](./a/README.md)` as [README.md](./b/README.md)", ["./b/README.md"]],
333 ["[a `b](./a/README.md)` [c](./b/README.md)", ["./b/README.md"]],
334 ["[`code`](./a/README.md) and [x](<./b/a`b`.md>)", ["./a/README.md", "./b/a`b`.md"]],
335 ["`unclosed [a](./a/README.md)\n\nand` [b](./b/README.md)", ["./a/README.md", "./b/README.md"]],
336 ["~~~\n[a](./a/README.md)\n\n~~~\n[b](./b/README.md)", ["./b/README.md"]],
337 ["Type `` ``` `` then [a](./a/README.md)", ["./a/README.md"]],
338 ["\\``[a](./a/README.md)` [b](./b/README.md)", ["./b/README.md"]],
339 ["```\n```js\n[a](./a/README.md)\n```\n[b](./b/README.md)", ["./b/README.md"]],
340 ["- ```\n [a](./a/README.md)\n ```\n\n[b](./b/README.md)", ["./b/README.md"]],
341 ["> ```\n> [a](./a/README.md)\n> ```\n\n[b](./b/README.md)", ["./b/README.md"]],
342 ["- ```\n [a](./a/README.md)\n\n[b](./b/README.md)", ["./b/README.md"]],
343 ])("skips links inside code in %s", (markdown, hrefs) => {
344 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
345 });
346
347 it.each([
348 ["\t[a](src/a.ts)\r\n\r\n [b](src/b.ts)\r\n[c](src/c.ts)", ["src/c.ts"]],
349 ["- Item\n\n [a](src/a.ts)\n\n [b](src/b.ts)", ["src/b.ts"]],
350 ["> [a](src/a.ts)\n>\n> [b](src/b.ts)", ["src/b.ts"]],
351 ["Paragraph\n [a](src/a.ts)\n\n [b](src/b.ts)", ["src/a.ts"]],
352 [" [a](src/a.ts)\r\rOpen [b](src/b.ts)", ["src/b.ts"]],
353 ["Intro\r\r [a](src/a.ts)\r\nOpen [b](src/b.ts)", ["src/b.ts"]],
354 [
355 "[^n]: Intro\n\n [a](src/a.ts)\n\n [code](src/code.ts)\n\nSee [^n] and [b](src/b.ts)",
356 ["src/a.ts", "src/b.ts"],
357 ],
358 ["a | b\n-- | --\n [a](src/a.ts) | hi\n\n[b](src/b.ts)", ["src/b.ts"]],
359 ["a | b\n-- | --\nrow | row\n [a](src/a.ts) | hi\n\n[b](src/b.ts)", ["src/b.ts"]],
360 [
361 "- [code](src/code.ts)\n- [a](src/a.ts)\n- \t\t[b](src/b.ts)",
362 ["src/a.ts", "src/b.ts"],
363 ],
364 [
365 "- Example\n\n [a](src/a.ts)\n\n [code](src/code.ts)\n- [b](src/b.ts)",
366 ["src/a.ts", "src/b.ts"],
367 ],
368 [
369 "- [index.ts](src/a/index.ts) | heading\n -- | --\n\n[index.ts](src/b/index.ts)\n\n- [notes](src/notes.ts)",
370 ["src/b/index.ts", "src/notes.ts"],
371 ],
372 [
373 "- first\n\n - [index.ts](src/a/index.ts) | heading\n -- | --\n\n[index.ts](src/b/index.ts)",
374 ["src/b/index.ts"],
375 ],
376 [
377 "> - [index.ts](src/a/index.ts)\n> heading continuation\n> ===\n\n[index.ts](src/b/index.ts)",
378 ["src/b/index.ts"],
379 ],
380 [
381 "[^a\u00a0b]: Intro\n\n [index.ts](src/a/index.ts)\n\nSee [^a\u00a0b] and [index.ts](src/b/index.ts)",
382 ["src/a/index.ts", "src/b/index.ts"],
383 ],
384 ])("excludes indented code destinations in %j", (markdown, hrefs) => {
385 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
386 });
387
388 it.each([
389 [
390 String.raw`[one](<./a/doc\> final.ts>) [two](<./b/doc\> final.ts>)`,
391 [String.raw`./a/doc\> final.ts`, String.raw`./b/doc\> final.ts`],
392 ],
393 [
394 String.raw`[one](<./a/doc\\> "title") [two](<./b/doc\\> (title))`,
395 [String.raw`./a/doc\\`, String.raw`./b/doc\\`],
396 ],
397 [
398 String.raw`[bad](<./x/dir\> text) [one](<./a/doc\\\> final.ts> "title") [two](<./b/doc\< final.ts>)`,
399 [String.raw`./a/doc\\\> final.ts`, String.raw`./b/doc\< final.ts`],
400 ],
401 [
402 "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
403 ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
404 ],
405 ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
406 ["[a.ts](a/a.ts (first)) [a.ts](b/a.ts (second))", ["a/a.ts", "b/a.ts"]],
407 [
408 String.raw`[a](<a/a.ts> (escaped \(first\) and \\)) [b](b/a.ts (second))`,
409 ["a/a.ts", "b/a.ts"],
410 ],
411 [
412 "[a](a.ts (nested (title))) [b](b.ts (missing close) [c](c.ts (blank\n\nline)) [d](ok)",
413 ["ok"],
414 ],
415 [`[a](a.ts "it's a title") [b](b.ts 'a "quoted" title') [c](ok)`, ["a.ts", "b.ts", "ok"]],
416 [`[a.ts](src/a.ts "say \\"hello\\"") [a.ts](lib/a.ts)`, ["src/a.ts", "lib/a.ts"]],
417 [`[a](a.ts 'it\\'s') [b](b.ts "ends in \\\\") [c](ok)`, ["a.ts", "b.ts", "ok"]],
418 [`[a](a.ts "unclosed \\") [b](b.ts)`, ["b.ts"]],
419 [`[a](a.ts "mixed') [b](b.ts)`, ["b.ts"]],
420 [`[a](a.ts "it's\n\n[b](lib/a.ts)")`, ["lib/a.ts"]],
421 ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
422 ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
423 ["[a](unclosed(paren) [b](ok)", ["ok"]],
424 ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
425 ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
426 ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
427 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
428 });
429
430 it.each([
431 [
432 "[left](./left\u00a0folder/a.ts) and [right](./right/a.ts)",
433 ["./left\u00a0folder/a.ts", "./right/a.ts"],
434 ],
435 ["[a](./a.ts\f) [b](./b.ts\v) [c](ok)", ["ok"]],
436 [
437 "[left](./left/a.ts\u00a0) and [right](./right/a.ts\u00a0)",
438 ["./left/a.ts\u00a0", "./right/a.ts\u00a0"],
439 ],
440 ["[x](./a\0b.ts)", ["./a\0b.ts"]],
441 ])("ends bare destinations only at ASCII whitespace in %j", (markdown, hrefs) => {
442 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
443 });
444});
445
446describe("resolveMarkdownFileLinkTarget", () => {
447 it("resolves a relative folder with a trailing slash against cwd", () => {
448 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
449 "/repo/project/patches/",
450 );
451 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
452 });
453
454 // regression: #33625 backslash-separated relative links open the workspace file
455 it.each([
456 ["src/Example.cs", "C:\\repo\\src\\Example.cs"],
457 ["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
458 ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
459 ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
460 ])("resolves the relative link %s against a windows cwd", (href, target) => {
461 expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
462 });
463
464 it("keeps a backslash in a relative link that already names a file", () => {
465 expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/foo\\bar.ts");
466 });
467
468 it("keeps a final nonbreaking space in the path, before a line anchor too", () => {
469 expect(resolveMarkdownFileLinkTarget("./a.ts\u00a0", "/repo")).toBe("/repo/a.ts\u00a0");
470 expect(resolveMarkdownFileLinkTarget("./a.ts\u00a0#L3", "/repo")).toBe("/repo/a.ts\u00a0:3");
471 });
472
473 it("does not treat filename line references as external schemes", () => {
474 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
475 "/Users/julius/project/script.ts:10",
476 );
477 });
478
479 it("maps #L line anchors to editor line suffixes", () => {
480 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
481 "/Users/julius/project/src/main.ts:42:7",
482 );
483 });
484
485 it("ignores external urls", () => {
486 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
487 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
488 });
489
490 it("does not double-decode file URLs", () => {
491 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
492 "/Users/julius/project/file%20name.md",
493 );
494 });
495
496 it("decodes escaped spaces in a file URL that also holds a literal percent", () => {
497 expect(
498 resolveMarkdownFileLink("file:///C:/My%20Folder/100%/notes.md#L10-L20", "C:/"),
499 ).toMatchObject({
500 targetPath: "C:/My Folder/100%/notes.md:10",
501 position: { path: "C:/My Folder/100%/notes.md", line: 10, endLine: 20 },
502 });
503 expect(resolveMarkdownFileLinkTarget("file:///tmp/My%20Folder/100%25%/a%2520b.md")).toBe(
504 "/tmp/My Folder/100%%/a%20b.md",
505 );
506 expect(resolveMarkdownFileLinkTarget("file:///tmp/100%/notes%FF.md#L10")).toBe(
507 "/tmp/100%/notes%FF.md:10",
508 );
509 });
510
511 it("opens an explicit file URL to an extensionless file outside known roots", () => {
512 expect(resolveMarkdownFileLink("file:///nix/store/test/bin/tool#L10-L20")).toMatchObject({
513 targetPath: "/nix/store/test/bin/tool:10",
514 position: { path: "/nix/store/test/bin/tool", line: 10, endLine: 20 },
515 });
516 });
517
518 it("links a network path only inside the project's share", () => {
519 const cwd = "\\\\server\\share";
520 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
521 "\\\\server\\share\\workspace-image.svg",
522 );
523 // Opening these sent the Windows user's NTLM credentials to attacker.test.
524 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
525 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
526 }
527 });
528
529 it("keeps an encoded final space in the absolute target", () => {
530 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
531 "/tmp/repo/file.ts ",
532 );
533 });
534
535 it("normalizes slash-prefixed windows drive paths before resolving", () => {
536 expect(
537 resolveMarkdownFileLinkTarget(
538 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
539 ),
540 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
541 });
542
543 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
544 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
545 "c:/Users/x/shot.png",
546 );
547 });
548});