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): restore the table figure wrapper on import
Elian Doran64acea20233d
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.tsindex 0ab74af487..5635860a8e 100644--- a/packages/trilium-core/src/services/import/markdown.spec.ts+++ b/packages/trilium-core/src/services/import/markdown.spec.ts@@ -360,8 +360,14 @@ $$`;` </tbody>`,`</table>`].join("\n");+ const expected = [+ `<figure class="table"><table><tbody>`,+ `<tr><th>Heading</th><td>Not a heading</td></tr>`,+ `<tr><td>Heading</td><td>Not a heading</td></tr>`,+ `</tbody></table></figure>`+ ].join("");const result = markdownService.renderToHtml(html, "Title");- expect(result).toStrictEqual(html);+ expect(result).toStrictEqual(expected);// No synthesized empty header row leaked in.expect(result).not.toContain("<thead>");expect(result).not.toContain("<th></th>");@@ -528,4 +534,57 @@ $$`;expect(markdownService.renderToHtml(markdownExportService.toMarkdown(original), "Title")).toStrictEqual(original);});});+ describe("tables", () => {+ it("wraps a table kept as raw HTML in the figure CKEditor downcasts, and drops the indentation the exporter adds", () => {+ const input = trimIndentation`\+ <table class="ck-table-resized" style="border-style:none;">+ <colgroup>+ <col style="width:80.6%;">+ <col style="width:19.4%;">+ </colgroup>+ <tbody>+ <tr>+ <td>a</td>+ <td>b</td>+ </tr>+ </tbody>+ </table>`;+ const expected = [+ `<figure class="table"><table class="ck-table-resized" style="border-style:none;">`,+ `<colgroup><col style="width:80.6%;"><col style="width:19.4%;"></colgroup>`,+ `<tbody><tr><td>a</td><td>b</td></tr></tbody></table></figure>`+ ].join("");+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });++ it("leaves a table the renderer already wrapped alone rather than nesting a second figure", () => {+ const input = trimIndentation`\+ | a | b |+ | --- | --- |+ | 1 | 2 |`;+ const expected = [+ `<figure class="table"><table><thead><tr><th>a</th><th>b</th></tr></thead>`,+ `<tbody><tr><td>1</td><td>2</td></tr></tbody></table></figure>`+ ].join("");+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });++ it("keeps the whitespace inside a cell, which carries inline content rather than blocks", () => {+ const input = `<table><tbody><tr><td>a <em>b</em> c</td><td> </td></tr></tbody></table>`;+ const expected = `<figure class="table"><table><tbody><tr><td>a <em>b</em> c</td><td> </td></tr></tbody></table></figure>`;+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });++ it("round-trips a table through the Markdown exporter unchanged, whether it is kept as raw HTML or written as a pipe table", () => {+ for (const original of [+ `<figure class="table"><table class="ck-table-resized" style="border-style:none;"><colgroup><col style="width:80.6%;"><col style="width:19.4%;"></colgroup><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td><ul><li>one</li></ul></td><td>2</td></tr></tbody></table></figure>`,+ `<figure class="table"><table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table></figure>`,+ `<details class="trilium-collapsible"><summary>S</summary><figure class="table"><table class="ck-table-resized"><colgroup><col style="width:50%;"></colgroup><tbody><tr><td>x</td></tr></tbody></table></figure></details>`+ ]) {+ const markdown = markdownExportService.toMarkdown(original);+ expect(markdownService.renderToHtml(markdown, "Title")).toStrictEqual(original);+ expect(markdownExportService.toMarkdown(markdownService.renderToHtml(markdown, "Title"))).toStrictEqual(markdown);+ }+ });+ });});diff --git a/packages/trilium-core/src/services/import/markdown.ts b/packages/trilium-core/src/services/import/markdown.tsindex edd60e553c..3fac6f2ef6 100644--- a/packages/trilium-core/src/services/import/markdown.ts+++ b/packages/trilium-core/src/services/import/markdown.ts@@ -10,7 +10,7 @@ function renderToHtml(content: string, title: string, opts?: { obsidian?: booleataskStates: getTaskStates(),obsidian: opts?.obsidian});- return normalizeCollapsibles(html);+ return normalizeToEditorMarkup(html);}export default {@@ -18,20 +18,21 @@ export default {};/**- * Restores what `buildDetailsFilter()` in `services/export/markdown.ts` gives up when it- * writes a `<details>` block to Markdown: the `trilium-collapsible` class, and the absence- * of whitespace between block children, which that filter indents for readability. Both are- * what the collapsible CKEditor plugin downcasts, so a note that goes out to Markdown and- * comes back keeps the content it started with instead of changing on its next save.+ * Restores what the Markdown exporter gives up on a `<details>` block and on a table it keeps as+ * raw HTML: the `trilium-collapsible` class, the `<figure class="table">` wrapper, and the absence+ * of whitespace between block children, which the exporter indents for readability. All three are+ * what CKEditor downcasts, so a note that goes out to Markdown and comes back keeps the content it+ * started with instead of changing on its next save.*/-function normalizeCollapsibles(html: string): string {- if (!html.includes("<details")) {+function normalizeToEditorMarkup(html: string): string {+ if (!html.includes("<details") && !html.includes("<table")) {return html;}const root = parseHtml(html);const collapsibles = root.querySelectorAll("details");- if (!collapsibles.length) {+ const tables = root.querySelectorAll("table");+ if (!collapsibles.length && !tables.length) {return html;}@@ -44,18 +45,31 @@ function normalizeCollapsibles(html: string): string {classList.push(COLLAPSIBLE_CLASS);}collapsible.setAttributes({ class: classList.join(" "), ...rest });- removeLayoutWhitespace(collapsible);+ removeLayoutWhitespace(collapsible, COLLAPSIBLE_CONTAINER_TAGS);+ }++ // Innermost table first: wrapping re-serializes the element, so an inner table has to carry+ // its own wrapper by the time the one around it is rewritten.+ for (const table of tables.reverse()) {+ removeLayoutWhitespace(table, TABLE_CONTAINER_TAGS);++ const parent = table.parentNode;+ if (parent instanceof HTMLElement && parent.tagName === "FIGURE") {+ continue;+ }+ table.insertAdjacentHTML("beforebegin", `<figure class="table">${table.toString()}</figure>`);+ table.remove();}return root.toString();}/**- * Drops whitespace-only text nodes from an element whose children are blocks, and recurses- * into the block containers `buildDetailsFilter()` indents. An element holding text of its- * own is left alone, so spacing that separates inline content survives. Whitespace inside- * `<pre>` is never reached — `<pre>` is not one of the containers.+ * Drops whitespace-only text nodes from an element whose children are blocks, and recurses into+ * `containerTags`, the tags the exporter indents into. An element holding text of its own is left+ * alone, so spacing that separates inline content survives. Whitespace inside `<pre>` and inside a+ * table cell is never reached — neither is one of the containers.*/-function removeLayoutWhitespace(element: HTMLElement) {+function removeLayoutWhitespace(element: HTMLElement, containerTags: Set<string>) {const isText = (node: Node) => node.nodeType === NodeType.TEXT_NODE;const holdsText = (node: Node) => isText(node) && node.rawText.trim() !== "";@@ -64,8 +78,8 @@ function removeLayoutWhitespace(element: HTMLElement) {}for (const child of element.childNodes) {- if (child instanceof HTMLElement && BLOCK_CONTAINER_TAGS.has(child.tagName)) {- removeLayoutWhitespace(child);+ if (child instanceof HTMLElement && containerTags.has(child.tagName)) {+ removeLayoutWhitespace(child, containerTags);}}}@@ -73,5 +87,8 @@ function removeLayoutWhitespace(element: HTMLElement) {/** The styling hook `collapsible_editing.ts` downcasts onto every `<details>` it writes. */const COLLAPSIBLE_CLASS = "trilium-collapsible";-/** Mirrors `DETAILS_CONTAINER_TAGS` in `services/export/markdown.ts`, the tags it indents into. */-const BLOCK_CONTAINER_TAGS = new Set(["DETAILS", "UL", "OL", "LI", "BLOCKQUOTE"]);+/** Mirrors `DETAILS_CONTAINER_TAGS` in `services/export/markdown.ts`. */+const COLLAPSIBLE_CONTAINER_TAGS = new Set(["DETAILS", "UL", "OL", "LI", "BLOCKQUOTE"]);++/** Mirrors `TABLE_CONTAINER_TAGS` in `@triliumnext/turndown-plugin-gfm`. */+const TABLE_CONTAINER_TAGS = new Set(["TABLE", "THEAD", "TBODY", "TFOOT", "TR", "COLGROUP"]);