TypeScript
fix(shared): recognize relative folder links with trailing slash (#22188)
maco6de7da548d15
diff --git a/packages/shared/src/markdownLinks.test.ts b/packages/shared/src/markdownLinks.test.tsindex dafbfce2f94..01d9192e17e 100644--- a/packages/shared/src/markdownLinks.test.ts+++ b/packages/shared/src/markdownLinks.test.ts@@ -26,6 +26,8 @@ describe("inlineCodeFilePathCandidate", () => {["origin/main", null],["z-ai/glm-5.3", null],["share/man/ls.1", "share/man/ls.1"],+ ["src/main.ts/", null],+ ["./patches/", "./patches/"],])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {expect(inlineCodeFilePathCandidate(source)).toBe(candidate);});@@ -40,13 +42,18 @@ describe("parseMarkdownFileLink", () => {expect(parseMarkdownFileLink(href)?.path).toBe(path);});- it.each(["https://example.com/docs", "/chat/settings#L3", "TODO:12"])(+ 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("recognizes a relative folder that ends in a slash", () => {+ expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });+ expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");+ });+it("accepts conventional extensionless names with or without a position", () => {expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });@@ -89,6 +96,13 @@ describe("extractMarkdownLinkHrefs", () => {});describe("resolveMarkdownFileLinkTarget", () => {+ it("resolves a relative folder with a trailing slash against cwd", () => {+ expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(+ "/repo/project/patches/",+ );+ expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();+ });+it("does not treat filename line references as external schemes", () => {expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe("/Users/julius/project/script.ts:10",diff --git a/packages/shared/src/markdownLinks.ts b/packages/shared/src/markdownLinks.tsindex 941f26f55c4..6e985ccdb0f 100644--- a/packages/shared/src/markdownLinks.ts+++ b/packages/shared/src/markdownLinks.ts@@ -9,8 +9,10 @@ import {const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;+// 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 =- /^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2}$/;+ /^(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\/)+(?:[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*(?::\d+){0,2})?$/;const RELATIVE_FILE_NAME_PATTERN =/^[A-Za-z0-9._-]+(?: +[A-Za-z0-9._-]+)*\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;@@ -176,6 +178,9 @@ export function inlineCodeFilePathCandidate(codeText: string): string | null {isWindowsAbsolutePath(candidate);if (!hasExplicitPathShape) {const withoutPosition = candidate.replace(POSITION_SUFFIX_PATTERN, "");+ // Markdown destinations accept `patches/`. Inline code still needs a+ // file-shaped final segment, so a trailing slash stays plain code.+ if (withoutPosition.endsWith("/")) return null;const firstSegment = withoutPosition.split("/")[0] ?? withoutPosition;if (looksLikeHostname(firstSegment, hasPosition)) return null;const basename =