fix: preserve table header row in markdown export (#2532)

This commit is contained in:
Philip Okugbe
2026-09-30 01:19:16 +01:00
committed by GitHub
parent a78e52b8e6
commit f4caf0ba02
2 changed files with 59 additions and 1 deletions
@@ -0,0 +1,54 @@
import { jsonToMarkdown } from './collaboration.util';
const cell = (type: 'tableHeader' | 'tableCell', text: string) => ({
type,
content: [
{
type: 'paragraph',
content: text ? [{ type: 'text', text }] : [],
},
],
});
const row = (type: 'tableHeader' | 'tableCell', texts: string[]) => ({
type: 'tableRow',
content: texts.map((text) => cell(type, text)),
});
const tableDoc = (rows: ReturnType<typeof row>[]) => ({
type: 'doc',
content: [{ type: 'table', content: rows }],
});
describe('jsonToMarkdown', () => {
it('uses the table header row as the markdown header', () => {
const markdown = jsonToMarkdown(
tableDoc([
row('tableHeader', ['Name', 'Role']),
row('tableCell', ['Ada', 'Engineer']),
]),
);
expect(markdown.trim().split('\n')).toEqual([
'| Name | Role |',
'| --- | --- |',
'| Ada | Engineer |',
]);
});
it('adds an empty header when the table has no header row', () => {
const markdown = jsonToMarkdown(
tableDoc([
row('tableCell', ['Ada', 'Engineer']),
row('tableCell', ['Alan', 'Mathematician']),
]),
);
expect(markdown.trim().split('\n')).toEqual([
'| | |',
'| --- | --- |',
'| Ada | Engineer |',
'| Alan | Mathematician |',
]);
});
});
@@ -37,7 +37,11 @@ export function htmlToMarkdown(html: string): string {
footnoteRef,
footnotesList,
]);
return turndownService.turndown(html).replaceAll('<br>', ' ');
const htmlWithoutColgroups = html.replace(
/<colgroup\b[^>]*>[\s\S]*?<\/colgroup>/gi,
'',
);
return turndownService.turndown(htmlWithoutColgroups).replaceAll('<br>', ' ');
}
function listParagraph(turndownService: _TurndownService) {