Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/markdown): maintain consistency with CKEditor for images

Elian Doran3eaa68da23aa
diff --git a/src/services/import/markdown.spec.ts b/src/services/import/markdown.spec.ts
index 18b11037b4..297b68e2f0 100644
--- a/src/services/import/markdown.spec.ts
+++ b/src/services/import/markdown.spec.ts
@@ -102,4 +102,10 @@ second line 2</code></pre><ul><li>Hello</li><li>world</li></ul><ol><li>Hello</li
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
+ it("imports images with same outcome as if inserted from CKEditor", () => {
+ const input = "![](api/attachments/YbkR3wt2zMcA/image/image)";
+ const expected = `<p><img src="api/attachments/YbkR3wt2zMcA/image/image"></p>`;
+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
+ });
+
});
diff --git a/src/services/import/markdown.ts b/src/services/import/markdown.ts
index c0677b5f15..8f49730826 100644
--- a/src/services/import/markdown.ts
+++ b/src/services/import/markdown.ts
@@ -34,6 +34,11 @@ class CustomMarkdownRenderer extends Renderer {
return super.listitem(item).trimEnd();
}
+ image(token: Tokens.Image): string {
+ return super.image(token)
+ .replace(` alt=""`, "");
+ }
+
blockquote({ tokens }: Tokens.Blockquote): string {
const body = renderer.parser.parse(tokens);
@@ -72,6 +77,9 @@ function renderToHtml(content: string, title: string) {
html = importUtils.handleH1(html, title);
html = htmlSanitizer.sanitize(html);
+ // Remove slash for self-closing tags to match CKEditor's approach.
+ html = html.replace(/<(\w+)([^>]*)\s+\/>/g, "<$1$2>");
+
return html;
}