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 <br> in Markdown table cells as line breaks (#2572)

Chad Pihac8c0f8afeb7e
diff --git a/src/components/ui/MarkdownRenderer.tsx b/src/components/ui/MarkdownRenderer.tsx
index 3f6bc35e3..d8e47205e 100644
--- a/src/components/ui/MarkdownRenderer.tsx
+++ b/src/components/ui/MarkdownRenderer.tsx
@@ -51,6 +51,31 @@ function MarkdownImage({
);
}
+interface MarkdownNode {
+ type: string;
+ value?: string;
+ children?: MarkdownNode[];
+}
+
+const BREAK_TAG = /^<br\s*\/?>$/i;
+
+// A GFM table row is a single line, so models break lines inside a cell with
+// <br>. Raw HTML renders as escaped text, so only a bare <br> tag is turned
+// into a Markdown line break; every other tag, and a <br> with attributes,
+// stays literal.
+function remarkBreakTags(): (tree: MarkdownNode) => void {
+ const visit = (node: MarkdownNode): void => {
+ node.children?.forEach((child, index, siblings) => {
+ if (child.type === "html" && BREAK_TAG.test(child.value ?? "")) {
+ siblings[index] = { type: "break" };
+ } else {
+ visit(child);
+ }
+ });
+ };
+ return visit;
+}
+
// Stable component types preserve DOM state, including table scroll positions.
const markdownComponents: Components = {
h1: ({ children }): ReactElement => (
@@ -128,7 +153,7 @@ export function MarkdownRenderer({ content, className }: MarkdownRendererProps):
<div dir="auto" className={className}>
<FootnotePrefixContext value={footnotePrefix}>
<Markdown
- remarkPlugins={[remarkGfm]}
+ remarkPlugins={[remarkGfm, remarkBreakTags]}
remarkRehypeOptions={{ clobberPrefix: footnotePrefix }}
components={markdownComponents}
>
diff --git a/test/components/markdownRenderer.test.js b/test/components/markdownRenderer.test.js
index 38e6dad63..5a63ccdd3 100644
--- a/test/components/markdownRenderer.test.js
+++ b/test/components/markdownRenderer.test.js
@@ -258,3 +258,37 @@ test("URL sanitisation is unchanged with the plugin enabled", async (t) => {
assert.ok(!html.includes("data:text"), "data: href is stripped");
assert.ok(!html.includes("<img"), "raw HTML stays escaped");
});
+
+// A GFM table row is a single line, so models break lines inside a cell with
+// <br>. Raw HTML stays escaped, which printed those tags as literal text.
+test("<br> inside a table cell becomes a line break, not literal text", async (t) => {
+ const html = await renderMarkdown(
+ t,
+ [
+ "| Call | Topics |",
+ "|------|--------|",
+ "| One | • first<br>• second<br/>• third<BR />• fourth |",
+ "",
+ ].join("\n")
+ );
+
+ assert.ok(!html.includes("&lt;br"), "no literal <br> text in the cell");
+ assert.equal((html.match(/<br\/?>/g) || []).length, 3, "each tag is a real line break");
+ assert.ok(html.includes("fourth"), "text after the breaks still renders");
+});
+
+test("other raw HTML stays escaped next to a converted <br>", async (t) => {
+ const html = await renderMarkdown(t, "line one<br>line two <b>bold</b> <br onclick=x>\n");
+
+ assert.equal((html.match(/<br\/?>/g) || []).length, 1, "only the plain <br> converts");
+ assert.ok(html.includes("&lt;b&gt;"), "<b> is still escaped");
+ assert.ok(html.includes("&lt;br onclick=x&gt;"), "a <br> with attributes is still escaped");
+ assert.ok(!html.includes("onclick=x>"), "no attribute reaches the DOM");
+});
+
+test("<br> in inline code stays literal", async (t) => {
+ const html = await renderMarkdown(t, "Use `<br>` for a break.\n");
+
+ assert.ok(html.includes("&lt;br&gt;"), "code shows the tag as written");
+ assert.ok(!/<br\/?>/.test(html), "no line break is inserted");
+});