fix: add markdown attribute to details blocks in markdown export (#2533)

This commit is contained in:
Philip Okugbe
2026-09-30 01:36:08 +01:00
committed by GitHub
parent f4caf0ba02
commit b6434371ed
3 changed files with 55 additions and 55 deletions
@@ -1,54 +0,0 @@
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 |',
]);
});
});
@@ -0,0 +1,54 @@
import { htmlToMarkdown } from '@docmost/editor-ext';
const colgroup =
'<colgroup><col style="min-width: 25px;" /><col style="min-width: 25px;" /></colgroup>';
describe('htmlToMarkdown', () => {
it('uses the table header row as the markdown header', () => {
const markdown = htmlToMarkdown(
`<table>${colgroup}<tbody>` +
'<tr><th><p>Name</p></th><th><p>Role</p></th></tr>' +
'<tr><td><p>Ada</p></td><td><p>Engineer</p></td></tr>' +
'</tbody></table>',
);
expect(markdown.trim().split('\n')).toEqual([
'| Name | Role |',
'| --- | --- |',
'| Ada | Engineer |',
]);
});
it('adds an empty header when the table has no header row', () => {
const markdown = htmlToMarkdown(
`<table>${colgroup}<tbody>` +
'<tr><td><p>Ada</p></td><td><p>Engineer</p></td></tr>' +
'<tr><td><p>Alan</p></td><td><p>Mathematician</p></td></tr>' +
'</tbody></table>',
);
expect(markdown.trim().split('\n')).toEqual([
'| | |',
'| --- | --- |',
'| Ada | Engineer |',
'| Alan | Mathematician |',
]);
});
it('marks details blocks so their content is parsed as markdown', () => {
const markdown = htmlToMarkdown(
'<details><summary data-type="detailsSummary">Title</summary>' +
'<div data-type="detailsContent"><p>Some <strong>bold</strong></p></div>' +
'</details>',
);
expect(markdown.trim().split('\n')).toEqual([
'<details markdown="1">',
'<summary>Title</summary>',
'',
'Some **bold**',
'',
'</details>',
]);
});
});
@@ -150,7 +150,7 @@ function preserveDetail(turndownService: _TurndownService) {
)
.join('');
return `\n<details>\n${detailSummary}\n\n${detailsContent}\n\n</details>\n`;
return `\n<details markdown="1">\n${detailSummary}\n\n${detailsContent}\n\n</details>\n`;
},
});
}