diff --git a/apps/web/package.json b/apps/web/package.json index 598feaec0c..7e0d5775a2 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -39,6 +39,7 @@ "jszip": "3.10.1", "lexical": "^0.41.0", "lucide-react": "^0.564.0", + "micromark-util-decode-string": "^2.0.1", "react": "19.2.6", "react-dom": "19.2.6", "react-markdown": "^10.1.0", diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.ts index 7a66234d0f..fb9648d952 100644 --- a/apps/web/src/markdown-links.test.ts +++ b/apps/web/src/markdown-links.test.ts @@ -14,7 +14,7 @@ describe("extractMarkdownLinkHrefs", () => { it("extracts angle-bracketed destinations containing spaces", () => { const [href] = extractMarkdownLinkHrefs('[file]( "source")'); - expect(href).toBe(""); + expect(href).toBe("src/file name.ts"); expect(normalizeMarkdownLinkHrefKey(href ?? "")).toBe( normalizeMarkdownLinkHrefKey("src/file%20name.ts"), ); @@ -28,6 +28,50 @@ describe("extractMarkdownLinkHrefs", () => { it("continues to extract regular markdown destinations", () => { expect(extractMarkdownLinkHrefs("[file](src/file%20name.ts)")).toEqual(["src/file%20name.ts"]); }); + + it("extracts and resolves destinations containing balanced parentheses", () => { + const hrefs = extractMarkdownLinkHrefs( + '[one](src/foo(bar).ts) [two](src/foo(bar(baz)).ts "source")', + ); + + expect(hrefs).toEqual(["src/foo(bar).ts", "src/foo(bar(baz)).ts"]); + expect(resolveMarkdownFileLinkMeta(hrefs[0], "/repo/project")).toMatchObject({ + filePath: "/repo/project/src/foo(bar).ts", + }); + }); + + it("matches renderer decoding and nested labels", () => { + const hrefs = extractMarkdownLinkHrefs( + String.raw`[escaped](src/foo\(bar\).ts) [nested [label]](src/foo(bar).ts (source))`, + ); + + expect(hrefs).toEqual(["src/foo(bar).ts", "src/foo(bar).ts"]); + }); + + it("handles images inside links and brackets in destinations", () => { + expect(extractMarkdownLinkHrefs("[outer ![alt](img.png)](src/foo(bar).ts)")).toEqual([ + "src/foo(bar).ts", + ]); + expect(extractMarkdownLinkHrefs("[file]()")).toEqual(["/tmp/foo[bar].ts"]); + }); + + it("ignores destinations with unbalanced parentheses", () => { + expect(extractMarkdownLinkHrefs("[file](src/foo(bar).ts")).toEqual([]); + }); + + it("recovers a valid link after a malformed destination", () => { + expect(extractMarkdownLinkHrefs("[broken](oops\n[file](src/file.ts)")).toEqual(["src/file.ts"]); + expect(extractMarkdownLinkHrefs("[bad](oops([good](src/file.ts)")).toEqual(["src/file.ts"]); + expect(extractMarkdownLinkHrefs(String.raw`[bad](oops\ [good](src/file.ts))`)).toEqual([ + "src/file.ts", + ]); + }); + + it("keeps malformed input parsing bounded", () => { + const malformed = `${"[x]( { diff --git a/apps/web/src/markdown-links.ts b/apps/web/src/markdown-links.ts index bf34926bc8..8ee071ae5f 100644 --- a/apps/web/src/markdown-links.ts +++ b/apps/web/src/markdown-links.ts @@ -1,3 +1,5 @@ +import { decodeString } from "micromark-util-decode-string"; + import { formatWorkspaceRelativePath } from "./filePathDisplay"; import { resolvePathLinkTarget, splitPathAndPosition } from "./terminal-links"; @@ -5,11 +7,13 @@ const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/; const WINDOWS_UNC_PATH_PATTERN = /^\\\\/; const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/; const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/; -const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._ -]+(?:\/[A-Za-z0-9._ -]+)+(?::\d+){0,2}$/; -const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._ -]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/; +const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._ ()-]+(?:\/[A-Za-z0-9._ ()-]+)+(?::\d+){0,2}$/; +const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._ ()-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/; const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/; const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/; -const MARKDOWN_LINK_HREF_PATTERN = /\[[^\]]*]\(\s*(<[^>\n]+>|[^)\s]+)(?:\s+["'][^"']*["'])?\s*\)/g; +const MARKDOWN_ESCAPABLE_CHARACTER_PATTERN = /^[!-/:-@[-`{-~]$/; +const MAX_MARKDOWN_DESTINATION_DEPTH = 32; +const MAX_MARKDOWN_TITLE_LENGTH = 1_024; // Standard OS and dev-container roots; deliberately excludes app-route-ish // prefixes like /app/ or /chat/ so SPA routes never read as files. const POSIX_FILE_ROOT_PREFIXES = [ @@ -65,12 +69,150 @@ export function normalizeMarkdownLinkDestination(value: string): string { return unwrapMarkdownLinkDestination(value.trim()); } +interface ParsedMarkdownDestination { + readonly href: string; + readonly end: number; +} + +function isMarkdownEscape(text: string, index: number): boolean { + return text[index] === "\\" && MARKDOWN_ESCAPABLE_CHARACTER_PATTERN.test(text[index + 1] ?? ""); +} + +function skipMarkdownWhitespace(text: string, start: number): number | null { + let index = start; + let lineBreaks = 0; + while (index < text.length && /[\t\n\r ]/.test(text[index] ?? "")) { + if (text[index] === "\n" || text[index] === "\r") { + lineBreaks += 1; + if (lineBreaks > 1) return null; + if (text[index] === "\r" && text[index + 1] === "\n") index += 1; + } + index += 1; + } + return index; +} + +function parseMarkdownLinkTitle(text: string, start: number): number | null { + const opener = text[start]; + if (opener !== '"' && opener !== "'" && opener !== "(") return null; + const closer = opener === "(" ? ")" : opener; + + const end = Math.min(text.length, start + 1 + MAX_MARKDOWN_TITLE_LENGTH); + for (let index = start + 1; index < end; index += 1) { + const character = text[index]; + if (isMarkdownEscape(text, index)) { + index += 1; + continue; + } + if (character === closer) return index + 1; + if (character === "\n" || character === "\r") { + const next = skipMarkdownWhitespace(text, index); + if (next === null) return null; + } + } + return null; +} + +function finishMarkdownDestination( + text: string, + destination: string, + start: number, +): ParsedMarkdownDestination | null { + const suffixStart = skipMarkdownWhitespace(text, start); + if (suffixStart === null) return null; + if (text[suffixStart] === ")") { + return { href: decodeString(destination), end: suffixStart }; + } + + const titleEnd = parseMarkdownLinkTitle(text, suffixStart); + if (titleEnd === null) return null; + const wrapperEnd = skipMarkdownWhitespace(text, titleEnd); + if (wrapperEnd === null || text[wrapperEnd] !== ")") return null; + return { href: decodeString(destination), end: wrapperEnd }; +} + +function parseMarkdownDestination(text: string, start: number): ParsedMarkdownDestination | null { + const destinationStart = skipMarkdownWhitespace(text, start); + if (destinationStart === null) return null; + + if (text[destinationStart] === "<") { + for (let index = destinationStart + 1; index < text.length; index += 1) { + const character = text[index]; + if (isMarkdownEscape(text, index)) { + index += 1; + continue; + } + if (character === ">") { + return finishMarkdownDestination(text, text.slice(destinationStart + 1, index), index + 1); + } + if (character === "<" || character === "\n" || character === "\r") { + return null; + } + } + return null; + } + + let depth = 0; + for (let index = destinationStart; index < text.length; index += 1) { + const character = text[index]; + if (isMarkdownEscape(text, index)) { + index += 1; + continue; + } + if (character === "(" && ++depth > MAX_MARKDOWN_DESTINATION_DEPTH) return null; + if (character === ")") { + if (depth === 0) { + return { + href: decodeString(text.slice(destinationStart, index)), + end: index, + }; + } + depth -= 1; + continue; + } + if (character === "\n" || character === "\r" || character === "\t" || character === " ") { + if (depth > 0) return null; + return finishMarkdownDestination(text, text.slice(destinationStart, index), index); + } + } + return null; +} + export function extractMarkdownLinkHrefs(text: string): string[] { const hrefs: string[] = []; - for (const match of text.matchAll(MARKDOWN_LINK_HREF_PATTERN)) { - const href = match[1]?.trim(); - if (href) hrefs.push(href); + const labelOpeners: { readonly image: boolean }[] = []; + let imageOpener = false; + + for (let index = 0; index < text.length; index += 1) { + const character = text[index]; + if (isMarkdownEscape(text, index)) { + index += 1; + continue; + } + if (character === "!" && text[index + 1] === "[") { + imageOpener = true; + continue; + } + if (character === "[") { + labelOpeners.push({ image: imageOpener }); + imageOpener = false; + continue; + } + imageOpener = false; + + if (character !== "]") continue; + const opener = labelOpeners.pop(); + if (!opener || text[index + 1] !== "(") continue; + + const destination = parseMarkdownDestination(text, index + 2); + if (!destination) continue; + if (!opener.image) { + hrefs.push(destination.href); + labelOpeners.length = 0; + } + index = destination.end; } + return hrefs; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2c79aea36a..11d963dfdd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -597,6 +597,9 @@ importers: lucide-react: specifier: ^0.564.0 version: 0.564.0(react@19.2.6) + micromark-util-decode-string: + specifier: ^2.0.1 + version: 2.0.1 react: specifier: 19.2.6 version: 19.2.6