Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): preserve image width/height attribute

Elian Doran319cccfb1507
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts
index 7de3dfab2d..0419e17654 100644
--- a/src/services/export/markdown.spec.ts
+++ b/src/services/export/markdown.spec.ts
@@ -250,4 +250,22 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
+ it("converts image if it has no custom properties", () => {
+ const html = /*html*/`<p><img src="Include Note_image.png"></p>`;
+ const expected = `![](Include%20Note_image.png)`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ });
+
+ it("preserves image verbatim if it has a width or height attribute", () => {
+ const scenarios = [
+ `<img src="Include Note_image.png" width="16" height="16">`,
+ `<img src="Include Note_image.png" width="16">`,
+ `<img src="Include Note_image.png" height="16">`
+ ];
+ for (const expected of scenarios) {
+ const html = /*html*/`<p>${expected}</p>`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ }
+ });
+
});
diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts
index 0bed882f9d..f3fb9f9fc8 100644
--- a/src/services/export/markdown.ts
+++ b/src/services/export/markdown.ts
@@ -97,7 +97,15 @@ function buildImageFilter() {
const imageFilter: TurndownService.Rule = {
filter: "img",
- replacement(content, node) {
+ replacement(content, _node) {
+ const node = _node as HTMLElement;
+
+ // Preserve image verbatim if it has a width or height attribute.
+ if (node.hasAttribute("width") || node.hasAttribute("height")) {
+ return node.outerHTML;
+ }
+
+ // TODO: Deduplicate with upstream.
const untypedNode = (node as any);
const alt = escapeMarkdown(cleanAttribute(untypedNode.getAttribute('alt')))
const src = escapeLinkDestination(untypedNode.getAttribute('src') || '')