Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): preserve figures

Elian Doranc80d016305da
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts
index 0419e17654..2a4e5fbf17 100644
--- a/src/services/export/markdown.spec.ts
+++ b/src/services/export/markdown.spec.ts
@@ -268,4 +268,15 @@ describe("Markdown export", () => {
}
});
+ it("preserves figures", () => {
+ const html = /*html*/trimIndentation`\
+ <figure class="image">
+ <img style="aspect-ratio:991/403;" src="Jump to Note_image.png" width="991"
+ height="403">
+ </figure>
+ `;
+ const expected = `<figure class="image"><img style="aspect-ratio:991/403;" src="Jump to Note_image.png" width="991" height="403"></figure>`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ });
+
});
diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts
index f3fb9f9fc8..fd125d0f66 100644
--- a/src/services/export/markdown.ts
+++ b/src/services/export/markdown.ts
@@ -45,6 +45,7 @@ function toMarkdown(content: string) {
instance.addRule("img", buildImageFilter());
instance.addRule("admonition", buildAdmonitionFilter());
instance.addRule("inlineLink", buildInlineLinkFilter());
+ instance.addRule("figure", buildFigureFilter());
instance.use(gfm);
instance.keep([ "kbd" ]);
}
@@ -195,6 +196,17 @@ function buildInlineLinkFilter(): Rule {
}
}
+function buildFigureFilter(): Rule {
+ return {
+ filter(node, options) {
+ return node.nodeName === 'FIGURE'
+ },
+ replacement(content, node) {
+ return (node as HTMLElement).outerHTML;
+ }
+ }
+}
+
// Taken from upstream since it's not exposed.
// https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.js
function cleanAttribute(attribute: string | null | undefined) {