Skip to content

Commit e465ff3

Browse files
fix(chat): skip spaced prose paths and quoted anchor text
A prose path ends at whitespace, so a match whose last segment is not file-shaped (no extension, known name or position) is left as text; '/Users/me/Library/Application Support/...' no longer links a truncated path. The raw-anchor counter now scans whole tags and comments with the pattern the anchor placeholder pass already uses, so '</a>' inside a quoted attribute no longer closes the anchor. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dfea90d commit e465ff3

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ export function isWindowsDrivePathHref(href: string): boolean {
442442
return /^[A-Za-z]:[\\/]/.test(safeDecodeURIComponent(href));
443443
}
444444

445-
// Bare prose paths need a filesystem root or a position, so `/api/users.json` stays a route.
445+
// Bare prose paths need a filesystem root or a position, so `/api/users.json` stays a route,
446+
// and a file-shaped last segment.
446447
export function absoluteFilePathMatches(text: string): Array<{ index: number; path: string }> {
447448
const paths: Array<{ index: number; path: string }> = [];
448449
for (const match of text.matchAll(/(^|[\s(["'])(\/[^\s<>"'`]+)/g)) {
@@ -471,6 +472,14 @@ export function absoluteFilePathMatches(text: string): Array<{ index: number; pa
471472
!POSITION_SUFFIX_PATTERN.test(path)
472473
)
473474
continue;
475+
// Prose ends a path at whitespace, so `/Users/me/Library/Application Support/...` must not link.
476+
const basename = path.slice(path.lastIndexOf("/") + 1);
477+
if (
478+
!POSITION_SUFFIX_PATTERN.test(path) &&
479+
!FILE_EXTENSION_PATTERN.test(basename) &&
480+
!EXTENSIONLESS_FILE_NAMES.has(basename)
481+
)
482+
continue;
474483
if (!parseMarkdownFileLink(path)) continue;
475484
paths.push({ index: match.index + match[1]!.length, path });
476485
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,21 @@ it("links complete absolute paths in prose and skips shortened ones", () => {
404404
};
405405
expect(links(`Saved to ${path} and /tmp/other.txt.`)).toEqual([path, "/tmp/other.txt"]);
406406
expect(links(`See (${path}:12).`)).toEqual([`${path}:12`]);
407-
expect(links("See /tmp/report(1). and /tmp/app/[id]/page.tsx.")).toEqual([
408-
"/tmp/report(1)",
407+
expect(links("See /tmp/report(1).txt. and /tmp/app/[id]/page.tsx.")).toEqual([
408+
"/tmp/report(1).txt",
409409
"/tmp/app/[id]/page.tsx",
410410
]);
411411
expect(links("(see /tmp/claude-1000/...). and /tmp/claude-1000/….")).toEqual([]);
412412
expect(links("Served at /index.html; GET /api/users.json; see /tmp/x.md:3.")).toEqual([
413413
"/tmp/x.md:3",
414414
]);
415415
expect(
416-
links('<a href="https://example.com">/tmp/notes.md</a> and **<a href="x">/tmp/a.md</a>**'),
416+
links(
417+
'<a title="</a>" href="https://example.com">/tmp/notes.md</a> and **<a href="x">/tmp/a.md</a>**',
418+
),
419+
).toEqual([]);
420+
expect(
421+
links("Config lives in /Users/me/Library/Application Support/T3 Code/settings.json now."),
417422
).toEqual([]);
418423
expect(links(`/chat/settings and https://example.com${path}`)).toEqual([
419424
`https://example.com${path}`,

‎packages/shared/src/markdownPipeline.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ function markStandaloneImages(node: MarkdownImageHastNode) {
7171
});
7272
}
7373

74+
// Whole tags, attributes, and comments, so closing-anchor text inside them never counts as a tag.
75+
const RAW_HTML_TAG_PATTERN = /<!--[\s\S]*?(?:-->|$)|<\/?[A-Za-z](?:[^"'<>]|"[^"]*"|'[^']*')*>/g;
76+
7477
function rehypePreserveBareAnchorPlaceholders() {
7578
return (tree: MarkdownImageHastNode) => {
7679
const anchors: Array<MarkdownImageHastNode | null> = [];
7780
let rawTextTag: string | undefined;
7881
const visit = (node: MarkdownImageHastNode) => {
7982
if (node.type === "raw" && typeof node.value === "string") {
80-
// Scan whole tags, attributes, and comments so embedded closing-anchor text cannot match a placeholder.
81-
const tags = /<!--[\s\S]*?(?:-->|$)|<\/?[A-Za-z](?:[^"'<>]|"[^"]*"|'[^']*')*>/g;
83+
const tags = new RegExp(RAW_HTML_TAG_PATTERN);
8284
let offset = 0;
8385
while (rawTextTag !== "plaintext") {
8486
// Raw text ends at its closing tag even inside comment-looking text.
@@ -459,12 +461,10 @@ function remarkLinkAbsoluteFilePaths(): Transformer<Root> {
459461
if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;
460462
node.children = node.children.flatMap((child): typeof node.children => {
461463
if (child.type === "html") {
462-
rawAnchorDepth = Math.max(
463-
0,
464-
rawAnchorDepth +
465-
(child.value.match(/<a[\s>]/gi)?.length ?? 0) -
466-
(child.value.match(/<\/a\s*>/gi)?.length ?? 0),
467-
);
464+
for (const [tag] of child.value.matchAll(RAW_HTML_TAG_PATTERN)) {
465+
if (/^<a[\s/>]/i.test(tag)) rawAnchorDepth += 1;
466+
else if (/^<\/a[\s>]/i.test(tag)) rawAnchorDepth = Math.max(0, rawAnchorDepth - 1);
467+
}
468468
}
469469
if (child.type !== "text" || rawAnchorDepth > 0) {
470470
visit(child);

0 commit comments

Comments
 (0)