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
feat(markdown): export highlights as ==text== instead of dropping them
Elian Dorand7ba1f6a9459
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.tsindex 8cef49dd52..5f1e41ead3 100644--- a/packages/trilium-core/src/services/export/markdown.spec.ts+++ b/packages/trilium-core/src/services/export/markdown.spec.ts@@ -772,4 +772,60 @@ describe("Markdown export", () => {expect(markdownExportService.toMarkdown(html)).toBe(expected);});+ describe("highlights", () => {+ it("renders a background-coloured span and a <mark> as ==text==", () => {+ expect(markdownExportService.toMarkdown(+ /*html*/`<p>A <span style="background-color:hsl(60, 75%, 60%);">highlight</span> here.</p>`+ )).toBe("A ==highlight== here.");++ // Whatever the colour, and however the style attribute is spelled.+ expect(markdownExportService.toMarkdown(+ /*html*/`<p><span style="color:#ff0000;background-color:rgb(0, 255, 0)">green</span></p>`+ )).toBe("==green==");++ expect(markdownExportService.toMarkdown(/*html*/`<p>A <mark>highlight</mark> here.</p>`))+ .toBe("A ==highlight== here.");+ });++ it("keeps inline formatting inside and around a highlight", () => {+ expect(markdownExportService.toMarkdown(+ /*html*/`<p><span style="background-color:yellow"><strong>bold</strong></span></p>`+ )).toBe("==**bold**==");++ expect(markdownExportService.toMarkdown(+ /*html*/`<p><strong><span style="background-color:yellow">bold</span></strong></p>`+ )).toBe("**==bold==**");+ });++ it("hoists whitespace out of the delimiters so the result parses back", () => {+ expect(markdownExportService.toMarkdown(+ /*html*/`<p>a<span style="background-color:yellow"> hi </span>b</p>`+ )).toBe("a ==hi== b");+ });++ it("leaves a font colour, a coloured block and a highlighted formula alone", () => {+ // `color` without a background is a font colour, not a highlight.+ expect(markdownExportService.toMarkdown(/*html*/`<p><span style="color:#ff0000">red</span></p>`))+ .toBe("red");++ // Only inline highlights are representable; a coloured block would wrap its whole text.+ expect(markdownExportService.toMarkdown(/*html*/`<p style="background-color:yellow">para</p>`))+ .toBe("para");++ expect(markdownExportService.toMarkdown(+ /*html*/`<p><span class="math-tex" style="background-color:yellow">\\(x^2\\)</span></p>`+ )).toBe("$x^2$");+ });++ it("emits no delimiters for a highlight with no text of its own", () => {+ expect(markdownExportService.toMarkdown(/*html*/`<p>a<span style="background-color:yellow"><br></span>b</p>`))+ .not.toContain("==");+ });++ it("does not touch == inside a code block", () => {+ const html = /*html*/`<pre><code class="language-text-x-trilium-auto">a ==b== c</code></pre>`;+ expect(markdownExportService.toMarkdown(html)).toBe("```\na ==b== c\n```");+ });+ });+});diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.tsindex e89febe8d7..673fa10f00 100644--- a/packages/trilium-core/src/services/export/markdown.ts+++ b/packages/trilium-core/src/services/export/markdown.ts@@ -67,6 +67,9 @@ function toMarkdown(content: string) {instance.addRule("inlineLink", buildInlineLinkFilter());instance.addRule("figure", buildFigureFilter());instance.addRule("linkPreview", buildLinkPreviewFilter());+ // Before "math": rules are consulted in reverse registration order, so a highlighted+ // formula stays a formula instead of being flattened into `==\(x\)==`.+ instance.addRule("highlight", buildHighlightFilter());instance.addRule("math", buildMathFilter());instance.addRule("li", buildListItemFilter());instance.use(gfm);@@ -408,6 +411,34 @@ function buildMathFilter(): Rule {};}+/**+ * `==text==` for highlighted text, in both shapes it reaches the exporter: CKEditor's Font+ * Background Color emits `<span style="background-color:…">`, and `<mark>` arrives from pasted+ * or imported HTML. Neither has a rule of its own, so without this the wrapper is dropped and+ * only the text survives — the highlight is silently lost on export.+ *+ * Markdown cannot express *which* colour, so every colour collapses to a plain highlight and+ * re-imports as the default yellow. A font colour (`color:` with no background) is not a+ * highlight and is deliberately left alone.+ */+function buildHighlightFilter(): Rule {+ return {+ filter(node) {+ if (node.nodeName === "MARK") {+ return true;+ }++ return node.nodeName === "SPAN"+ && /(?:^|;)\s*background-color\s*:/i.test(node.getAttribute("style") ?? "");+ },+ replacement(content: string) {+ // Turnish hoists flanking whitespace out of the delimiters, but an element whose only+ // content is a `<br>` still lands here; `== ==` would not parse back as a highlight.+ return content.trim() ? `==${content}==` : content;+ }+ };+}+// Taken from upstream since it's not exposed.// https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.jsfunction cleanAttribute(attribute: string | null | undefined) {diff --git a/packages/trilium-core/src/services/note_format_conversion.spec.ts b/packages/trilium-core/src/services/note_format_conversion.spec.tsindex 121c691673..c0cd4d2f9e 100644--- a/packages/trilium-core/src/services/note_format_conversion.spec.ts+++ b/packages/trilium-core/src/services/note_format_conversion.spec.ts@@ -50,6 +50,26 @@ describe("note format conversion", () => {expect(html).toContain("<h2>Heading</h2>");expect(html).toContain("<a href=\"https://example.com\">link</a>");});++ it("preserves a highlight across a Markdown -> HTML -> Markdown round-trip", () => {+ 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(convertNoteContent("html", html, "title").content).toBe(original);+ });++ it("preserves a highlight applied in the editor across an HTML -> Markdown -> HTML round-trip", () => {+ // Any highlight colour collapses to the default yellow, since Markdown cannot carry one.+ const original = '<p>Some <span style="background-color:rgb(0, 255, 0);">highlighted</span> text.</p>';++ const { content: markdown } = convertNoteContent("html", original, "title");+ expect(markdown).toBe("Some ==highlighted== text.");++ expect(convertNoteContent("markdown", markdown, "title").content)+ .toContain('<span style="background-color:hsl(60, 75%, 60%)">highlighted</span>');+ });});describe("note format conversion (real DB)", () => {