Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(markdown): avoid duplicate spacing after table breaks

Afloatf2f529cf921e
diff --git a/src/components/ChatMarkdown.table-breaks.test.ts b/src/components/ChatMarkdown.table-breaks.test.ts
index ce1267c354..2d9bb054ca 100644
--- a/src/components/ChatMarkdown.table-breaks.test.ts
+++ b/src/components/ChatMarkdown.table-breaks.test.ts
@@ -20,6 +20,7 @@ const render = (text: string) => {
it.each(["<br>", "<br/>", "<br />", "<BR>"])("renders %s as a line break inside a table cell", (tag) => {
const cell = render(table(`Score: 8.8 / 10${tag}Details`)).querySelector("td")!;
expect(cell.querySelectorAll("br")).toHaveLength(1);
+ expect(cell.querySelector("br")?.nextSibling?.textContent).toBe("Details");
expect(cell.textContent).not.toContain(tag);
expect(cell.textContent).toContain("Score: 8.8 / 10");
expect(cell.textContent).toContain("Details");
@@ -28,6 +29,7 @@ it.each(["<br>", "<br/>", "<br />", "<BR>"])("renders %s as a line break inside
it("also breaks a table heading without losing its emphasis", () => {
const cell = render("| **Score**<br>out of ten |\n| --- |\n| 8.8 |").querySelector("th")!;
expect(cell.querySelectorAll("br")).toHaveLength(1);
+ expect(cell.querySelector("br")?.nextSibling?.textContent).toBe("out of ten");
expect(cell.querySelector("strong")?.textContent).toBe("Score");
expect(cell.textContent).toContain("out of ten");
});
@@ -35,6 +37,7 @@ it("also breaks a table heading without losing its emphasis", () => {
it("breaks inside emphasis while keeping that emphasis in the cell", () => {
const strong = render(table("**First<br>Second**")).querySelector("td strong")!;
expect(strong.querySelectorAll("br")).toHaveLength(1);
+ expect(strong.querySelector("br")?.nextSibling?.textContent).toBe("Second");
expect(strong.textContent).toContain("Second");
});
@@ -53,6 +56,12 @@ it("keeps HTML outside table cells as source text", () => {
expect(host.textContent).toBe("Before<br>After");
});
+it("keeps the existing rendering of Markdown line breaks outside tables", () => {
+ const host = render("Before \nAfter");
+ expect(host.querySelectorAll("br")).toHaveLength(1);
+ expect(host.querySelector("br")?.nextSibling?.textContent).toBe("\nAfter");
+});
+
it.each([
["First<br>Second", "First\nSecond"],
["First<br/><br/>Second", "First\n\nSecond"],
@@ -66,6 +75,9 @@ it.each([
const root = createRoot(host);
try {
await act(async () => root.render(createElement(ChatMarkdown, { text: table(source) })));
+ const content = host.querySelector("td .whitespace-pre-wrap")!.cloneNode(true) as HTMLElement;
+ for (const br of content.querySelectorAll("br")) br.replaceWith(document.createTextNode("\n"));
+ expect(content.textContent).toBe(copied);
const button = host.querySelector<HTMLButtonElement>('button[aria-label="Copy table"]');
expect(button).not.toBeNull();
await act(async () => button!.click());
diff --git a/src/components/MarkdownTable.tsx b/src/components/MarkdownTable.tsx
index 9333c0f5ad..e002aa85e7 100644
--- a/src/components/MarkdownTable.tsx
+++ b/src/components/MarkdownTable.tsx
@@ -1,4 +1,4 @@
-import { Children, isValidElement, useMemo, type ReactElement, type ReactNode } from "react";
+import { Children, cloneElement, isValidElement, useMemo, type ReactElement, type ReactNode } from "react";
import { RichTable, type TableCell, type TableColumn } from "./RichTable";
type Element = ReactElement<{ children?: ReactNode; align?: string; alt?: string }>;
@@ -16,13 +16,24 @@ function plain(node: ReactNode): string {
return plain(element.props.children) || element.props.alt || "";
}
+function cellContent(node: ReactNode): ReactNode {
+ if (Array.isArray(node)) return node.map((child, index) => {
+ const previous = node[index - 1];
+ // Wrapped cells preserve whitespace, so a break's formatting newline would add an extra visible line.
+ return typeof child === "string" && isValidElement(previous) && previous.type === "br" ? child.replace(/^\n/, "") : cellContent(child);
+ });
+ if (!isValidElement(node)) return node;
+ const element = node as Element;
+ return element.props.children === undefined ? element : cloneElement(element, { children: cellContent(element.props.children) });
+}
+
/** Security: keeps react-markdown's already-sanitized cell children; never renders model-authored raw HTML. */
export function MarkdownTable({ children, direction }: { children?: ReactNode; direction?: "rtl" | "ltr" }) {
const data = useMemo(() => {
const sections = elements(children);
const head = sections.find((section) => section.type === "thead");
const body = sections.find((section) => section.type === "tbody");
- const cell = (element: Element): TableCell => ({ text: plain(element.props.children), content: element.props.children });
+ const cell = (element: Element): TableCell => ({ text: plain(element.props.children), content: cellContent(element.props.children) });
const columns: TableColumn[] = elements(elements(head?.props.children)[0]?.props.children).map((element) => ({
...cell(element), align: element.props.align === "left" || element.props.align === "right" || element.props.align === "center" ? element.props.align : undefined,
}));