Skip to content

Commit 7da6dc9

Browse files
committed
fix(shared): file links with & in the path keep their parent folder labels
The pre-scan decoded destinations with its own rules, and those differ from the parser on `&`: it stripped `\.` from Windows paths while the renderer keeps it, and it left character references like `&` undecoded on every path. The two then disagreed on the path, so the folder label was lost. Destinations with `&` now go through the same parser and Windows path plugin the renderer uses. The helper moves next to that parser, so markdownLinks stays free of it.
1 parent d0bf1f9 commit 7da6dc9

5 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/
22
import {
33
isWindowsDrivePathHref,
44
normalizeMarkdownLinkDestination,
5-
decodeMarkdownLinkEscapes,
65
extractInlineCodeSpans,
76
extractMarkdownLinkHrefs,
87
inlineCodeFilePathCandidate,
@@ -18,6 +17,7 @@ import {
1817
CHAT_MARKDOWN_REMARK_PLUGINS,
1918
CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
2019
CHAT_MARKDOWN_REHYPE_PLUGINS,
20+
decodeMarkdownLinkEscapes,
2121
} from "@t3tools/shared/markdownPipeline";
2222
import { useAtomValue } from "@effect/atom-react";
2323
import {

‎packages/shared/src/markdownLinks.ts‎

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

406-
export function decodeMarkdownLinkEscapes(href: string): string {
407-
if (isWindowsAbsolutePath(href) && !href.includes("&")) return href;
408-
return href.replace(/\\([!-/:-@[-`{-~])/g, "$1");
409-
}
410-
411406
export function extractMarkdownLinkHrefs(markdown: string): string[] {
412407
const hrefs: string[] = [];
413408
const scanDestination = markdownLinkDestinationScanner(markdown);

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
remarkCodexDirectives,
1616
} from "./codexMarkdownDirectives.ts";
1717
import { isWindowsDrivePathHref, normalizeMarkdownLinkDestination } from "./markdownLinks.ts";
18+
import { isWindowsAbsolutePath } from "./path.ts";
1819
import { THREAD_LINK_PROTOCOL } from "./threadLinks.ts";
1920

2021
type MarkdownImageHastNode = {
@@ -413,3 +414,17 @@ export function chatMarkdownImageSources(markdown: string): Array<string> {
413414
visit(imageSourceProcessor.runSync(imageSourceProcessor.parse(markdown)) as ImageSourceNode);
414415
return sources;
415416
}
417+
418+
const linkDestinationProcessor = unified().use(remarkParse).use(remarkKeepWindowsPaths);
419+
420+
/** Reads a destination from `extractMarkdownLinkHrefs` as the renderer's parser does. */
421+
export function decodeMarkdownLinkEscapes(href: string): string {
422+
// Only the parser knows character references, so destinations that may have one go through it.
423+
if (href.includes("&")) {
424+
const destination = /\s/.test(href) ? `<${href}>` : href;
425+
const paragraph = linkDestinationProcessor.parse(`[](${destination})`).children[0];
426+
const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
427+
if (link?.type === "link") return link.url;
428+
}
429+
return isWindowsAbsolutePath(href) ? href : href.replace(/\\([!-/:-@[-`{-~])/g, "$1");
430+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ it("searches displayed prose and file-chip labels without hidden paths", () => {
9393
String.raw`C:\repo`,
9494
),
9595
).toEqual(["a page.tsx · repo/.a and b page.tsx · repo/.b"]);
96+
for (const name of ["x&y", "x&amp;y"]) {
97+
expect(
98+
assistantSegments(
99+
`[a](C:\\repo\\.a\\${name}\\page.tsx) and [b](C:\\repo\\.b\\${name}\\page.tsx)`,
100+
String.raw`C:\repo`,
101+
),
102+
).toEqual(["a page.tsx · .a/x&y and b page.tsx · .b/x&y"]);
103+
}
104+
expect(
105+
assistantSegments("[a](/repo/a&amp;b/page.tsx) and [b](/repo/c&amp;d/page.tsx)", "/repo"),
106+
).toEqual(["a page.tsx · repo/a&b and b page.tsx · repo/c&d"]);
96107
});
97108

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

‎packages/shared/src/threadFindText.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from "./fileLinks.ts";
1616
import {
1717
isMarkdownFileLinkLabel,
18-
decodeMarkdownLinkEscapes,
1918
extractMarkdownLinkHrefs,
2019
extractInlineCodeSpans,
2120
inlineCodeFilePathCandidate,
@@ -37,6 +36,7 @@ import {
3736
CHAT_MARKDOWN_REHYPE_PLUGINS,
3837
CHAT_MARKDOWN_REMARK_PLUGINS,
3938
CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
39+
decodeMarkdownLinkEscapes,
4040
} from "./markdownPipeline.ts";
4141

4242
// Inline wrappers (including Shiki token spans) must not split a search phrase.

0 commit comments

Comments
 (0)