TypeScript
fix(web): punctuation after a file chip stays on the chip's line (#26871)
Pawełfd08b170b6be
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex acf24ba40b7..93e182fb408 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -206,6 +206,7 @@ import {import { MarkdownExternalLinkContent } from "./chat/MarkdownExternalLink";import { MarkdownFileLink } from "./chat/MarkdownFileLink";import { canUseMarkdownFileShellActions } from "./chat/MarkdownFileLink.logic";+import { rehypeChipPunctuation } from "./chat/markdownChipPunctuation";import { handleMarkdownFragmentClick, rehypeHeadingIds } from "./chat/markdownFragments";import { rehypeTextDirection } from "./chat/markdownTextDirection";import { hastHasText, hastPlainTextDeep, plainHastText } from "./chat/markdownHast.logic";@@ -391,11 +392,13 @@ const CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS = [...CHAT_MARKDOWN_REHYPE_PLUGINS,rehypeHeadingIds,rehypeTextDirection,+ rehypeChipPunctuation,] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [rehypeHeadingIds,rehypeTextDirection,+ rehypeChipPunctuation,] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;function markdownUrlTransform(href: string) {@@ -1323,7 +1326,8 @@ const CHAT_MARKDOWN_COMPONENTS = {chip) : (<span data-markdown-copy={copyMarkdown}>- {children} {chip}+ <span className="whitespace-normal">{children} </span>+ {chip}</span>);},diff --git a/apps/web/src/components/chat/markdownChipPunctuation.test.ts b/apps/web/src/components/chat/markdownChipPunctuation.test.tsnew file mode 100644index 00000000000..9f09ffbf804--- /dev/null+++ b/apps/web/src/components/chat/markdownChipPunctuation.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from "vite-plus/test";++import { rehypeChipPunctuation } from "./markdownChipPunctuation";++const text = (value: string) => ({ type: "text", value });+const element = (tagName: string, children: object[]) => ({+ type: "element",+ tagName,+ properties: {} as Record<string, unknown>,+ children,+});++function paired(block: ReturnType<typeof element>) {+ rehypeChipPunctuation()({ type: "root", children: [block] });+ return block.children;+}++describe("rehypeChipPunctuation", () => {+ it("pairs a link with the punctuation that follows it", () => {+ const link = element("a", [text("a.ts")]);+ expect(paired(element("p", [text("See "), link, text("... next")]))).toEqual([+ text("See "),+ expect.objectContaining({ tagName: "span", children: [link, text("...")] }),+ text(" next"),+ ]);+ });++ it("leaves code blocks alone", () => {+ const children = [element("code", [text("a.ts")]), text(".")];+ expect(paired(element("pre", [...children]))).toEqual(children);+ });+});diff --git a/apps/web/src/components/chat/markdownChipPunctuation.ts b/apps/web/src/components/chat/markdownChipPunctuation.tsnew file mode 100644index 00000000000..67d78a52479--- /dev/null+++ b/apps/web/src/components/chat/markdownChipPunctuation.ts@@ -0,0 +1,41 @@+type HastNode = {+ type?: string;+ tagName?: string;+ value?: string;+ properties?: Record<string, unknown>;+ children?: HastNode[];+};++const LEADING_PUNCTUATION = /^[.,:;!?…)\]}"'’”]+/;++// Browsers allow a wrap after inline-flex chips even before punctuation.+export function rehypeChipPunctuation() {+ const visit = (node: HastNode): void => {+ if (node.tagName === "pre" || !node.children) return;+ const children: HastNode[] = [];+ for (const child of node.children) {+ visit(child);+ const previous = children.at(-1);+ const punctuation =+ child.type === "text" && (previous?.tagName === "a" || previous?.tagName === "code")+ ? LEADING_PUNCTUATION.exec(child.value ?? "")?.[0]+ : undefined;+ if (!previous || !punctuation) {+ children.push(child);+ continue;+ }+ children[children.length - 1] = {+ type: "element",+ tagName: "span",+ properties: {+ className: ["has-[[data-slot=context-chip-label]]:whitespace-nowrap"],+ },+ children: [previous, { type: "text", value: punctuation }],+ };+ const rest = (child.value ?? "").slice(punctuation.length);+ if (rest) children.push({ ...child, value: rest });+ }+ node.children = children;+ };+ return visit;+}