Skip to content

Commit 280fadd

Browse files
fix(shared): links to bracketed filenames keep their folder labels
The markdown link scanner ended a label at the first `]`, including the composer's escaped `\]`, so links like [\[id\].tsx](pages/posts/%5Bid%5D.tsx) were never extracted and same-named files lost their parent-folder labels. AI-assisted; verified with the tests and tools listed above.
1 parent 7e153d8 commit 280fadd

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,26 @@ function markdownLinkDestinationScanner(markdown: string) {
407407
};
408408
}
409409

410+
// The composer escapes brackets in labels (`[\[id\].tsx](...)`), and `\]` does not close one.
411+
function unescapedLabelEnd(markdown: string, start: number): number {
412+
let end = markdown.indexOf("]", start);
413+
while (end !== -1) {
414+
let backslashes = 0;
415+
while (end - backslashes > start && markdown[end - backslashes - 1] === "\\") backslashes += 1;
416+
if (backslashes % 2 === 0) return end;
417+
end = markdown.indexOf("]", end + 1);
418+
}
419+
return -1;
420+
}
421+
410422
export function extractMarkdownLinkHrefs(markdown: string): string[] {
411423
const hrefs: string[] = [];
412424
const scanDestination = markdownLinkDestinationScanner(markdown);
413425
let cursor = 0;
414426
while (cursor < markdown.length) {
415427
const labelStart = markdown.indexOf("[", cursor);
416428
if (labelStart === -1) break;
417-
const labelEnd = markdown.indexOf("]", labelStart + 1);
429+
const labelEnd = unescapedLabelEnd(markdown, labelStart + 1);
418430
if (labelEnd === -1) break;
419431

420432
// Skip a failed label once; nested openings share its closing bracket.

0 commit comments

Comments
 (0)