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
fix(import/markdown): unable to import code blocks with XML/HTML content if safe mode on (closes #1530)
Elian Doran9a3f765d4264
diff --git a/src/services/import/markdown.spec.ts b/src/services/import/markdown.spec.tsindex 297b68e2f0..2a7330091c 100644--- a/src/services/import/markdown.spec.ts+++ b/src/services/import/markdown.spec.ts@@ -43,7 +43,6 @@ describe("markdown", () => {expect(result).toBe(`<h2>Hello</h2><h2>world</h2><h2>another one</h2><p>Hello, world</p>`);});-it("parses duplicate title with escape correctly", () => {const result = markdownService.renderToHtml(trimIndentation`\# What's new@@ -108,4 +107,24 @@ second line 2</code></pre><ul><li>Hello</li><li>world</li></ul><ol><li>Hello</liexpect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);});+ it("maintains code blocks with XML/HTML", () => {+ const input = trimIndentation`\+ Before+ \`\`\`+ <application+ ...+ android:testOnly="false">+ ...+ </application>+ \`\`\`+ After`;+ const expected = trimIndentation`\+ <p>Before</p><pre><code class="language-text-x-trilium-auto"><application+ ...+ android:testOnly="false">+ ...+ </application></code></pre><p>After</p>`;+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });+});diff --git a/src/services/import/markdown.ts b/src/services/import/markdown.tsindex 8f49730826..aa714d253d 100644--- a/src/services/import/markdown.ts+++ b/src/services/import/markdown.ts@@ -20,6 +20,12 @@ class CustomMarkdownRenderer extends Renderer {return "";}+ // Escape the HTML.+ text = utils.escapeHtml(text);++ // Unescape "+ text = text.replace(/"/g, '"');+const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;}@@ -66,6 +72,7 @@ import htmlSanitizer from "../html_sanitizer.js";import importUtils from "./utils.js";import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor } from "./mime_type_definitions.js";import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js";+import utils from "../utils.js";function renderToHtml(content: string, title: string) {let html = parse(content, {@@ -75,7 +82,7 @@ function renderToHtml(content: string, title: string) {// h1 handling needs to come before sanitizationhtml = importUtils.handleH1(html, title);- html = htmlSanitizer.sanitize(html);+ // html = htmlSanitizer.sanitize(html);// Remove slash for self-closing tags to match CKEditor's approach.html = html.replace(/<(\w+)([^>]*)\s+\/>/g, "<$1$2>");