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(markdown): keep the trailing semicolon in a style attribute on import
Elian Doran0e4171b1217d
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.tsindex f3591ae49e..fd7ad93370 100644--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.spec.ts@@ -297,6 +297,6 @@ describe("renderWithSourceLines", () => {it("renders a colour the exporter preserved as inline HTML", () => {// Non-default colours survive a Markdown export as a span, so the preview has to show them.expect(html(`a <span style="color:#ff0000">**red**</span> b`))- .toBe(`<p>a <span style="color:#ff0000"><strong>red</strong></span> b</p>`);+ .toBe(`<p>a <span style="color:#ff0000;"><strong>red</strong></span> b</p>`);});});diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.tsindex 310ffa807f..c532bccb73 100644--- a/packages/commons/src/lib/markdown_renderer.ts+++ b/packages/commons/src/lib/markdown_renderer.ts@@ -607,8 +607,9 @@ export function renderToHtml(content: string, title: string, options: RenderToHt}html = options.sanitize(html);- // Add a trailing semicolon to CSS styles.- html = html.replaceAll(/(<(img|figure|col).*?style=".*?)"/g, '$1;"');+ // Sanitization re-serializes a `style` attribute without the trailing semicolon CKEditor+ // writes, which would rewrite a note the first time it is saved after an import.+ html = html.replaceAll(/(<[a-z][^<>]*\sstyle="[^"]*[^";])"/gi, '$1;"');// Remove slash for self-closing tags to match CKEditor's approach.html = html.replace(/<(\w+)([^>]*)\s+\/>/g, "<$1$2>");diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.tsindex 95ae5efd12..0ab74af487 100644--- a/packages/trilium-core/src/services/import/markdown.spec.ts+++ b/packages/trilium-core/src/services/import/markdown.spec.ts@@ -453,6 +453,15 @@ $$`;expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);});+ it("keeps the trailing semicolon CKEditor writes into a style attribute", () => {+ // Without it, the first save after an import rewrites every styled element in the note.+ const input = `<ul style="list-style-type:disc;margin-left:0px;"><li style="margin-left:0px">Item</li></ul>`;+ const expected = `<ul style="list-style-type:disc;margin-left:0px;"><li style="margin-left:0px;">Item</li></ul>`;+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ // Sanitization drops an empty attribute, so nothing comes back holding `style=";"`.+ expect(markdownService.renderToHtml(`<p style="">Empty</p>`, "Title")).toStrictEqual(`<p>Empty</p>`);+ });+describe("collapsible blocks", () => {it("re-stamps the trilium-collapsible class and drops the indentation the exporter adds, so the block matches what the editor downcasts", () => {const input = trimIndentation`\diff --git a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.tsindex 5ec3f8494e..7934804625 100644--- a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts+++ b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts@@ -233,9 +233,9 @@ describe("Obsidian importer — integration", () => {});const note = importRoot.getChildNotes().find((n) => n.title === "Note");- // The highlight survives sanitization as a CKEditor background-colour span (the sanitizer drops the- // trailing semicolon); the comment is rendered as an HTML comment, which the sanitizer then strips.- expect(decodeUtf8(note?.getContent() ?? "")).toBe('<p>A <span style="background-color:hsl(60, 75%, 60%)">highlight</span> and a comment.</p>');+ // The highlight survives sanitization as a CKEditor background-colour span; the comment is+ // rendered as an HTML comment, which the sanitizer then strips.+ expect(decodeUtf8(note?.getContent() ?? "")).toBe('<p>A <span style="background-color:hsl(60, 75%, 60%);">highlight</span> and a comment.</p>');});it("drops the .base/.canvas files and the .obsidian config folder, but keeps an orphan attachment", async () => {diff --git a/packages/trilium-core/src/services/note_format_conversion.spec.ts b/packages/trilium-core/src/services/note_format_conversion.spec.tsindex f333216788..aa14b9ae41 100644--- a/packages/trilium-core/src/services/note_format_conversion.spec.ts+++ b/packages/trilium-core/src/services/note_format_conversion.spec.ts@@ -55,13 +55,13 @@ describe("note format conversion", () => {const original = "Some ==highlighted== text.";const { content: html } = convertNoteContent("markdown", original, "title");- expect(html).toContain('<span style="background-color:hsl(60, 75%, 60%)">highlighted</span>');+ expect(html).toContain('<span style="background-color:hsl(60, 75%, 60%);">highlighted</span>');expect(convertNoteContent("html", html, "title").content).toBe(original);});it("preserves a default-yellow highlight applied in the editor across an HTML -> Markdown -> HTML round-trip", () => {- const original = '<p>Some <span style="background-color:hsl(60, 75%, 60%)">highlighted</span> text.</p>';+ const original = '<p>Some <span style="background-color:hsl(60, 75%, 60%);">highlighted</span> text.</p>';const { content: markdown } = convertNoteContent("html", original, "title");expect(markdown).toBe("Some ==highlighted== text.");@@ -72,9 +72,9 @@ describe("note format conversion", () => {it("preserves a non-default colour exactly, rather than repainting it the highlight yellow", () => {// `==…==` carries no colour, so these keep their own markup in the Markdown form.for (const original of [- '<p>Some <span style="background-color:rgb(0, 255, 0)">green</span> text.</p>',- '<p>Some <span style="color:#ff0000">red</span> text.</p>',- '<p>Some <span style="color:#ffffff;background-color:#000000">inverted</span> text.</p>'+ '<p>Some <span style="background-color:rgb(0, 255, 0);">green</span> text.</p>',+ '<p>Some <span style="color:#ff0000;">red</span> text.</p>',+ '<p>Some <span style="color:#ffffff;background-color:#000000;">inverted</span> text.</p>']) {const { content: markdown } = convertNoteContent("html", original, "title");expect(convertNoteContent("markdown", markdown, "title").content).toBe(original);