Skip to content

Commit a733c66

Browse files
committed
fix(shared): relative file links with routing punctuation open as files
The relative link patterns only allowed letters, digits, dots, underscores, hyphens and spaces, so app/routes/posts.$id.tsx, app/(group)/page.tsx, pages/[id].tsx, src/routes/+page.svelte and @scope/pkg/index.js were not file links, including the links the composer itself writes for those files.
1 parent 8760d06 commit a733c66

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎packages/shared/src/markdownLinks.test.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { workspaceRelativeFilePath } from "./path.ts";
22
import { describe, expect, it } from "vite-plus/test";
3+
import { serializeComposerFileLink } from "./composerTrigger.ts";
34
import { extractMarkdownLinkHrefs, resolveMarkdownFileLinkTarget } from "./markdownLinks.ts";
45

56
import {
@@ -38,16 +39,38 @@ describe("parseMarkdownFileLink", () => {
3839
["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
3940
["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
4041
["file://localhost/home/me/notes.md", "/home/me/notes.md"],
42+
["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"],
43+
["src/routes/+page.svelte", "src/routes/+page.svelte"],
44+
["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"],
45+
["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],
46+
["+page.svelte", "+page.svelte"],
47+
["[id].tsx", "[id].tsx"],
4148
])("recognizes %s as a file", (href, path) => {
4249
expect(parseMarkdownFileLink(href)?.path).toBe(path);
4350
});
4451

45-
it.each(["https://example.com/docs", "/chat/settings#L3", "TODO:12", "patches"])(
46-
"does not treat %s as a file",
47-
(href) => {
48-
expect(parseMarkdownFileLink(href)).toBeNull();
49-
},
50-
);
52+
it.each([
53+
"app/routes/posts.$postId.tsx",
54+
"pages/posts/[id].tsx",
55+
"docs/My File (draft).md",
56+
"@scope/package.json",
57+
"$postId.tsx",
58+
])("reads back the composer's link to %s", (path) => {
59+
const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
60+
expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);
61+
});
62+
63+
it.each([
64+
"https://example.com/docs",
65+
"/chat/settings#L3",
66+
"TODO:12",
67+
"patches",
68+
69+
"foo(bar)",
70+
"$HOME",
71+
])("does not treat %s as a file", (href) => {
72+
expect(parseMarkdownFileLink(href)).toBeNull();
73+
});
5174

5275
it("recognizes a relative folder that ends in a slash", () => {
5376
expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import {
1414

1515
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
1616

17-
// Names may use any script (`線性代數/考試.pdf`). The final segment is optional so an
18-
// explicit folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no
19-
// slash still fails.
17+
// Names may use any script (`線性代數/考試.pdf`) and routing punctuation (`posts.$id.tsx`,
18+
// `(group)/[id]/+page.svelte`, `@scope/pkg`). The final segment is optional so an explicit
19+
// folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no slash still fails.
2020
const RELATIVE_FILE_PATH_PATTERN =
21-
/^(?:[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*\/)+(?:[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*(?::\d+){0,2})?$/u;
21+
/^(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*\/)+(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*(?::\d+){0,2})?$/u;
2222

23+
// No `@` here: `[email protected]` is an address, not a file.
2324
const RELATIVE_FILE_NAME_PATTERN =
24-
/^[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u;
25+
/^[\p{L}\p{M}\p{N}._$+()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+()[\]-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u;
2526

2627
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
2728

0 commit comments

Comments
 (0)