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
feat(export): drop trilium-collapsible class from exported details
Elian Doran3613679f1157
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.tsindex e814abb498..36e4eb400f 100644--- a/packages/trilium-core/src/services/export/markdown.spec.ts+++ b/packages/trilium-core/src/services/export/markdown.spec.ts@@ -299,19 +299,29 @@ describe("Markdown export", () => {expect(markdownExportService.toMarkdown(html)).toBe(expected);});- it("preserves collapsible details blocks as pretty-printed raw HTML", () => {+ it("preserves collapsible details blocks as pretty-printed raw HTML, dropping the trilium-collapsible class", () => {const html = /*html*/`<details class="trilium-collapsible"><summary>Click to expand</summary><p>Body content</p></details>`;// Kept as raw HTML (Markdown has no disclosure syntax) but pretty-printed one- // child per line, mirroring how raw-HTML tables are serialized. Still- // round-trips losslessly through the markdown importer.+ // child per line, mirroring how raw-HTML tables are serialized. The Trilium-only+ // styling hook is stripped; it still round-trips through the markdown importer.const expected = trimIndentation`\- <details class="trilium-collapsible">+ <details><summary>Click to expand</summary><p>Body content</p></details>`;expect(markdownExportService.toMarkdown(html)).toBe(expected);});+ it("keeps user-added classes on a details block while dropping trilium-collapsible", () => {+ const html = /*html*/`<details class="custom trilium-collapsible"><summary>S</summary><p>B</p></details>`;+ const expected = trimIndentation`\+ <details class="custom">+ <summary>S</summary>+ <p>B</p>+ </details>`;+ expect(markdownExportService.toMarkdown(html)).toBe(expected);+ });+it("keeps a text-only details block verbatim so its content is not dropped", () => {const html = /*html*/`<details>Just some text</details>`;expect(markdownExportService.toMarkdown(html)).toBe(html);diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.tsindex 2d297ea6a4..c9abb5198b 100644--- a/packages/trilium-core/src/services/export/markdown.ts+++ b/packages/trilium-core/src/services/export/markdown.ts@@ -236,6 +236,11 @@ function buildFigureFilter(): Rule {* nested lists indent too; inline wrappers (<summary>, list-item <span>s, etc.)* are emitted verbatim on a single line, and a node holding direct text is kept* 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.*/function buildDetailsFilter(): Rule {// Block containers whose children are themselves blocks. Inline-content@@ -248,7 +253,12 @@ function buildDetailsFilter(): Rule {return node.nodeName === "DETAILS";},replacement(_content, node) {- return `\n\n${serializeStructuralHtml(node as HTMLElement, DETAILS_CONTAINER_TAGS)}\n\n`;+ const details = node as HTMLElement;+ details.classList.remove("trilium-collapsible");+ if (details.classList.length === 0) {+ details.removeAttribute("class");+ }+ return `\n\n${serializeStructuralHtml(details, DETAILS_CONTAINER_TAGS)}\n\n`;}};}