Skip to content

Commit 8bac6a9

Browse files
committed
fix(markdown): keep descriptive file labels interactive
1 parent 3a9acc5 commit 8bac6a9

4 files changed

Lines changed: 63 additions & 19 deletions

File tree

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ describe("ChatMarkdown file-link labels", () => {
8787
);
8888
});
8989
const authoredLabel = () =>
90-
renderer!.root.findAllByType("span").find((node) => node.children.includes(label));
90+
renderer!.root
91+
.findAll((node) => typeof node.type === "string" && node.children.includes(label))
92+
.at(0);
9193
expect(authoredLabel()).toBeDefined();
9294
const copied = authoredLabel()!.props["data-markdown-copy"];
9395
await act(async () => {
@@ -101,6 +103,22 @@ describe("ChatMarkdown file-link labels", () => {
101103
}
102104
});
103105

106+
it("copies the filename for a whitespace-only link label", async () => {
107+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
108+
let renderer: ReactTestRenderer | undefined;
109+
try {
110+
await act(async () => {
111+
renderer = create(<ChatMarkdown cwd="/repo" text="[ ](/repo/example.ts:12)" />);
112+
});
113+
expect(renderer!.root.findByType("button").props["data-markdown-copy"]).toBe(
114+
"[example.ts](/repo/example.ts:12)",
115+
);
116+
} finally {
117+
await act(async () => renderer?.unmount());
118+
vi.unstubAllGlobals();
119+
}
120+
});
121+
104122
it("retains descriptive prose, emphasis, destinations, and copy text", async () => {
105123
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
106124
let renderer: ReactTestRenderer | undefined;
@@ -114,9 +132,14 @@ describe("ChatMarkdown file-link labels", () => {
114132
);
115133
});
116134
expect(renderer!.root.findByType("strong").children).toEqual(["validates"]);
135+
expect(
136+
renderer!.root
137+
.findAllByType("button")
138+
.some((button) => button.findAllByType("strong").length > 0),
139+
).toBe(true);
117140
const label = renderer!.root
118-
.findAllByType("span")
119-
.find((node) => node.children.includes(" the input"));
141+
.findAll((node) => typeof node.type === "string" && node.children.includes(" the input"))
142+
.at(0);
120143
expect(label).toBeDefined();
121144
expect(label!.props["data-markdown-copy"]).toBe(
122145
"[validates the input](/repo/src/example.ts:12)",

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ function UncachedShikiCodeBlock({
10831083
}
10841084

10851085
interface MarkdownFileLinkProps {
1086+
children?: ReactNode;
10861087
href: string;
10871088
targetPath: string;
10881089
iconPath: string;
@@ -1782,6 +1783,7 @@ function MarkdownExternalLinkContent({
17821783
}
17831784

17841785
const MarkdownFileLink = memo(function MarkdownFileLink({
1786+
children,
17851787
href,
17861788
targetPath,
17871789
iconPath,
@@ -2064,6 +2066,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20642066
canOpenInPanel,
20652067
});
20662068

2069+
const chip = <FileTagChipContent path={iconPath} label={label} theme={theme} selectable />;
2070+
const content = children ? (
2071+
<>
2072+
{children} (<span className={CHAT_FILE_TAG_CHIP_CLASS_NAME}>{chip}</span>)
2073+
</>
2074+
) : (
2075+
chip
2076+
);
2077+
20672078
return (
20682079
<Tooltip>
20692080
<TooltipTrigger
@@ -2072,7 +2083,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20722083
<a
20732084
href={href}
20742085
className={cn(
2075-
CHAT_FILE_TAG_CHIP_CLASS_NAME,
2086+
children ? "text-left" : CHAT_FILE_TAG_CHIP_CLASS_NAME,
20762087
MARKDOWN_FILE_LINK_CLASS_NAME,
20772088
className,
20782089
)}
@@ -2092,15 +2103,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20922103
}}
20932104
onContextMenu={handleContextMenu}
20942105
>
2095-
<FileTagChipContent path={iconPath} label={label} theme={theme} selectable />
2106+
{content}
20962107
</a>
20972108
) : (
20982109
<button
20992110
type="button"
2100-
aria-label={`File options for ${label}`}
2111+
aria-label={children ? undefined : `File options for ${label}`}
21012112
aria-haspopup="menu"
21022113
className={cn(
2103-
CHAT_FILE_TAG_CHIP_CLASS_NAME,
2114+
children ? "text-left" : CHAT_FILE_TAG_CHIP_CLASS_NAME,
21042115
MARKDOWN_FILE_LINK_CLASS_NAME,
21052116
"select-text",
21062117
className,
@@ -2109,7 +2120,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21092120
onClick={handleContextMenu}
21102121
onContextMenu={handleContextMenu}
21112122
>
2112-
<FileTagChipContent path={iconPath} label={label} theme={theme} selectable />
2123+
{content}
21132124
</button>
21142125
)
21152126
}
@@ -2133,6 +2144,7 @@ function areMarkdownFileLinkPropsEqual(
21332144
next: Readonly<MarkdownFileLinkProps>,
21342145
): boolean {
21352146
return (
2147+
previous.children === next.children &&
21362148
previous.href === next.href &&
21372149
previous.targetPath === next.targetPath &&
21382150
previous.iconPath === next.iconPath &&
@@ -2496,6 +2508,7 @@ function useChatMarkdownState({
24962508
copyMarkdown: string,
24972509
className?: string,
24982510
mediaSource?: string,
2511+
children?: ReactNode,
24992512
) => {
25002513
const parentSuffix = fileLinkParentSuffixByPath.get(
25012514
fileLinkMeta.filePath.replaceAll("\\", "/"),
@@ -2554,7 +2567,9 @@ function useChatMarkdownState({
25542567
: undefined
25552568
}
25562569
className={className}
2557-
/>
2570+
>
2571+
{children}
2572+
</MarkdownFileLink>
25582573
);
25592574
},
25602575
[
@@ -2967,18 +2982,17 @@ const CHAT_MARKDOWN_COMPONENTS = {
29672982
}
29682983

29692984
const label = nodeToPlainText(children);
2970-
const escapedLabel = (label || fileLinkMeta.basename)
2985+
const escapedLabel = (label.trim() ? label : fileLinkMeta.basename)
29712986
.replaceAll("\\", "\\\\")
29722987
.replaceAll("[", "\\[")
29732988
.replaceAll("]", "\\]");
29742989
const copyMarkdown = `[${escapedLabel}](${normalizedHref})`;
2975-
const chip = fileLinkChip(fileLinkMeta, copyMarkdown, props.className, normalizedHref);
2976-
return isMarkdownFileLinkLabel(label, normalizedHref) ? (
2977-
chip
2978-
) : (
2979-
<span data-markdown-copy={copyMarkdown}>
2980-
{children} ({chip})
2981-
</span>
2990+
return fileLinkChip(
2991+
fileLinkMeta,
2992+
copyMarkdown,
2993+
props.className,
2994+
normalizedHref,
2995+
isMarkdownFileLinkLabel(label, normalizedHref) ? undefined : children,
29822996
);
29832997
},
29842998
code: function MarkdownCode({ node, children, className, ...props }) {

‎packages/client-runtime/src/markdownLinks.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ describe("isMarkdownFileLinkLabel", () => {
186186
"preserves authored label %s",
187187
(label) => expect(isMarkdownFileLinkLabel(label, "/repo/src/example.ts:12")).toBe(false),
188188
);
189+
it.each(["favicons", "favicons/", "/tmp/favicons/"])(
190+
"keeps directory label %s compact",
191+
(label) => {
192+
expect(isMarkdownFileLinkLabel(label, "/tmp/favicons/")).toBe(true);
193+
},
194+
);
189195
it("recognizes Windows paths and encoded destinations", () => {
190196
expect(isMarkdownFileLinkLabel("src\\example.ts:12", "C:\\repo\\src\\example.ts:12")).toBe(
191197
true,

‎packages/client-runtime/src/markdownLinks.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ export function isMarkdownFileLinkLabel(label: string, href: string): boolean {
346346
if (!target) return false;
347347
const path = (parseMarkdownFileLink(label)?.path ?? splitFilePathPosition(label.trim()).path)
348348
.replaceAll("\\", "/")
349-
.replace(/^\.\//, "");
350-
const targetPath = target.path.replaceAll("\\", "/");
349+
.replace(/^\.\//, "")
350+
.replace(/([^/:])\/+$/, "$1");
351+
const targetPath = target.path.replaceAll("\\", "/").replace(/([^/:])\/+$/, "$1");
351352
return path === targetPath || targetPath.endsWith(`/${path}`);
352353
}

0 commit comments

Comments
 (0)