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(import): convert Evernote line-divs to paragraphs on ENEX import
Elian Doranec4b7a040aa7
diff --git a/packages/trilium-core/src/services/import/enex_converter.spec.ts b/packages/trilium-core/src/services/import/enex_converter.spec.tsindex 753f04e60f..2f19a12a70 100644--- a/packages/trilium-core/src/services/import/enex_converter.spec.ts+++ b/packages/trilium-core/src/services/import/enex_converter.spec.ts@@ -59,13 +59,13 @@ describe("convertEnexContent — legacy checkboxes (<en-todo>)", () => {it("starts a new list when a non-todo line interrupts the run", () => {const input = `<div><en-todo checked="false"/>A</div><div>Heading</div><div><en-todo checked="true"/>B</div>`;expect(convertEnexContent(input)).toBe(- `${todoList(todoItem("A"))}<div>Heading</div>${todoList(todoItem("B", { checked: true }))}`+ `${todoList(todoItem("A"))}<p>Heading</p>${todoList(todoItem("B", { checked: true }))}`);});it("falls back to a unicode ballot box for an inline <en-todo> that isn't a line's checkbox", () => {const input = `<div>Mixed <en-todo checked="true"/>content</div>`;- expect(convertEnexContent(input)).toBe(`<div>Mixed ☑ content</div>`);+ expect(convertEnexContent(input)).toBe(`<p>Mixed ☑ content</p>`);});});@@ -157,12 +157,12 @@ describe("convertEnexContent — tasks (--en-task-group + <task> elements)", ()describe("convertEnexContent — admonitions (--en-callout)", () => {it("maps the default light-bulb callout to a tip admonition (emoji dropped)", () => {const input = `<div style="--en-callout:true; --en-emoji:💡;--en-requiredFeatures:"[\\"callout\\"]";"><div>Callout with default icon.</div></div>`;- expect(convertEnexContent(input)).toBe(`<aside class="admonition tip"><div>Callout with default icon.</div></aside>`);+ expect(convertEnexContent(input)).toBe(`<aside class="admonition tip"><p>Callout with default icon.</p></aside>`);});it("maps a custom-emoji callout to a note admonition with the emoji injected into the content", () => {const input = `<div style="--en-callout:true; --en-emoji:🤖;"><div>Callout with custom emoji.</div></div>`;- expect(convertEnexContent(input)).toBe(`<aside class="admonition note"><div>🤖 Callout with custom emoji.</div></aside>`);+ expect(convertEnexContent(input)).toBe(`<aside class="admonition note"><p>🤖 Callout with custom emoji.</p></aside>`);});});@@ -170,18 +170,47 @@ describe("convertEnexContent — toggles (--en-toggle)", () => {it("converts a collapsed toggle into a Trilium collapsible <details> (no open attribute)", () => {const input = `<div style="--en-toggle:true; --en-isCollapsed:true;"><div style="--en-toggleSummary:true;">Toggle goes here</div><div style="--en-toggleContent:true;"><div style="padding-left:40px;">Content goes here.</div><div style="padding-left:40px;"><br/></div></div></div>`;expect(convertEnexContent(input)).toBe(- `<details class="trilium-collapsible"><summary>Toggle goes here</summary><div style="padding-left:40px;">Content goes here.</div></details>`+ `<details class="trilium-collapsible"><summary>Toggle goes here</summary><p style="padding-left:40px;">Content goes here.</p></details>`);});it("preserves an expanded toggle's open state", () => {const input = `<div style="--en-toggle:true; --en-isCollapsed:false;"><div style="--en-toggleSummary:true;">S</div><div style="--en-toggleContent:true;"><div>Body</div></div></div>`;expect(convertEnexContent(input)).toBe(- `<details class="trilium-collapsible" open><summary>S</summary><div>Body</div></details>`+ `<details class="trilium-collapsible" open><summary>S</summary><p>Body</p></details>`);});});+describe("convertEnexContent — paragraphs (Evernote line-divs)", () => {+ it("converts an inline-content div into a paragraph, preserving attributes", () => {+ expect(convertEnexContent(`<div>Left align</div>`)).toBe(`<p>Left align</p>`);+ expect(convertEnexContent(`<div style="text-align:right;">Right align</div>`)).toBe(`<p style="text-align:right;">Right align</p>`);+ });++ it("keeps inline formatting and links inside the paragraph", () => {+ expect(convertEnexContent(`<div>A <b>bold</b> <a href="http://x.com">link</a></div>`)).toBe(`<p>A <b>bold</b> <a href="http://x.com">link</a></p>`);+ });++ it("turns an empty or <br>-only div into an empty paragraph", () => {+ expect(convertEnexContent(`<div><br/></div>`)).toBe(`<p> </p>`);+ expect(convertEnexContent(`<div></div>`)).toBe(`<p> </p>`);+ expect(convertEnexContent(`<div style="text-align:center;"><br/></div>`)).toBe(`<p> </p>`);+ });++ it("converts a run of line-divs into separate paragraphs, preserving blank lines", () => {+ expect(convertEnexContent(`<div>One</div><div><br/></div><div>Two</div>`)).toBe(`<p>One</p><p> </p><p>Two</p>`);+ });++ it("keeps an image line as a paragraph", () => {+ expect(convertEnexContent(`<div><en-media hash="h" type="image/png"/></div>`)).toBe(`<p><en-media hash="h" type="image/png"></en-media></p>`);+ });++ it("leaves a div wrapping block content alone (a paragraph can't hold block elements)", () => {+ expect(convertEnexContent(`<div><ul><li>x</li></ul></div>`)).toBe(`<div><ul><li>x</li></ul></div>`);+ });+});+describe("convertEnexContent — composition and inertness", () => {it("leaves ordinary HTML untouched", () => {expect(convertEnexContent(`<p>Hello</p>`)).toBe(`<p>Hello</p>`);diff --git a/packages/trilium-core/src/services/import/enex_converter.ts b/packages/trilium-core/src/services/import/enex_converter.tsindex b1cac8b210..64e5f61526 100644--- a/packages/trilium-core/src/services/import/enex_converter.ts+++ b/packages/trilium-core/src/services/import/enex_converter.ts@@ -35,6 +35,7 @@ export function convertEnexContent(html: string, tasks: EnexTask[] = []): stringconvertCodeBlocks(root);convertFormulaBlocks(root);convertMermaidBlocks(root);+ convertParagraphs(root);return root.toString();}@@ -392,6 +393,45 @@ function resolveCodeLanguage(language: string | undefined): string {}// #endregion+// #region Paragraphs+/** Block-level tags a `<p>` can't contain — a `<div>` holding any of these is left as a div, not paragraph-ised. */+const BLOCK_TAGS = new Set(["div", "p", "ul", "ol", "li", "table", "thead", "tbody", "tr", "td", "th", "figure", "pre", "blockquote", "aside", "details", "summary", "h1", "h2", "h3", "h4", "h5", "h6", "hr"]);++/**+ * Evernote wraps every line in a `<div>` rather than using paragraphs, so an imported note is otherwise a+ * wall of `<div>`s (which CKEditor keeps verbatim, unlike the `<p>`-based output of the other importers).+ * Runs last — once the special-block transforms have consumed their marker divs — to rewrite each remaining+ * inline-content `<div>` into a `<p>`, keeping its attributes (e.g. `text-align`). An empty or `<br>`-only+ * div (Evernote's blank line) becomes an empty paragraph; a div wrapping block content is left alone, since+ * a paragraph can't hold block elements.+ */+function convertParagraphs(root: HTMLElement) {+ for (const div of root.querySelectorAll("div")) {+ // A `<li>`'s wrapping div is unwrapped to bare text by the list-workaround regexes that run after the+ // converter, so leave it alone here rather than turning it into a `<li><p>…</p>` block list item.+ if (isTag(div.parentNode, "li")) {+ continue;+ }+ if (div.childNodes.some((node) => node instanceof HTMLElement && BLOCK_TAGS.has(node.tagName?.toLowerCase()))) {+ continue;+ }+ const paragraph = isBlankLine(div) ? "<p> </p>" : `<p${attributeString(div)}>${div.innerHTML}</p>`;+ div.insertAdjacentHTML("beforebegin", paragraph);+ div.remove();+ }+}++/** A blank line — an empty div, or one holding only a `<br>` — with no embedded image. */+function isBlankLine(div: HTMLElement): boolean {+ return (div.textContent ?? "").trim() === "" && div.querySelectorAll("img, en-media").length === 0;+}++/** The element's attributes as a leading-space string (`" style=…"`), or "" when it has none. */+function attributeString(el: HTMLElement): string {+ return el.rawAttrs ? ` ${el.rawAttrs}` : "";+}+// #endregion+// #region Shared helpers/** Renders a CKEditor to-do item; `checked` before `disabled` matches what the editor re-emits on save. */function renderTodoItem(checked: boolean, description: string, nested = ""): string {