Skip to content

Commit 8191e46

Browse files
committed
fix(shared): decode escaped file links before labeling
1 parent d857dbd commit 8191e46

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/
22
import {
33
isWindowsDrivePathHref,
44
normalizeMarkdownLinkDestination,
5+
decodeMarkdownLinkEscapes,
56
extractInlineCodeSpans,
67
extractMarkdownLinkHrefs,
78
inlineCodeFilePathCandidate,
@@ -455,6 +456,7 @@ function fileLinkSourcesKey(
455456
): string {
456457
const hrefs = extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(text))
457458
.filter((href) => !parseComposerContextHref(href))
459+
.map(decodeMarkdownLinkEscapes)
458460
.map(normalizeMarkdownLinkHrefKey)
459461
.filter((href) => resolveMarkdownFileLinkMeta(href, cwd, imageBaseDir ?? cwd));
460462
const spans = extractInlineCodeSpans(text).filter((span) =>

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ function markdownLinkDestinationScanner(markdown: string) {
403403
};
404404
}
405405

406+
export function decodeMarkdownLinkEscapes(href: string): string {
407+
return href.replace(/\\([!-/:-@[-`{-~])/g, "$1");
408+
}
409+
406410
export function extractMarkdownLinkHrefs(markdown: string): string[] {
407411
const hrefs: string[] = [];
408412
const scanDestination = markdownLinkDestinationScanner(markdown);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ it("searches displayed prose and file-chip labels without hidden paths", () => {
7979
"label main.ts · L3:C2",
8080
]);
8181
expect(assistantSegments("[label](src/main.ts#L3C2)")).toEqual(["label"]);
82+
for (const text of [
83+
"[a](/repo/app/(a)/page.tsx) and [b](/repo/app/(b)/page.tsx)",
84+
String.raw`[a](/repo/app/\(a\)/page.tsx) and [b](/repo/app/\(b\)/page.tsx)`,
85+
]) {
86+
expect(assistantSegments(text, "/repo")).toEqual([
87+
"a page.tsx · app/(a) and b page.tsx · app/(b)",
88+
]);
89+
}
8290
});
8391

8492
it("keeps fence paths literal and indexes file-chip labels in user messages", () => {

‎packages/shared/src/threadFindText.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "./fileLinks.ts";
1616
import {
1717
isMarkdownFileLinkLabel,
18+
decodeMarkdownLinkEscapes,
1819
extractMarkdownLinkHrefs,
1920
extractInlineCodeSpans,
2021
inlineCodeFilePathCandidate,
@@ -119,7 +120,9 @@ function markdownThreadFindText(
119120
: assistantProcessor;
120121
const tree = processor.runSync(processor.parse(markdown));
121122
const paths = [
122-
...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)),
123+
...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)).map(
124+
decodeMarkdownLinkEscapes,
125+
),
123126
...extractInlineCodeSpans(markdown).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),
124127
].flatMap((href) => {
125128
const target = resolveMarkdownFileLinkTarget(href, cwd);

0 commit comments

Comments
 (0)