Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(chat): render GFM tables in assistant markdown (#2131)

Joshuaf6d81558cd12
diff --git a/package-lock.json b/package-lock.json
index 39ae89b51..acd9e1d04 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -65,6 +65,7 @@
"react-dom": "^19.1.0",
"react-i18next": "^17.0.10",
"react-markdown": "^10.1.0",
+ "remark-gfm": "^4.0.1",
"shadcn-ui": "^0.9.5",
"tailwind-merge": "^3.3.1",
"tar": "^7.5.22",
@@ -9945,6 +9946,16 @@
"integrity": "sha512-TxFAc76Jnhb2OUu+n3yz9RMu4CwGfaT788br6HhEDlvWfdeJcLUsxk1Hgw2yJio0OXsxv7pyIPmvECY7bMbluA==",
"license": "ISC"
},
+ "node_modules/markdown-table": {
+ "version": "3.0.4",
+ "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.4.tgz",
+ "integrity": "sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/wooorm"
+ }
+ },
"node_modules/matcher": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/matcher/-/matcher-4.0.0.tgz",
@@ -9970,6 +9981,34 @@
"node": ">= 0.4"
}
},
+ "node_modules/mdast-util-find-and-replace": {
+ "version": "3.0.2",
+ "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.2.tgz",
+ "integrity": "sha512-Tmd1Vg/m3Xz43afeNxDIhWRtFZgM2VLyaf4vSTYwudTyeuTneoL3qtWMA5jeLyz/O1vDJmmV4QuScFCA2tBPwg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "escape-string-regexp": "^5.0.0",
+ "unist-util-is": "^6.0.0",
+ "unist-util-visit-parents": "^6.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-find-and-replace/node_modules/escape-string-regexp": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
+ "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
"node_modules/mdast-util-from-markdown": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-2.0.3.tgz",
@@ -9994,6 +10033,107 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/mdast-util-gfm": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm/-/mdast-util-gfm-3.1.0.tgz",
+ "integrity": "sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==",
+ "license": "MIT",
+ "dependencies": {
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-gfm-autolink-literal": "^2.0.0",
+ "mdast-util-gfm-footnote": "^2.0.0",
+ "mdast-util-gfm-strikethrough": "^2.0.0",
+ "mdast-util-gfm-table": "^2.0.0",
+ "mdast-util-gfm-task-list-item": "^2.0.0",
+ "mdast-util-to-markdown": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-gfm-autolink-literal": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-2.0.1.tgz",
+ "integrity": "sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "ccount": "^2.0.0",
+ "devlop": "^1.0.0",
+ "mdast-util-find-and-replace": "^3.0.0",
+ "micromark-util-character": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-gfm-footnote": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-2.1.0.tgz",
+ "integrity": "sha512-sqpDWlsHn7Ac9GNZQMeUzPQSMzR6Wv0WKRNvQRg0KqHh02fpTz69Qc1QSseNX29bhz1ROIyNyxExfawVKTm1GQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "devlop": "^1.1.0",
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-to-markdown": "^2.0.0",
+ "micromark-util-normalize-identifier": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-gfm-strikethrough": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-2.0.0.tgz",
+ "integrity": "sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-to-markdown": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-gfm-table": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm-table/-/mdast-util-gfm-table-2.0.0.tgz",
+ "integrity": "sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "devlop": "^1.0.0",
+ "markdown-table": "^3.0.0",
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-to-markdown": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/mdast-util-gfm-task-list-item": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-2.0.0.tgz",
+ "integrity": "sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "devlop": "^1.0.0",
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-to-markdown": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/mdast-util-mdx-expression": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/mdast-util-mdx-expression/-/mdast-util-mdx-expression-2.0.1.tgz",
@@ -10198,6 +10338,127 @@
"micromark-util-types": "^2.0.0"
}
},
+ "node_modules/micromark-extension-gfm": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm/-/micromark-extension-gfm-3.0.0.tgz",
+ "integrity": "sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==",
+ "license": "MIT",
+ "dependencies": {
+ "micromark-extension-gfm-autolink-literal": "^2.0.0",
+ "micromark-extension-gfm-footnote": "^2.0.0",
+ "micromark-extension-gfm-strikethrough": "^2.0.0",
+ "micromark-extension-gfm-table": "^2.0.0",
+ "micromark-extension-gfm-tagfilter": "^2.0.0",
+ "micromark-extension-gfm-task-list-item": "^2.0.0",
+ "micromark-util-combine-extensions": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-autolink-literal": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-autolink-literal/-/micromark-extension-gfm-autolink-literal-2.1.0.tgz",
+ "integrity": "sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==",
+ "license": "MIT",
+ "dependencies": {
+ "micromark-util-character": "^2.0.0",
+ "micromark-util-sanitize-uri": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-footnote": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-footnote/-/micromark-extension-gfm-footnote-2.1.0.tgz",
+ "integrity": "sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==",
+ "license": "MIT",
+ "dependencies": {
+ "devlop": "^1.0.0",
+ "micromark-core-commonmark": "^2.0.0",
+ "micromark-factory-space": "^2.0.0",
+ "micromark-util-character": "^2.0.0",
+ "micromark-util-normalize-identifier": "^2.0.0",
+ "micromark-util-sanitize-uri": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-strikethrough": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-strikethrough/-/micromark-extension-gfm-strikethrough-2.1.0.tgz",
+ "integrity": "sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==",
+ "license": "MIT",
+ "dependencies": {
+ "devlop": "^1.0.0",
+ "micromark-util-chunked": "^2.0.0",
+ "micromark-util-classify-character": "^2.0.0",
+ "micromark-util-resolve-all": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-table": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-table/-/micromark-extension-gfm-table-2.1.1.tgz",
+ "integrity": "sha512-t2OU/dXXioARrC6yWfJ4hqB7rct14e8f7m0cbI5hUmDyyIlwv5vEtooptH8INkbLzOatzKuVbQmAYcbWoyz6Dg==",
+ "license": "MIT",
+ "dependencies": {
+ "devlop": "^1.0.0",
+ "micromark-factory-space": "^2.0.0",
+ "micromark-util-character": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-tagfilter": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-tagfilter/-/micromark-extension-gfm-tagfilter-2.0.0.tgz",
+ "integrity": "sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==",
+ "license": "MIT",
+ "dependencies": {
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-gfm-task-list-item": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-gfm-task-list-item/-/micromark-extension-gfm-task-list-item-2.1.0.tgz",
+ "integrity": "sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==",
+ "license": "MIT",
+ "dependencies": {
+ "devlop": "^1.0.0",
+ "micromark-factory-space": "^2.0.0",
+ "micromark-util-character": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/micromark-factory-destination": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/micromark-factory-destination/-/micromark-factory-destination-2.0.1.tgz",
@@ -11858,6 +12119,24 @@
"node": ">= 6"
}
},
+ "node_modules/remark-gfm": {
+ "version": "4.0.1",
+ "resolved": "https://registry.npmjs.org/remark-gfm/-/remark-gfm-4.0.1.tgz",
+ "integrity": "sha512-1quofZ2RQ9EWdeN34S79+KExV1764+wCUGop5CPL1WGdD0ocPpu91lzPGbwWMECpEpd42kJGQwzRfyov9j4yNg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "mdast-util-gfm": "^3.0.0",
+ "micromark-extension-gfm": "^3.0.0",
+ "remark-parse": "^11.0.0",
+ "remark-stringify": "^11.0.0",
+ "unified": "^11.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/remark-parse": {
"version": "11.0.0",
"resolved": "https://registry.npmjs.org/remark-parse/-/remark-parse-11.0.0.tgz",
@@ -11891,6 +12170,21 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/remark-stringify": {
+ "version": "11.0.0",
+ "resolved": "https://registry.npmjs.org/remark-stringify/-/remark-stringify-11.0.0.tgz",
+ "integrity": "sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "mdast-util-to-markdown": "^2.0.0",
+ "unified": "^11.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
diff --git a/package.json b/package.json
index fccbf9442..94bf79cea 100644
--- a/package.json
+++ b/package.json
@@ -177,6 +177,7 @@
"react-dom": "^19.1.0",
"react-i18next": "^17.0.10",
"react-markdown": "^10.1.0",
+ "remark-gfm": "^4.0.1",
"shadcn-ui": "^0.9.5",
"tailwind-merge": "^3.3.1",
"tar": "^7.5.22",
diff --git a/src/components/dictation/AssistantPanel.tsx b/src/components/dictation/AssistantPanel.tsx
index bbf809b1e..c66e8626b 100644
--- a/src/components/dictation/AssistantPanel.tsx
+++ b/src/components/dictation/AssistantPanel.tsx
@@ -81,10 +81,7 @@ interface AssistantPanelProps {
onSelectionContextChange: (context: AgentSelectionContext | null) => void;
}
-// Updating the selection indicator must not rerender react-markdown: its
-// component map is recreated on render, which remounts the text nodes and
-// collapses the browser's live selection. Streaming content still rerenders
-// normally because the content prop changes.
+// Avoid reparsing Markdown when only the selection indicator changes.
const StableAssistantMarkdown = memo(MarkdownRenderer);
export function AssistantPanel({
diff --git a/src/components/ui/MarkdownRenderer.tsx b/src/components/ui/MarkdownRenderer.tsx
index 41bc718ce..db4dd1049 100644
--- a/src/components/ui/MarkdownRenderer.tsx
+++ b/src/components/ui/MarkdownRenderer.tsx
@@ -1,61 +1,112 @@
-import Markdown from "react-markdown";
-import { cn } from "../lib/utils";
+import { createContext, useContext, useId, type ReactElement } from "react";
+import Markdown, { type Components } from "react-markdown";
+import remarkGfm from "remark-gfm";
interface MarkdownRendererProps {
content: string;
className?: string;
}
-export function MarkdownRenderer({ content, className }: MarkdownRendererProps) {
- return (
- <div dir="auto" className={cn("prose prose-sm max-w-none", className)}>
- <Markdown
- components={{
- h1: ({ children }) => (
- <h1 className="text-lg font-bold mb-2 mt-3 first:mt-0">{children}</h1>
- ),
- h2: ({ children }) => (
- <h2 className="text-base font-semibold mb-2 mt-3 first:mt-0">{children}</h2>
- ),
- h3: ({ children }) => (
- <h3 className="text-sm font-semibold mb-1.5 mt-2 first:mt-0">{children}</h3>
- ),
- p: ({ children }) => <p className="mb-2 last:mb-0">{children}</p>,
- ul: ({ children }) => <ul className="list-disc ps-4 mb-2 space-y-1">{children}</ul>,
- ol: ({ children }) => <ol className="list-decimal ps-4 mb-2 space-y-1">{children}</ol>,
- li: ({ children }) => <li className="ps-1">{children}</li>,
- a: ({ href, children }) => (
- <a
- href={href}
- target="_blank"
- rel="noopener noreferrer"
- className="text-link underline decoration-link/30 hover:decoration-link/60 transition-colors"
- >
- {children}
- </a>
- ),
- code: ({ children }) => (
- <code dir="ltr" className="bg-black/10 px-1 py-0.5 rounded text-xs font-mono">
- {children}
- </code>
- ),
- pre: ({ children }) => (
- <pre dir="ltr" className="bg-black/10 p-2 rounded overflow-x-auto text-xs mb-2">
- {children}
- </pre>
- ),
- strong: ({ children }) => <strong className="font-semibold">{children}</strong>,
- em: ({ children }) => <em className="italic">{children}</em>,
- blockquote: ({ children }) => (
- <blockquote className="border-s-2 border-current/30 ps-3 italic my-2">
- {children}
- </blockquote>
- ),
- hr: () => <hr className="border-current/20 my-3" />,
- }}
+const FootnotePrefixContext = createContext("");
+
+// Stable component types preserve DOM state, including table scroll positions.
+const markdownComponents: Components = {
+ h1: ({ children }): ReactElement => (
+ <h1 className="text-lg font-bold mb-2 mt-3 first:mt-0">{children}</h1>
+ ),
+ h2: function MarkdownHeading({ children, id, className }): ReactElement {
+ const prefix = useContext(FootnotePrefixContext);
+ return (
+ <h2
+ id={id === "footnote-label" ? `${prefix}footnote-label` : id}
+ className={className ?? "text-base font-semibold mb-2 mt-3 first:mt-0"}
>
- {content}
- </Markdown>
+ {children}
+ </h2>
+ );
+ },
+ h3: ({ children }): ReactElement => (
+ <h3 className="text-sm font-semibold mb-1.5 mt-2 first:mt-0">{children}</h3>
+ ),
+ p: ({ children }): ReactElement => <p className="mb-2 last:mb-0">{children}</p>,
+ ul: ({ children }): ReactElement => <ul className="list-disc ps-4 mb-2 space-y-1">{children}</ul>,
+ ol: ({ children }): ReactElement => (
+ <ol className="list-decimal ps-4 mb-2 space-y-1">{children}</ol>
+ ),
+ li: ({ children, id }): ReactElement => (
+ <li id={id} className="ps-1">
+ {children}
+ </li>
+ ),
+ a: function MarkdownLink({ node: _node, ...props }): ReactElement {
+ const prefix = useContext(FootnotePrefixContext);
+ return (
+ <a
+ {...props}
+ aria-describedby={
+ props["aria-describedby"] === "footnote-label"
+ ? `${prefix}footnote-label`
+ : props["aria-describedby"]
+ }
+ target={props.href?.startsWith("#") ? undefined : "_blank"}
+ rel="noopener noreferrer"
+ className="text-link underline decoration-link/30 hover:decoration-link/60 transition-colors"
+ />
+ );
+ },
+ code: ({ children }): ReactElement => (
+ <code dir="ltr" className="bg-black/10 px-1 py-0.5 rounded text-xs font-mono">
+ {children}
+ </code>
+ ),
+ pre: ({ children }): ReactElement => (
+ <pre dir="ltr" className="bg-black/10 p-2 rounded overflow-x-auto text-xs mb-2">
+ {children}
+ </pre>
+ ),
+ strong: ({ children }): ReactElement => <strong className="font-semibold">{children}</strong>,
+ em: ({ children }): ReactElement => <em className="italic">{children}</em>,
+ blockquote: ({ children }): ReactElement => (
+ <blockquote className="border-s-2 border-current/30 ps-3 italic my-2">{children}</blockquote>
+ ),
+ hr: (): ReactElement => <hr className="border-current/20 my-3" />,
+ table: ({ children }): ReactElement => (
+ <div className="overflow-x-auto mb-2">
+ <table className="w-full text-xs border-collapse">{children}</table>
+ </div>
+ ),
+ thead: ({ children }): ReactElement => (
+ <thead className="border-b border-current/20">{children}</thead>
+ ),
+ tr: ({ children }): ReactElement => (
+ <tr className="border-b border-current/10 last:border-0">{children}</tr>
+ ),
+ // style carries the GFM column alignment (:--, :-:, --:).
+ th: ({ children, style }): ReactElement => (
+ <th style={style} className="px-2 py-1.5 text-start font-semibold align-top">
+ {children}
+ </th>
+ ),
+ td: ({ children, style }): ReactElement => (
+ <td style={style} className="px-2 py-1.5 align-top">
+ {children}
+ </td>
+ ),
+};
+
+export function MarkdownRenderer({ content, className }: MarkdownRendererProps): ReactElement {
+ const footnotePrefix = `user-content-${useId()}-`;
+ return (
+ <div dir="auto" className={className}>
+ <FootnotePrefixContext value={footnotePrefix}>
+ <Markdown
+ remarkPlugins={[remarkGfm]}
+ remarkRehypeOptions={{ clobberPrefix: footnotePrefix }}
+ components={markdownComponents}
+ >
+ {content}
+ </Markdown>
+ </FootnotePrefixContext>
</div>
);
}
diff --git a/test/components/markdownRenderer.test.js b/test/components/markdownRenderer.test.js
new file mode 100644
index 000000000..cbc7ac127
--- /dev/null
+++ b/test/components/markdownRenderer.test.js
@@ -0,0 +1,168 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const { createElement } = require("react");
+const { renderToStaticMarkup } = require("react-dom/server");
+const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");
+
+// Chat answers routinely come back as GFM tables. react-markdown follows
+// CommonMark, which has no table syntax, so without remark-gfm the whole table
+// collapsed into one paragraph of literal pipes.
+const TABLE_MARKDOWN = [
+ "| Theme | Note | Meaning |",
+ "|:------|:----:|--------:|",
+ "| **Activation** | 63 | Week-one drop-off |",
+ "| Accuracy | 65 | Four distinct causes |",
+ "",
+].join("\n");
+
+async function renderMarkdown(t, content) {
+ installBrowserGlobals(t);
+ const vite = await createRendererServer(t, {
+ cachePrefix: "openwhispr-markdown-renderer-test-",
+ });
+ const mod = await vite.ssrLoadModule("/components/ui/MarkdownRenderer.tsx");
+ if (Array.isArray(content)) {
+ return renderToStaticMarkup(
+ createElement(
+ "div",
+ null,
+ ...content.map((message) => createElement(mod.default, { content: message }))
+ )
+ );
+ }
+ return renderToStaticMarkup(createElement(mod.default, { content }));
+}
+
+test("footnote references and backlinks reach accessible in-document destinations", async (t) => {
+ const html = await renderMarkdown(t, "Claim.[^source] Again.[^source]\n\n[^source]: Evidence.");
+ const ids = new Set([...html.matchAll(/\bid="([^"]+)"/g)].map((match) => match[1]));
+ const links = [...html.matchAll(/<a\b[^>]*href="#[^"]+"[^>]*>/g)].map((match) => match[0]);
+
+ assert.equal(links.length, 4, "two references and their return links");
+ for (const link of links) {
+ const destination = link.match(/href="#([^"]+)"/)[1];
+ assert.ok(ids.has(destination), `destination ${destination} exists`);
+ assert.ok(!link.includes('target="_blank"'), "footnotes stay in the document");
+ }
+ const label = html.match(/<h2\b[^>]*id="([^"]+)"[^>]*class="[^"]*sr-only[^>]*>/);
+ assert.ok(label, "the footnote heading keeps its accessible ID and hidden styling");
+ assert.ok(html.includes(`aria-describedby="${label[1]}"`));
+ assert.ok(html.includes("data-footnote-ref"));
+ assert.ok(html.includes("data-footnote-backref"));
+ assert.ok(html.includes('aria-label="Back to reference'));
+});
+
+test("separate messages with the same footnote label have distinct destinations", async (t) => {
+ const content = "Claim.[^source]\n\n[^source]: Evidence.";
+ const html = await renderMarkdown(t, [content, content]);
+ const ids = [...html.matchAll(/\bid="([^"]+)"/g)].map((match) => match[1]);
+
+ assert.equal(ids.length, 6, "each message has a reference, definition and heading ID");
+ assert.equal(new Set(ids).size, ids.length, "messages never share a destination ID");
+});
+
+test("external links still open separately with safe relationship attributes", async (t) => {
+ const html = await renderMarkdown(t, "[Website](https://openwhispr.com)");
+ assert.ok(html.includes('href="https://openwhispr.com"'));
+ assert.ok(html.includes('target="_blank"'));
+ assert.ok(html.includes('rel="noopener noreferrer"'));
+});
+
+test("a GFM table renders as a real table, not literal pipes", async (t) => {
+ const html = await renderMarkdown(t, TABLE_MARKDOWN);
+
+ assert.ok(html.includes("<table"), "renders a table element");
+ assert.ok(html.includes("<th"), "renders header cells");
+ assert.ok(
+ /<th[^>]*text-start/.test(html),
+ "header cells align to the writing direction, not a physical edge"
+ );
+ assert.equal((html.match(/<tr/g) || []).length, 3, "one header row plus two body rows");
+ assert.ok(!html.includes("|"), "no literal pipe survives into the output");
+});
+
+test("column alignment from the separator row is preserved", async (t) => {
+ const html = await renderMarkdown(t, TABLE_MARKDOWN);
+
+ assert.ok(html.includes("text-align:left"), "left-aligned column");
+ assert.ok(html.includes("text-align:center"), "centre-aligned column");
+ assert.ok(html.includes("text-align:right"), "right-aligned column");
+});
+
+test("a wide table scrolls instead of stretching its container", async (t) => {
+ const html = await renderMarkdown(t, TABLE_MARKDOWN);
+
+ assert.ok(html.includes("overflow-x-auto"), "table sits in a scrollable wrapper");
+});
+
+test("Markdown component types stay stable across streaming and parent rerenders", async (t) => {
+ installBrowserGlobals(t);
+ const vite = await createRendererServer(t, {
+ cachePrefix: "openwhispr-markdown-reconciliation-test-",
+ });
+ const { MarkdownRenderer } = await vite.ssrLoadModule("/components/ui/MarkdownRenderer.tsx");
+ let components;
+ function InspectMarkdown(props) {
+ const element = MarkdownRenderer(props);
+ components = element.props.children.props.children.props.components;
+ return element;
+ }
+ renderToStaticMarkup(createElement(InspectMarkdown, { content: TABLE_MARKDOWN }));
+ const initial = components;
+
+ for (const props of [
+ { content: TABLE_MARKDOWN },
+ { content: `${TABLE_MARKDOWN}\nMore streamed text.` },
+ { content: TABLE_MARKDOWN, className: "text-sm" },
+ ]) {
+ renderToStaticMarkup(createElement(InspectMarkdown, props));
+ const updated = components;
+ // A changed ancestor type also remounts nested tables and loses their scroll position.
+ for (const tag of Object.keys(initial)) {
+ assert.equal(updated[tag], initial[tag], `${tag} must reconcile instead of remounting`);
+ }
+ }
+});
+
+test("inline markdown inside cells still renders", async (t) => {
+ const html = await renderMarkdown(t, TABLE_MARKDOWN);
+
+ assert.ok(html.includes("<strong"), "bold inside a cell is parsed");
+});
+
+test("non-table markdown is unchanged", async (t) => {
+ const html = await renderMarkdown(t, "## Heading\n\n- one\n- two\n\n**bold** and `code`\n");
+
+ assert.ok(html.includes("<h2"), "headings still render");
+ assert.ok(html.includes("<ul"), "lists still render");
+ assert.ok(html.includes("<strong"), "bold still renders");
+ assert.ok(html.includes("<code"), "inline code still renders");
+ assert.ok(!html.includes("prose"), "no typography-plugin classes: the plugin is not installed");
+});
+
+test("GFM extras the plugin enables render as elements, not literal syntax", async (t) => {
+ const html = await renderMarkdown(
+ t,
+ "~~gone~~ and https://openwhispr.com\n\n- [ ] open\n- [x] done\n"
+ );
+
+ assert.ok(html.includes("<del"), "strikethrough renders as del");
+ assert.ok(!html.includes("~~"), "no literal tildes survive");
+ assert.ok(html.includes('href="https://openwhispr.com"'), "a bare URL is autolinked");
+ assert.equal(
+ (html.match(/type="checkbox"/g) || []).length,
+ 2,
+ "task list renders two checkboxes"
+ );
+});
+
+test("URL sanitisation is unchanged with the plugin enabled", async (t) => {
+ const html = await renderMarkdown(
+ t,
+ "[click](javascript:alert(1)) [data](data:text/html,hi) <img src=x onerror=alert(1)>\n"
+ );
+
+ assert.ok(!html.includes("javascript:"), "javascript: href is stripped");
+ assert.ok(!html.includes("data:text"), "data: href is stripped");
+ assert.ok(!html.includes("<img"), "raw HTML stays escaped");
+});