fix(editor): preserve table cell line breaks

This commit is contained in:
Neil
2026-09-18 03:42:00 -07:00
parent a7cfdddfe5
commit a4f60ff9ea
2 changed files with 55 additions and 1 deletions
@@ -88,8 +88,50 @@ describe('compact Markdown table serialization', () => {
it('counts alignment markers inside the separator width', () => {
const node = table(['left', 'center', 'right'], [['a', 'b', '42']])
for (const [index, align] of ['left', 'center', 'right'].entries()) {
const header = node.content?.[0]?.content?.[index]
if (header) {
header.attrs = { align }
}
}
const header = lines(renderTableToCompactMarkdown(node, helpers))[0]
const rows = lines(renderTableToCompactMarkdown(node, helpers))
expect(rows[1]?.length).toBe(header?.length)
expect(rows[1]).toContain(':---')
expect(rows[1]).toContain(':----:')
expect(rows[1]).toContain('---:')
})
it('keeps multiple blocks in a cell on one Markdown table row', () => {
const node: JSONContent = {
type: 'table',
content: [
{
type: 'tableRow',
content: [
{
type: 'tableHeader',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Name' }] }]
}
]
},
{
type: 'tableRow',
content: [
{
type: 'tableCell',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'first' }] },
{ type: 'paragraph', content: [{ type: 'text', text: 'second' }] }
]
}
]
}
]
}
const output = renderTableToCompactMarkdown(node, helpers)
expect(output).toContain('first<br>second')
expect(lines(output).every((line) => !line.includes('\n'))).toBe(true)
})
})
@@ -7,6 +7,7 @@ const MIN_COLUMN_WIDTH = 3
const OUTLIER_MEDIAN_FACTOR = 2.5
const MAX_COLUMN_WIDTH = 60
const MAX_ALIGNED_TABLE_WIDTH = 160
const CELL_LINE_SEPARATOR = '\u001F'
function collapseWhitespace(value: string): string {
return value.replace(/\s+/g, ' ').trim()
@@ -64,7 +65,18 @@ function separatorCell(width: number, align: TableCellAlign): string {
function extractRows(node: JSONContent, helpers: MarkdownRendererHelpers): TableCell[][] {
return (node.content ?? []).map((rowNode) =>
(rowNode.content ?? []).map((cellNode) => ({
text: collapseWhitespace(cellNode.content ? helpers.renderChildren(cellNode.content) : ''),
text: collapseWhitespace(
(cellNode.content?.length ?? 0) > 1
? cellNode.content
.map((child) => helpers.renderChildren(child))
.join(CELL_LINE_SEPARATOR)
.split(CELL_LINE_SEPARATOR)
.join('\n')
.replace(/[ \t]*\r?\n[ \t]*/g, '<br>')
: cellNode.content
? helpers.renderChildren(cellNode.content)
: ''
),
isHeader: cellNode.type === 'tableHeader',
align: normalizeAlign(cellNode.attrs)
}))