diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.ts index ae4c747eaa..70a7288b47 100644 --- a/apps/web/src/markdown-links.test.ts +++ b/apps/web/src/markdown-links.test.ts @@ -12,6 +12,12 @@ describe("normalizeMarkdownLinkDestination", () => { resolveMarkdownFileLinkTarget("apps/web/src/routes/\\(chat\\)/\\[id\\].tsx", "/repo"), ).toBe("/repo/apps/web/src/routes/(chat)/[id].tsx"); }); + + it("preserves windows path separators before punctuation", () => { + expect(resolveMarkdownFileLinkTarget(String.raw`C:\src\(group)\page.tsx`)).toBe( + String.raw`C:\src\(group)\page.tsx`, + ); + }); }); describe("rewriteMarkdownFileUriHref", () => { @@ -76,6 +82,16 @@ describe("resolveMarkdownFileLinkTarget", () => { ).toBe("/repo/project/apps/web/src/routes/(chat)/[threadId].tsx"); }); + it("preserves support for conservative extensionless relative file paths", () => { + expect(resolveMarkdownFileLinkTarget("scripts/release", "/repo/project")).toBe( + "/repo/project/scripts/release", + ); + }); + + it("does not treat ambiguous encoded relative web links as file paths", () => { + expect(resolveMarkdownFileLinkTarget("docs/user%20guide", "/repo/project")).toBeNull(); + }); + it("maps #L line anchors to editor line suffixes", () => { expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe( "/Users/julius/project/src/main.ts:42:7", diff --git a/apps/web/src/markdown-links.ts b/apps/web/src/markdown-links.ts index 2feaabd275..0054e66f1e 100644 --- a/apps/web/src/markdown-links.ts +++ b/apps/web/src/markdown-links.ts @@ -3,8 +3,10 @@ import { resolvePathLinkTarget, splitPathAndPosition } from "./terminal-links"; const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/; const WINDOWS_UNC_PATH_PATTERN = /^\\\\/; +const WINDOWS_BACKSLASH_PATH_PATTERN = /^(?:[A-Za-z]:\\|\\\\)/; const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/; const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/; +const CONSERVATIVE_RELATIVE_PATH_SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/; const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/; const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/; const POSIX_FILE_ROOT_PREFIXES = [ @@ -41,7 +43,11 @@ function safeDecode(value: string): string { const MARKDOWN_ESCAPE_PATTERN = /\\([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])/g; export function normalizeMarkdownLinkDestination(value: string): string { - return value.trim().replace(MARKDOWN_ESCAPE_PATTERN, "$1"); + const trimmedValue = value.trim(); + if (WINDOWS_BACKSLASH_PATH_PATTERN.test(trimmedValue)) { + return trimmedValue; + } + return trimmedValue.replace(MARKDOWN_ESCAPE_PATTERN, "$1"); } function stripSearchAndHash(value: string): { path: string; hash: string } { @@ -118,7 +124,12 @@ function isLikelyPathCandidate(path: string): boolean { const normalizedPath = actualPath.replaceAll("\\", "/"); const segments = normalizedPath.split("/"); if (segments.length > 1) { - return segments.every((segment) => segment.length > 0); + if (segments.some((segment) => segment.length === 0)) return false; + const basename = segments.at(-1) ?? ""; + return ( + /\.[A-Za-z0-9_-]+$/.test(basename) || + segments.every((segment) => CONSERVATIVE_RELATIVE_PATH_SEGMENT_PATTERN.test(segment)) + ); } return /\.[A-Za-z0-9_-]+$/.test(actualPath);