From c59e8bcc3f8ef42220fa52694cfdab3667a833de Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 14 Jun 2026 23:03:14 -0400 Subject: [PATCH] fix: Encode code & math fences in table cells with
to avoid breaking rows (#12569) * fix: Encode code & math fences in table cells with
to avoid breaking rows Code and LaTeX fences inside table cells emitted literal newlines during Markdown serialization, breaking the single-line table-row structure and corrupting the whole table. Serialize them on a single line using
for line breaks (and escaped pipes) when inside a table, matching the existing handling for lists and hard breaks. Co-Authored-By: Claude Opus 4.8 * fix: Escape backslashes in table-cell code & math fences CodeQL flagged incomplete string escaping: escaping pipes as \| without escaping pre-existing backslashes leaves the encoding ambiguous. Escape the backslash (the escape character) and pipe together in a single pass so cell content cannot break out of the column. Co-Authored-By: Claude Opus 4.8 * test: Cover math block table-cell serialization Add a test mirroring the code fence case to guard the MathBlock half of the table-cell
encoding and pipe/backslash escaping. Co-Authored-By: Claude Opus 4.8 --------- Co-authored-by: Claude Opus 4.8 --- server/models/helpers/DocumentHelper.test.ts | 115 +++++++++++++++++++ shared/editor/nodes/CodeFence.ts | 13 +++ shared/editor/nodes/MathBlock.ts | 11 ++ 3 files changed, 139 insertions(+) diff --git a/server/models/helpers/DocumentHelper.test.ts b/server/models/helpers/DocumentHelper.test.ts index 8616bca215..d829c1ce91 100644 --- a/server/models/helpers/DocumentHelper.test.ts +++ b/server/models/helpers/DocumentHelper.test.ts @@ -635,6 +635,121 @@ This is a [test paragraph](https://example.net)`, expect(result).toContain("[x] done"); }); + it("should export code fences inside table cells on a single line", async () => { + const document = await buildDocument({ + content: { + type: "doc", + content: [ + { + type: "table", + content: [ + { + type: "tr", + content: [ + { + type: "th", + attrs: { colspan: 1, rowspan: 1 }, + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "Header" }], + }, + ], + }, + ], + }, + { + type: "tr", + content: [ + { + type: "td", + attrs: { colspan: 1, rowspan: 1 }, + content: [ + { + type: "code_fence", + attrs: { language: "abap" }, + content: [ + { type: "text", text: "a | b\nc \\ d\nline 2" }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + // Code fences inside tables should use
tags rather than literal + // newlines that would break the table row structure, with pipes and + // backslashes escaped so the content cannot break out of the column. + expect(result).toContain( + "```abap
a \\| b
c \\\\ d
line 2
```" + ); + // The fence content must not introduce raw newlines inside the table. + expect(result).not.toMatch(/```abap\n/); + }); + + it("should export math blocks inside table cells on a single line", async () => { + const document = await buildDocument({ + content: { + type: "doc", + content: [ + { + type: "table", + content: [ + { + type: "tr", + content: [ + { + type: "th", + attrs: { colspan: 1, rowspan: 1 }, + content: [ + { + type: "paragraph", + content: [{ type: "text", text: "Header" }], + }, + ], + }, + ], + }, + { + type: "tr", + content: [ + { + type: "td", + attrs: { colspan: 1, rowspan: 1 }, + content: [ + { + type: "math_block", + content: [ + { type: "text", text: "a | b\n\\frac{1}{2}" }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }, + }); + const result = await DocumentHelper.toMarkdown(document, { + includeTitle: false, + }); + // Math blocks inside tables should use
tags rather than literal + // newlines that would break the table row structure, with pipes and + // backslashes escaped so the content cannot break out of the column. + expect(result).toContain("$$
a \\| b
\\\\frac{1}{2}
$$"); + // The block content must not introduce raw newlines inside the table. + expect(result).not.toMatch(/\$\$\n/); + }); + it("should include collection title by default", async () => { const collection = await buildCollection({ name: "Test Collection", diff --git a/shared/editor/nodes/CodeFence.ts b/shared/editor/nodes/CodeFence.ts index a736e68fad..59652813ad 100644 --- a/shared/editor/nodes/CodeFence.ts +++ b/shared/editor/nodes/CodeFence.ts @@ -698,6 +698,19 @@ export default class CodeFence extends Node { } toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) { + // Inside table cells literal newlines break the row structure, so encode + // the fence on a single line using
for line breaks. Backslashes and + // pipes are escaped so the cell content cannot break out of the column. + if (state.inTable) { + const code = node.textContent + .replace(/[\\|]/g, "\\$&") + .replace(/\n/g, "
"); + state.write( + "```" + (node.attrs.language || "") + "
" + code + "
```" + ); + return; + } + state.write("```" + (node.attrs.language || "") + "\n"); state.text(node.textContent, false); state.ensureNewLine(); diff --git a/shared/editor/nodes/MathBlock.ts b/shared/editor/nodes/MathBlock.ts index 5626ed9f1a..be485e3914 100644 --- a/shared/editor/nodes/MathBlock.ts +++ b/shared/editor/nodes/MathBlock.ts @@ -46,6 +46,17 @@ export default class MathBlock extends Node { } toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) { + // Inside table cells literal newlines break the row structure, so encode + // the block on a single line using
for line breaks. Backslashes and + // pipes are escaped so the cell content cannot break out of the column. + if (state.inTable) { + const math = node.textContent + .replace(/[\\|]/g, "\\$&") + .replace(/\n/g, "
"); + state.write("$$
" + math + "
$$"); + return; + } + state.write("$$\n"); state.text(node.textContent, false); state.ensureNewLine();