Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): keep collapsible blocks stable across an export/import cycle

Elian Doranaa8ab577e6ff
diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.ts
index 5a624ea5e2..934e6a4c77 100644
--- a/packages/trilium-core/src/services/export/markdown.ts
+++ b/packages/trilium-core/src/services/export/markdown.ts
@@ -323,9 +323,10 @@ function buildLinkPreviewFilter(): Rule {
* verbatim as well so its content is never dropped.
*
* The Trilium-only `trilium-collapsible` styling hook is dropped from the
- * exported <details> (any user-added classes are kept). It is not needed to
- * round-trip: the importer upcasts <details> by tag name and the collapsible
- * plugin re-stamps the class on the next save.
+ * exported <details> (any user-added classes are kept), so the Markdown stays
+ * portable. `normalizeCollapsibles()` in `services/import/markdown.ts` puts the
+ * class and the collapsed whitespace back, so the cycle returns the note
+ * unchanged.
*/
function buildDetailsFilter(): Rule {
// Block containers whose children are themselves blocks. Inline-content
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.ts
index 2af8f09c32..95ae5efd12 100644
--- a/packages/trilium-core/src/services/import/markdown.spec.ts
+++ b/packages/trilium-core/src/services/import/markdown.spec.ts
@@ -1,6 +1,7 @@
import { trimIndentation } from "@triliumnext/commons";
import { describe, expect, it } from "vitest";
+import markdownExportService from "../export/markdown.js";
import markdownService from "./markdown.js";
describe("markdown", () => {
@@ -451,4 +452,71 @@ $$`;
const expected = /*html*/`<ul><li>&lt;note&gt; is note.</li></ul>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
+
+ describe("collapsible blocks", () => {
+ it("re-stamps the trilium-collapsible class and drops the indentation the exporter adds, so the block matches what the editor downcasts", () => {
+ const input = trimIndentation`\
+ <details>
+ <summary>Configuration change</summary>
+ <p>Set the following:</p>
+ <pre><code class="language-text-x-yaml">entryPoints:
+ web:
+ http: true</code></pre>
+ <aside class="admonition tip"><p>A tip.</p></aside>
+ </details>`;
+ const expected = [
+ `<details class="trilium-collapsible"><summary>Configuration change</summary><p>Set the following:</p>`,
+ `<pre><code class="language-text-x-yaml">entryPoints:\n web:\n http: true</code></pre>`,
+ `<aside class="admonition tip"><p>A tip.</p></aside></details>`
+ ].join("");
+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
+ });
+
+ it("writes the class before an expanded block's open attribute and strips the indentation of nested lists", () => {
+ const input = trimIndentation`\
+ <details open="">
+ <summary>Sum</summary>
+ <ol>
+ <li>
+ <span>First</span>
+ <ul>
+ <li>
+ <span>Nested</span>
+ </li>
+ </ul>
+ </li>
+ </ol>
+ </details>`;
+ const expected = `<details class="trilium-collapsible" open=""><summary>Sum</summary><ol><li><span>First</span><ul><li><span>Nested</span></li></ul></li></ol></details>`;
+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
+ });
+
+ it("keeps a user-added class, adds the styling hook only once and leaves an element holding text of its own untouched", () => {
+ expect(markdownService.renderToHtml(`<details class="custom"><summary>S</summary><p>B</p></details>`, "Title"))
+ .toStrictEqual(`<details class="custom trilium-collapsible"><summary>S</summary><p>B</p></details>`);
+ expect(markdownService.renderToHtml(`<details class="trilium-collapsible"><summary>S</summary><p>B</p></details>`, "Title"))
+ .toStrictEqual(`<details class="trilium-collapsible"><summary>S</summary><p>B</p></details>`);
+ expect(markdownService.renderToHtml(`<details>Just some <em>text</em> here</details>`, "Title"))
+ .toStrictEqual(`<details class="trilium-collapsible">Just some <em>text</em> here</details>`);
+ });
+
+ it("leaves a <details> shown inside a fenced code block as escaped sample text", () => {
+ const input = trimIndentation`\
+ \`\`\`html
+ <details><summary>S</summary></details>
+ \`\`\``;
+ expect(markdownService.renderToHtml(input, "Title"))
+ .toStrictEqual(`<pre><code class="language-text-html">&lt;details&gt;&lt;summary&gt;S&lt;/summary&gt;&lt;/details&gt;</code></pre>`);
+ });
+
+ it("round-trips a collapsible block through the Markdown exporter unchanged", () => {
+ const original = [
+ `<details class="trilium-collapsible" open=""><summary>Configuration change</summary>`,
+ `<p>Set the <a href="https://example.com"><strong>static</strong> configuration</a>:</p>`,
+ `<pre><code class="language-text-x-yaml">entryPoints:\n web:\n http: true</code></pre>`,
+ `<aside class="admonition tip"><p>A&nbsp;tip.</p></aside></details>`
+ ].join("");
+ expect(markdownService.renderToHtml(markdownExportService.toMarkdown(original), "Title")).toStrictEqual(original);
+ });
+ });
});
diff --git a/packages/trilium-core/src/services/import/markdown.ts b/packages/trilium-core/src/services/import/markdown.ts
index 26bd3ae04c..edd60e553c 100644
--- a/packages/trilium-core/src/services/import/markdown.ts
+++ b/packages/trilium-core/src/services/import/markdown.ts
@@ -1,16 +1,77 @@
import { renderToHtml as renderToHtmlShared } from "@triliumnext/commons/src/lib/markdown_renderer.js";
+import { HTMLElement, Node, NodeType, parse as parseHtml } from "node-html-parser";
import { sanitizeHtml } from "../sanitizer.js";
import { getTaskStates } from "../task_states.js";
function renderToHtml(content: string, title: string, opts?: { obsidian?: boolean }): string {
- return renderToHtmlShared(content, title, {
+ const html = renderToHtmlShared(content, title, {
sanitize: sanitizeHtml,
taskStates: getTaskStates(),
obsidian: opts?.obsidian
});
+ return normalizeCollapsibles(html);
}
export default {
renderToHtml
};
+
+/**
+ * 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.
+ */
+function normalizeCollapsibles(html: string): string {
+ if (!html.includes("<details")) {
+ return html;
+ }
+
+ const root = parseHtml(html);
+ const collapsibles = root.querySelectorAll("details");
+ if (!collapsibles.length) {
+ return html;
+ }
+
+ for (const collapsible of collapsibles) {
+ // The plugin builds the element around `class`, so writing the attributes back in that
+ // order keeps a collapsible the user has expanded (`open=""`) byte-identical too.
+ const { class: classNames, ...rest } = collapsible.attributes;
+ const classList = classNames ? classNames.split(/\s+/) : [];
+ if (!classList.includes(COLLAPSIBLE_CLASS)) {
+ classList.push(COLLAPSIBLE_CLASS);
+ }
+ collapsible.setAttributes({ class: classList.join(" "), ...rest });
+ removeLayoutWhitespace(collapsible);
+ }
+ 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.
+ */
+function removeLayoutWhitespace(element: HTMLElement) {
+ const isText = (node: Node) => node.nodeType === NodeType.TEXT_NODE;
+ const holdsText = (node: Node) => isText(node) && node.rawText.trim() !== "";
+
+ if (!element.childNodes.some(holdsText)) {
+ element.childNodes = element.childNodes.filter((node) => !isText(node) || holdsText(node));
+ }
+
+ for (const child of element.childNodes) {
+ if (child instanceof HTMLElement && BLOCK_CONTAINER_TAGS.has(child.tagName)) {
+ removeLayoutWhitespace(child);
+ }
+ }
+}
+
+/** 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"]);