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): keep an inserted icon through the export
Elian Doran33c808e9d9df
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.tsindex 5392ba602f..5e5aa5de2c 100644--- a/packages/trilium-core/src/services/export/markdown.spec.ts+++ b/packages/trilium-core/src/services/export/markdown.spec.ts@@ -201,6 +201,25 @@ describe("Markdown export", () => {expect(markdownExportService.toMarkdown(html)).toBe(html);});+ it("keeps an icon, the space beside it and the colour around it", () => {+ // Turndown drops an element holding nothing before it consults a single rule, and+ // collapses the space beside it away with it, so the icon is given something to hold.+ const cog = `<span class="tn-icon bx bx-cog"></span>`;+ const star = `<span class="tn-icon bx bx-star"></span>`;+ const red = `<span style="color:rgb(255,0,0);">`;++ expect(markdownExportService.toMarkdown(`<p>Press ${cog} to open it.</p>`))+ .toBe(`Press ${cog} to open it.`);+ expect(markdownExportService.toMarkdown(`<p>Two ${cog}${star} in a row.</p>`))+ .toBe(`Two ${cog}${star} in a row.`);+ expect(markdownExportService.toMarkdown(`<p>a${red}${cog}</span>b</p>`))+ .toBe(`a${red}${cog}</span>b`);++ // A note that only talks about the class is returned as it came, not re-serialized.+ expect(markdownExportService.toMarkdown(`<p>The <code>tn-icon</code> class.</p>`))+ .toBe("The `tn-icon` class.");+ });+it("exports admonitions properly", () => {const html = trimIndentation`\<p>diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.tsindex 934e6a4c77..55cac236cb 100644--- a/packages/trilium-core/src/services/export/markdown.ts+++ b/packages/trilium-core/src/services/export/markdown.ts@@ -68,6 +68,7 @@ function toMarkdown(content: string) {instance.addRule("inlineLink", buildInlineLinkFilter());instance.addRule("figure", buildFigureFilter());instance.addRule("linkPreview", buildLinkPreviewFilter());+ instance.addRule("inlineIcon", buildInlineIconFilter());// 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());@@ -77,7 +78,53 @@ function toMarkdown(content: string) {instance.keep([ "kbd", "sup", "sub" ]);}- return instance.render(injectLinkPreviewFallbacks(content));+ return instance.render(injectIconFallbacks(injectLinkPreviewFallbacks(content)));+}++/** The class every icon in the application wears beside its pack's own. */+const ICON_CLASS = "tn-icon";++/**+ * Stands in for an icon's own content while Turndown runs. Turndown drops an element holding+ * nothing before it consults a single rule, and collapses the space beside it away with it.+ */+const ICON_PLACEHOLDER = String.fromCodePoint(0xE000);++function injectIconFallbacks(content: string): string {+ if (!content.includes(ICON_CLASS)) {+ return content;+ }++ const root = parseHtml(content);+ const icons = root.querySelectorAll(`span.${ICON_CLASS}`);+ // Returned as it came where the class was only ever mentioned, rather than serialized back out+ // of a parse that had nothing to change.+ if (!icons.length) {+ return content;+ }++ for (const icon of icons) {+ icon.textContent = ICON_PLACEHOLDER;+ }++ return root.toString();+}++function buildInlineIconFilter() {+ const iconFilter: Rule = {+ filter(node) {+ return node.nodeName === "SPAN" && node.classList.contains(ICON_CLASS);+ },+ replacement(_content, node) {+ // Written from the class alone, so an icon leaves in the form it is stored in and the+ // placeholder injected above never reaches the file.+ const classNames = "getAttribute" in node ? node.getAttribute("class") ?? "" : "";++ return `<span class="${escapeHtml(classNames)}"></span>`;+ }+ };++ return iconFilter;}function rewriteLanguageTag(source: string) {diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.tsindex 993327c6fc..0a9328a92d 100644--- a/packages/trilium-core/src/services/import/markdown.spec.ts+++ b/packages/trilium-core/src/services/import/markdown.spec.ts@@ -505,6 +505,23 @@ $$`;expect(markdownService.renderToHtml(`<p style="">Empty</p>`, "Title")).toStrictEqual(`<p>Empty</p>`);});+ it("brings an exported icon back in the form it left", () => {+ // The User Guide is kept as Markdown and read back from it, so an icon the exporter gives+ // up on is gone from the note as well on the next pass through `edit-docs`.+ const cog = `<span class="tn-icon bx bx-cog"></span>`;++ for (const html of [+ `<p>Press ${cog} to open it.</p>`,+ `<p>${cog}</p>`,+ `<p>a<span style="color:rgb(255,0,0);">${cog}</span>b</p>`,+ `<ul><li>In a list ${cog} here</li></ul>`+ ]) {+ const markdown = markdownExportService.toMarkdown(html);++ expect(markdownService.renderToHtml(markdown, "Title")).toStrictEqual(html);+ }+ });+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`\