Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(markdown): keep headerless tables as HTML instead of a phantom header
Elian Doranf8b68ed94109
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.tsindex 55cf49cdb6..a164d156be 100644--- a/packages/trilium-core/src/services/export/markdown.spec.ts+++ b/packages/trilium-core/src/services/export/markdown.spec.ts@@ -179,10 +179,7 @@ describe("Markdown export", () => {> [!IMPORTANT]> This is a very important information.>${space}- > | | |- > | --- | --- |- > | 1 | 2 |- > | 3 | 4 |+ > <table><tbody><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></tbody></table>> [!CAUTION]> This is a caution.@@ -387,7 +384,10 @@ describe("Markdown export", () => {expect(markdownExportService.toMarkdown(html)).toBe(expected);});- it("exports unformatted table", () => {+ // A table with no heading row can't be expressed as a GFM table without+ // inventing a phantom empty header (which would reimport as a blank row), so+ // it is kept as raw HTML to round-trip faithfully.+ it("keeps a table with no heading row as HTML", () => {const html = trimIndentation/*html*/`\<figure class="table"><table>@@ -412,11 +412,7 @@ describe("Markdown export", () => {</table></figure>`;- const expected = trimIndentation`\- | | |- | --- | --- |- | Hi | there |- | Hi | there |`;+ const expected = `<table><tbody><tr><td>Hi</td><td>there</td></tr><tr><td>Hi</td><td>there</td></tr></tbody></table>`;expect(markdownExportService.toMarkdown(html)).toBe(expected);});diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.tsindex 7ecf46a5ef..af13896290 100644--- a/packages/trilium-core/src/services/import/markdown.spec.ts+++ b/packages/trilium-core/src/services/import/markdown.spec.ts@@ -328,6 +328,18 @@ $$`;expect(markdownService.renderToHtml(html, "Title")).toStrictEqual(html);});+ // The exporter keeps headerless tables (and heading-column-only tables) as raw+ // HTML rather than inventing a phantom empty header row. Confirm the import side+ // passes that HTML through unchanged, so the round-trip introduces no blank row.+ it("preserves a headerless table (heading columns only) without a phantom header row", () => {+ const html = /*html*/`<table><tbody><tr><th>Heading</th><td>Not a heading</td></tr><tr><td>Heading</td><td>Not a heading</td></tr></tbody></table>`;+ const result = markdownService.renderToHtml(html, "Title");+ expect(result).toStrictEqual(html);+ // No synthesized empty header row leaked in.+ expect(result).not.toContain("<thead>");+ expect(result).not.toContain("<th></th>");+ });+it("generates strike-through text", () => {const input = `~~Hello~~ world.`;const expected = /*html*/`<p><del>Hello</del> world.</p>`;diff --git a/packages/turndown-plugin-gfm/src/lib/tables.js b/packages/turndown-plugin-gfm/src/lib/tables.jsindex 3d4e895ebc..c69937398d 100644--- a/packages/turndown-plugin-gfm/src/lib/tables.js+++ b/packages/turndown-plugin-gfm/src/lib/tables.js@@ -88,24 +88,12 @@ rules.table = {// Ensure there are no blank linescontent = content.replace(/\n+/g, '\n')- // If table has no heading, add an empty one so as to get a valid Markdown table- var secondLine = content.trim().split('\n');- if (secondLine.length >= 2) secondLine = secondLine[1]- var secondLineIsDivider = /\| :?---/.test(secondLine);-- var columnCount = tableColCount(node);- var emptyHeader = ''- if (columnCount && !secondLineIsDivider) {- // MD060 compact style: 2 spaces between pipes for empty cells- emptyHeader = '|' + ' |'.repeat(columnCount) + '\n' + '|'- for (var columnIndex = 0; columnIndex < columnCount; ++columnIndex) {- emptyHeader += ' ' + getBorder(getColumnAlignment(node, columnIndex)) + ' |';- }- }-+ // A table reaching this branch always has a real heading row (otherwise+ // `tableShouldBeHtml` would have kept it as HTML), so the rendered content+ // already starts with a header + divider and no synthetic header is needed.const captionContent = node.caption ? node.caption.textContent || '' : '';const caption = captionContent ? `${captionContent}\n\n` : '';- const tableContent = `${emptyHeader}${content}`.trimStart();+ const tableContent = content.trimStart();return `\n\n${caption}${tableContent}\n\n`;}}@@ -218,7 +206,21 @@ const tableShouldBeHtml = (tableNode, options) => {if (options.preserveNestedTables) possibleTags.push('TABLE');return nodeContains(tableNode, 'code') ||- nodeContains(tableNode, possibleTags);+ nodeContains(tableNode, possibleTags) ||+ // GFM tables must have a header row. A table with no heading row (or with+ // only heading columns) cannot be represented in Markdown without inventing+ // a phantom empty header, which reimports as a spurious blank row. Keep such+ // tables as raw HTML instead so they round-trip faithfully. Skippable tables+ // (e.g. a single cell) are rendered as paragraphs and are left untouched.+ (!tableShouldBeSkipped(tableNode) && !tableHasHeadingRow(tableNode));+}++// A table has a heading row when its first row qualifies as a heading row (its+// parent is a THEAD, or it is the first row and every cell is a TH). This is the+// row that produces the Markdown divider line; without it the table has no+// header and must be kept as HTML.+function tableHasHeadingRow(tableNode) {+ return !!tableNode.rows && tableNode.rows.length > 0 && isHeadingRow(tableNode.rows[0]);}// Various conditions under which a table should be skipped - i.e. each celldiff --git a/packages/turndown-plugin-gfm/test/gfm.spec.ts b/packages/turndown-plugin-gfm/test/gfm.spec.tsindex 58813286f3..a48466e9ba 100644--- a/packages/turndown-plugin-gfm/test/gfm.spec.ts+++ b/packages/turndown-plugin-gfm/test/gfm.spec.ts@@ -261,19 +261,19 @@ describe("GFM plugin", () => {expect(turndown.turndown(input)).toBe(expected);});- it("handles non-definitive heading row", () => {+ // GFM cannot represent a headerless table without inventing a phantom+ // empty header row, which reimports as a spurious blank row. Such tables+ // are kept as raw HTML instead so they round-trip faithfully.+ it("keeps a table with no heading row as HTML", () => {const input = `<table><tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr><tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr></table>`;- const expected = `| | |-| --- | --- |-| Row 1 Cell 1 | Row 1 Cell 2 |-| Row 2 Cell 1 | Row 2 Cell 2 |`;+ const expected = `<table><tbody><tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr><tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr></tbody></table>`;expect(turndown.turndown(input)).toBe(expected);});- it("handles non-definitive heading row with th", () => {+ it("keeps a table with only heading columns as HTML", () => {const input = `<table><tr><th>Heading</th>@@ -284,10 +284,7 @@ describe("GFM plugin", () => {<td>Not a heading</td></tr></table>`;- const expected = `| | |-| --- | --- |-| Heading | Not a heading |-| Heading | Not a heading |`;+ const expected = `<table><tbody><tr><th>Heading</th><td>Not a heading</td></tr><tr><td>Heading</td><td>Not a heading</td></tr></tbody></table>`;expect(turndown.turndown(input)).toBe(expected);});});