Skip to content

Commit d857dbd

Browse files
committed
fix(shared): file links with parentheses in the path keep their parent folder labels
extractMarkdownLinkHrefs ended a bare destination at the first closing parenthesis, so route group paths like app/(a)/page.tsx lost their parent folder labels and their folder text in thread search. Balance parentheses and honour escapes the way the parser does, with the same nesting limit.
1 parent 8760d06 commit d857dbd

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ describe("extractMarkdownLinkHrefs", () => {
102102
])("extracts links from incomplete and nested text", (markdown, hrefs) => {
103103
expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
104104
});
105+
106+
it.each([
107+
[
108+
"[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
109+
["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
110+
],
111+
['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
112+
["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
113+
["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
114+
["[a](unclosed(paren) [b](ok)", ["ok"]],
115+
["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
116+
["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
117+
])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
118+
expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
119+
});
105120
});
106121

107122
describe("resolveMarkdownFileLinkTarget", () => {

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,33 @@ function markdownLinkTailScanner(markdown: string) {
356356
};
357357
}
358358

359+
// micromark's own nesting limit. It also keeps link scanning linear: a scan only runs past another
360+
// link's opening parenthesis while that parenthesis counts toward this depth.
361+
const MAX_BARE_DESTINATION_PAREN_DEPTH = 32;
362+
363+
function bareMarkdownLinkDestinationEnd(markdown: string, start: number): number | null {
364+
const delimiter = /[\s()\\]/g;
365+
delimiter.lastIndex = start;
366+
let depth = 0;
367+
while (true) {
368+
const index = delimiter.exec(markdown)?.index ?? markdown.length;
369+
const char = markdown[index];
370+
if (char === "\\") {
371+
if (/[()\\]/.test(markdown[index + 1] ?? "")) delimiter.lastIndex = index + 2;
372+
} else if (char === "(") {
373+
if (depth === MAX_BARE_DESTINATION_PAREN_DEPTH) return null;
374+
depth += 1;
375+
} else if (char === ")" && depth > 0) {
376+
depth -= 1;
377+
} else {
378+
return depth === 0 ? index : null;
379+
}
380+
}
381+
}
382+
359383
function markdownLinkDestinationScanner(markdown: string) {
360384
const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
361385
const angleEnd = markdownRunEndFinder(markdown, /[>\n]/g);
362-
const bareEnd = markdownRunEndFinder(markdown, /[\s)]/g);
363386
// Separate caches keep scans moving forward when angle destinations fall back to bare ones.
364387
const angleTail = markdownLinkTailScanner(markdown);
365388
const bareTail = markdownLinkTailScanner(markdown);
@@ -373,8 +396,8 @@ function markdownLinkDestinationScanner(markdown: string) {
373396
if (end !== null) return { hrefStart: hrefStart + 1, hrefEnd, end };
374397
}
375398
}
376-
const hrefEnd = bareEnd(hrefStart);
377-
if (hrefEnd === hrefStart) return null;
399+
const hrefEnd = bareMarkdownLinkDestinationEnd(markdown, hrefStart);
400+
if (hrefEnd === null || hrefEnd === hrefStart) return null;
378401
const end = bareTail(hrefEnd);
379402
return end === null ? null : { hrefStart, hrefEnd, end };
380403
};

0 commit comments

Comments
 (0)