@devries-cameron20 / t3code Mirror · read-only / commits / cc0a7bb9d8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 14:45 UTC

fix(shared): file links with parentheses in the path keep their parent folder labels (#28644)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Abhishek B R committed Nov 14, 2026 at 18:39 UTC cc0a7bb9d8556cbe821b14e89ee9025b70192ebd
6 files changed +101 -4
apps/web/src/components/ChatMarkdown.tsx
+2
@@ -19,6 +19,7 @@ import {
19 CHAT_MARKDOWN_REMARK_PLUGINS,
20 CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
21 CHAT_MARKDOWN_REHYPE_PLUGINS,
22 + decodeMarkdownLinkEscapes,
23 remarkAssistantInsights,
24 } from "@t3tools/shared/markdownPipeline";
25 import { useAtomValue } from "@effect/atom-react";
@@ -471,6 +472,7 @@ function fileLinkSourcesKey(
472 ): string {
473 const hrefs = extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(text))
474 .filter((href) => !parseComposerContextHref(href))
475 + .map(decodeMarkdownLinkEscapes)
476 .map(normalizeMarkdownLinkHrefKey)
477 .filter((href) => resolveMarkdownFileLinkMeta(href, cwd, imageBaseDir ?? cwd));
478 const spans = extractInlineCodeSpans(text).filter((span) =>
packages/shared/src/markdownLinks.test.ts
+15
@@ -162,6 +162,21 @@ describe("extractMarkdownLinkHrefs", () => {
162 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
163 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
164 });
165 +
166 + it.each([
167 + [
168 + "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
169 + ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
170 + ],
171 + ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
172 + ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
173 + ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
174 + ["[a](unclosed(paren) [b](ok)", ["ok"]],
175 + ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
176 + ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
177 + ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
178 + expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
179 + });
180 });
181
182 describe("resolveMarkdownFileLinkTarget", () => {
packages/shared/src/markdownLinks.ts
+26 -3
@@ -383,10 +383,33 @@ function markdownLinkTailScanner(markdown: string) {
383 };
384 }
385
386 +// micromark's own nesting limit. It also keeps link scanning linear: a scan only runs past another
387 +// link's opening parenthesis while that parenthesis counts toward this depth.
388 +const MAX_BARE_DESTINATION_PAREN_DEPTH = 32;
389 +
390 +function bareMarkdownLinkDestinationEnd(markdown: string, start: number): number | null {
391 + const delimiter = /[\s()\\]/g;
392 + delimiter.lastIndex = start;
393 + let depth = 0;
394 + while (true) {
395 + const index = delimiter.exec(markdown)?.index ?? markdown.length;
396 + const char = markdown[index];
397 + if (char === "\\") {
398 + if (/[()\\]/.test(markdown[index + 1] ?? "")) delimiter.lastIndex = index + 2;
399 + } else if (char === "(") {
400 + if (depth === MAX_BARE_DESTINATION_PAREN_DEPTH) return null;
401 + depth += 1;
402 + } else if (char === ")" && depth > 0) {
403 + depth -= 1;
404 + } else {
405 + return depth === 0 ? index : null;
406 + }
407 + }
408 +}
409 +
410 function markdownLinkDestinationScanner(markdown: string) {
411 const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
412 const angleEnd = markdownRunEndFinder(markdown, /[>\n]/g);
389 - const bareEnd = markdownRunEndFinder(markdown, /[\s)]/g);
413 // Separate caches keep scans moving forward when angle destinations fall back to bare ones.
414 const angleTail = markdownLinkTailScanner(markdown);
415 const bareTail = markdownLinkTailScanner(markdown);
@@ -400,8 +423,8 @@ function markdownLinkDestinationScanner(markdown: string) {
423 if (end !== null) return { hrefStart: hrefStart + 1, hrefEnd, end };
424 }
425 }
403 - const hrefEnd = bareEnd(hrefStart);
404 - if (hrefEnd === hrefStart) return null;
426 + const hrefEnd = bareMarkdownLinkDestinationEnd(markdown, hrefStart);
427 + if (hrefEnd === null || hrefEnd === hrefStart) return null;
428 const end = bareTail(hrefEnd);
429 return end === null ? null : { hrefStart, hrefEnd, end };
430 };
packages/shared/src/markdownPipeline.ts
+19
@@ -30,6 +30,7 @@ import {
30 isWindowsDrivePathHref,
31 normalizeMarkdownLinkDestination,
32 } from "./markdownLinks.ts";
33 +import { isWindowsAbsolutePath } from "./path.ts";
34 import { THREAD_LINK_PROTOCOL } from "./threadLinks.ts";
35
36 export { remarkEnableOrderedListNumbers } from "./markdownOrderedListNumbers.ts";
@@ -867,3 +868,21 @@ export function chatMarkdownImageSources(markdown: string): Array<string> {
868 visit(imageSourceProcessor.runSync(imageSourceProcessor.parse(markdown)) as ImageSourceNode);
869 return sources;
870 }
871 +
872 +const linkDestinationProcessor = unified().use(remarkParse).use(remarkKeepWindowsPaths);
873 +
874 +// An angle destination cannot hold an unescaped `<` or `>`, or end on a lone backslash.
875 +const ANGLE_DESTINATION_CONTENT = /^(?:[^\\<>]|\\.)*$/;
876 +
877 +/** Reads a destination from `extractMarkdownLinkHrefs` as the renderer's parser does. */
878 +export function decodeMarkdownLinkEscapes(href: string): string {
879 + // Only the parser knows character references, so destinations that may have one go through it.
880 + if (href.includes("&")) {
881 + // The extractor drops angle brackets, and only that form keeps an unmatched `)` in the path.
882 + const destination = ANGLE_DESTINATION_CONTENT.test(href) ? `<${href}>` : href;
883 + const paragraph = linkDestinationProcessor.parse(`[](${destination})`).children[0];
884 + const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
885 + if (link?.type === "link") return link.url;
886 + }
887 + return isWindowsAbsolutePath(href) ? href : href.replace(/\\([!-/:-@[-`{-~])/g, "$1");
888 +}
packages/shared/src/threadFindText.test.ts
+35
@@ -87,6 +87,41 @@ it("searches displayed prose and file-chip labels without hidden paths", () => {
87 "label main.ts · L3:C2",
88 ]);
89 expect(assistantSegments("[label](src/main.ts#L3C2)")).toEqual(["label"]);
90 + for (const text of [
91 + "[a](/repo/app/(a)/page.tsx) and [b](/repo/app/(b)/page.tsx)",
92 + String.raw`[a](/repo/app/\(a\)/page.tsx) and [b](/repo/app/\(b\)/page.tsx)`,
93 + ]) {
94 + expect(assistantSegments(text, "/repo")).toEqual([
95 + "a page.tsx · app/(a) and b page.tsx · app/(b)",
96 + ]);
97 + }
98 + expect(
99 + assistantSegments(
100 + String.raw`[a](C:\repo\.a\page.tsx) and [b](C:\repo\.b\page.tsx)`,
101 + String.raw`C:\repo`,
102 + ),
103 + ).toEqual(["a page.tsx · repo/.a and b page.tsx · repo/.b"]);
104 + expect(
105 + assistantSegments(
106 + String.raw`[a](C:\repo\.a\x&amp;y\page.tsx) and [b](C:\repo\.b\x&amp;y\page.tsx)`,
107 + String.raw`C:\repo`,
108 + ),
109 + ).toEqual(["a page.tsx · .a/x&y and b page.tsx · .b/x&y"]);
110 + expect(
111 + assistantSegments("[a](/repo/a&amp;b/page.tsx) and [b](/repo/c&amp;d/page.tsx)", "/repo"),
112 + ).toEqual(["a page.tsx · repo/a&b and b page.tsx · repo/c&d"]);
113 + expect(
114 + assistantSegments("[a](</repo/a&b)/page.tsx>) and [b](</repo/c&d)/page.tsx>)", "/repo"),
115 + ).toEqual(["a page.tsx · repo/a&b) and b page.tsx · repo/c&d)"]);
116 + expect(
117 + assistantSegments("[a](/repo/a&b>c/page.tsx) and [b](/repo/c&d>e/page.tsx)", "/repo"),
118 + ).toEqual(["a page.tsx · repo/a&b>c and b page.tsx · repo/c&d>e"]);
119 + expect(
120 + assistantSegments(
121 + String.raw`[a](</repo/a\<b &amp;c/page.tsx>) and [b](</repo/d\<e &amp;f/page.tsx>)`,
122 + "/repo",
123 + ),
124 + ).toEqual(["a page.tsx · repo/a<b &c and b page.tsx · repo/d<e &f"]);
125 });
126
127 it("keeps fence paths literal and indexes file-chip labels in user messages", () => {
packages/shared/src/threadFindText.ts
+4 -1
@@ -38,6 +38,7 @@ import {
38 CHAT_MARKDOWN_REMARK_PLUGINS,
39 CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
40 CHAT_USER_MARKDOWN_REMARK_PLUGINS,
41 + decodeMarkdownLinkEscapes,
42 } from "./markdownPipeline.ts";
43
44 // Inline wrappers (including Shiki token spans) must not split a search phrase.
@@ -130,7 +131,9 @@ function markdownThreadFindText(
131 : processor;
132 const tree = renderer.runSync(renderer.parse(markdown), markdown);
133 const paths = [
133 - ...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)),
134 + ...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)).map(
135 + decodeMarkdownLinkEscapes,
136 + ),
137 ...extractInlineCodeSpans(markdown).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),
138 ].flatMap((href) => {
139 const target = resolveMarkdownFileLinkTarget(href, cwd);