TypeScript
fix(shared): relative file links with routing punctuation open as files (#28637)
Abhishek B R65efae1adb7f
diff --git a/packages/shared/src/markdownLinks.test.ts b/packages/shared/src/markdownLinks.test.tsindex 53377008716..318f2e1c333 100644--- a/packages/shared/src/markdownLinks.test.ts+++ b/packages/shared/src/markdownLinks.test.ts@@ -1,5 +1,6 @@import { workspaceRelativeFilePath } from "./path.ts";import { describe, expect, it } from "vite-plus/test";+import { serializeComposerFileLink } from "./composerTrigger.ts";import { extractMarkdownLinkHrefs, resolveMarkdownFileLinkTarget } from "./markdownLinks.ts";import {@@ -38,16 +39,38 @@ describe("parseMarkdownFileLink", () => {["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],["file://localhost/home/me/notes.md", "/home/me/notes.md"],+ ["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"],+ ["src/routes/+page.svelte", "src/routes/+page.svelte"],+ ["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"],+ ["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],+ ["+page.svelte", "+page.svelte"],+ ["[id].tsx", "[id].tsx"],])("recognizes %s as a file", (href, path) => {expect(parseMarkdownFileLink(href)?.path).toBe(path);});- it.each(["https://example.com/docs", "/chat/settings#L3", "TODO:12", "patches"])(- "does not treat %s as a file",- (href) => {- expect(parseMarkdownFileLink(href)).toBeNull();- },- );+ it.each([+ "app/routes/posts.$postId.tsx",+ "pages/posts/[id].tsx",+ "docs/My File (draft).md",+ "@scope/package.json",+ "$postId.tsx",+ ])("reads back the composer's link to %s", (path) => {+ const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];+ expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);+ });++ it.each([+ "https://example.com/docs",+ "/chat/settings#L3",+ "TODO:12",+ "patches",+ "[email protected]",+ "foo(bar)",+ "$HOME",+ ])("does not treat %s as a file", (href) => {+ expect(parseMarkdownFileLink(href)).toBeNull();+ });it("recognizes a relative folder that ends in a slash", () => {expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });diff --git a/packages/shared/src/markdownLinks.ts b/packages/shared/src/markdownLinks.tsindex 177e6fcbf50..6b76b61475a 100644--- a/packages/shared/src/markdownLinks.ts+++ b/packages/shared/src/markdownLinks.ts@@ -14,14 +14,15 @@ import {const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;-// Names may use any script (`線性代數/考試.pdf`). The final segment is optional so an-// explicit folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no-// slash still fails.+// Names may use any script (`線性代數/考試.pdf`) and routing punctuation (`posts.$id.tsx`,+// `(group)/[id]/+page.svelte`, `@scope/pkg`). The final segment is optional so an explicit+// folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no slash still fails.const RELATIVE_FILE_PATH_PATTERN =- /^(?:[\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;+ /^(?:[\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;+// No `@` here: `[email protected]` is an address, not a file.const RELATIVE_FILE_NAME_PATTERN =- /^[\p{L}\p{M}\p{N}._-]+(?: +[\p{L}\p{M}\p{N}._-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u;+ /^[\p{L}\p{M}\p{N}._$+()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+()[\]-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u;const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;