Skip to content

Build your personal knowledge base with Trilium Notes

text editor/collapsible: add support for markdown export

Adorian Doran7dd7e81de044
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.ts
index 3e2f7f45b8..e0cbb66fe2 100644
--- a/packages/trilium-core/src/services/export/markdown.spec.ts
+++ b/packages/trilium-core/src/services/export/markdown.spec.ts
@@ -283,6 +283,12 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
+ it("preserves collapsible details blocks as raw HTML", () => {
+ const html = /*html*/`<details class="trilium-collapsible"><summary>Click to expand</summary><p>Body content</p></details>`;
+ // Passthrough — round-trips losslessly through the markdown importer.
+ expect(markdownExportService.toMarkdown(html)).toBe(html);
+ });
+
it("converts inline math expressions into proper Markdown syntax", () => {
const html = /*html*/String.raw`<span class="math-tex">\(H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}\)</span></span>`;
const expected = String.raw`$H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}$`;
diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.ts
index 882e72003f..b23f619569 100644
--- a/packages/trilium-core/src/services/export/markdown.ts
+++ b/packages/trilium-core/src/services/export/markdown.ts
@@ -52,6 +52,7 @@ function toMarkdown(content: string) {
instance.addRule("fencedCodeBlock", fencedCodeBlockFilter);
instance.addRule("img", buildImageFilter());
instance.addRule("admonition", buildAdmonitionFilter());
+ instance.addRule("details", buildDetailsFilter());
instance.addRule("inlineLink", buildInlineLinkFilter());
instance.addRule("figure", buildFigureFilter());
instance.addRule("math", buildMathFilter());
@@ -218,6 +219,27 @@ function buildFigureFilter(): Rule {
};
}
+/**
+ * Markdown has no native syntax for disclosure widgets, but GitHub, Obsidian
+ * and most CommonMark+HTML renderers accept raw <details>/<summary> verbatim
+ * — and Trilium's markdown importer already parses them back into the
+ * collapsible model node, so passthrough round-trips losslessly.
+ *
+ * We match on tag name only (not on the trilium-collapsible class) so any
+ * pasted/imported <details> is preserved too; stripping it to plain text
+ * would silently lose structure.
+ */
+function buildDetailsFilter(): Rule {
+ return {
+ filter(node) {
+ return node.nodeName === "DETAILS";
+ },
+ replacement(_content, node) {
+ return `\n\n${(node as HTMLElement).outerHTML}\n\n`;
+ }
+ };
+}
+
// Keep in line with https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.js.
function buildListItemFilter(): Rule {
return {