Skip to content

Commit fd08b17

Browse files
kendyl93claudemaria-rcks
authored
fix(web): punctuation after a file chip stays on the chip's line (#26871)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 1d690b4 commit fd08b17

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ import {
206206
import { MarkdownExternalLinkContent } from "./chat/MarkdownExternalLink";
207207
import { MarkdownFileLink } from "./chat/MarkdownFileLink";
208208
import { canUseMarkdownFileShellActions } from "./chat/MarkdownFileLink.logic";
209+
import { rehypeChipPunctuation } from "./chat/markdownChipPunctuation";
209210
import { handleMarkdownFragmentClick, rehypeHeadingIds } from "./chat/markdownFragments";
210211
import { rehypeTextDirection } from "./chat/markdownTextDirection";
211212
import { hastHasText, hastPlainTextDeep, plainHastText } from "./chat/markdownHast.logic";
@@ -391,11 +392,13 @@ const CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS = [
391392
...CHAT_MARKDOWN_REHYPE_PLUGINS,
392393
rehypeHeadingIds,
393394
rehypeTextDirection,
395+
rehypeChipPunctuation,
394396
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
395397

396398
const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [
397399
rehypeHeadingIds,
398400
rehypeTextDirection,
401+
rehypeChipPunctuation,
399402
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
400403

401404
function markdownUrlTransform(href: string) {
@@ -1323,7 +1326,8 @@ const CHAT_MARKDOWN_COMPONENTS = {
13231326
chip
13241327
) : (
13251328
<span data-markdown-copy={copyMarkdown}>
1326-
{children} {chip}
1329+
<span className="whitespace-normal">{children} </span>
1330+
{chip}
13271331
</span>
13281332
);
13291333
},
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { rehypeChipPunctuation } from "./markdownChipPunctuation";
4+
5+
const text = (value: string) => ({ type: "text", value });
6+
const element = (tagName: string, children: object[]) => ({
7+
type: "element",
8+
tagName,
9+
properties: {} as Record<string, unknown>,
10+
children,
11+
});
12+
13+
function paired(block: ReturnType<typeof element>) {
14+
rehypeChipPunctuation()({ type: "root", children: [block] });
15+
return block.children;
16+
}
17+
18+
describe("rehypeChipPunctuation", () => {
19+
it("pairs a link with the punctuation that follows it", () => {
20+
const link = element("a", [text("a.ts")]);
21+
expect(paired(element("p", [text("See "), link, text("... next")]))).toEqual([
22+
text("See "),
23+
expect.objectContaining({ tagName: "span", children: [link, text("...")] }),
24+
text(" next"),
25+
]);
26+
});
27+
28+
it("leaves code blocks alone", () => {
29+
const children = [element("code", [text("a.ts")]), text(".")];
30+
expect(paired(element("pre", [...children]))).toEqual(children);
31+
});
32+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
type HastNode = {
2+
type?: string;
3+
tagName?: string;
4+
value?: string;
5+
properties?: Record<string, unknown>;
6+
children?: HastNode[];
7+
};
8+
9+
const LEADING_PUNCTUATION = /^[.,:;!?…)\]}"'’”]+/;
10+
11+
// Browsers allow a wrap after inline-flex chips even before punctuation.
12+
export function rehypeChipPunctuation() {
13+
const visit = (node: HastNode): void => {
14+
if (node.tagName === "pre" || !node.children) return;
15+
const children: HastNode[] = [];
16+
for (const child of node.children) {
17+
visit(child);
18+
const previous = children.at(-1);
19+
const punctuation =
20+
child.type === "text" && (previous?.tagName === "a" || previous?.tagName === "code")
21+
? LEADING_PUNCTUATION.exec(child.value ?? "")?.[0]
22+
: undefined;
23+
if (!previous || !punctuation) {
24+
children.push(child);
25+
continue;
26+
}
27+
children[children.length - 1] = {
28+
type: "element",
29+
tagName: "span",
30+
properties: {
31+
className: ["has-[[data-slot=context-chip-label]]:whitespace-nowrap"],
32+
},
33+
children: [previous, { type: "text", value: punctuation }],
34+
};
35+
const rest = (child.value ?? "").slice(punctuation.length);
36+
if (rest) children.push({ ...child, value: rest });
37+
}
38+
node.children = children;
39+
};
40+
return visit;
41+
}

0 commit comments

Comments
 (0)