mirror of
https://github.com/docmost/docmost.git
synced 2026-10-04 15:13:44 +10:00
fix: preserve table header row in markdown export
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user