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
refactor(import): refine ENEX internal-link resolution
Elian Doran16597554fa87
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.tsindex 7dcf0cfdf0..3ab09c3e4b 100644--- a/packages/trilium-core/src/services/import/enex.spec.ts+++ b/packages/trilium-core/src/services/import/enex.spec.ts@@ -180,6 +180,26 @@ describe("importEnex", () => {expect(linker.getRelations("internalLink").some(r => dupIds.includes(r.value))).toBe(false);});+ it("resolves an internal link to a note whose title has surrounding whitespace", async () => {+ const { importedNote } = await testImport("Internal links whitespace.enex");++ const linker = importedNote.getChildNotes().find(n => n.title === "Linker");+ // The target's exported title is padded (" Padded Note "); match it trim-insensitively.+ const padded = importedNote.getChildNotes().find(n => n.title.trim() === "Padded Note");+ if (!linker || !padded) {+ throw new Error("whitespace-title sample notes were not imported");+ }+ const content = decodeUtf8(linker.getContent());++ // The link text ("Padded Note") is matched against the padded title after trimming both sides,+ // so it still resolves to a reference link rather than being left as the raw evernote:// link.+ expect(content).toContain(`href="#root/${padded.noteId}"`);+ expect(content).toContain(`class="reference-link"`);+ expect(content).not.toContain("evernote://");+ // The backlink relation is created too.+ expect(linker.getRelations("internalLink").some(r => r.value === padded.noteId)).toBe(true);+ });+it("converts legacy <en-todo> checkboxes into a real todo-list, not literal ballot boxes", async () => {const { importedNote } = await testImport("Legacy checkboxes.enex");diff --git a/packages/trilium-core/src/services/import/enex.ts b/packages/trilium-core/src/services/import/enex.tsindex 21124e8dfa..73d388fc45 100644--- a/packages/trilium-core/src/services/import/enex.ts+++ b/packages/trilium-core/src/services/import/enex.ts@@ -370,7 +370,9 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p// Record for the internal-link resolution pass. Internal links are matched by the target's title// (the export carries no per-note guid to match on), so a title shared by 2+ notes is ambiguous:// mark it null to leave those links unresolved rather than pointing at the wrong same-named note.- noteIdByTitle.set(title, noteIdByTitle.has(title) ? null : noteEntity.noteId);+ // Key on the trimmed title so a padded export title still matches a link's (trimmed) anchor text.+ const titleKey = title.trim();+ noteIdByTitle.set(titleKey, noteIdByTitle.has(titleKey) ? null : noteEntity.noteId);createdNotes.push({ note: noteEntity, content, utcDateCreated, utcDateModified });}diff --git a/packages/trilium-core/src/services/import/enex_converter.spec.ts b/packages/trilium-core/src/services/import/enex_converter.spec.tsindex 0af79fcbee..753f04e60f 100644--- a/packages/trilium-core/src/services/import/enex_converter.spec.ts+++ b/packages/trilium-core/src/services/import/enex_converter.spec.ts@@ -1,4 +1,5 @@-import { describe, expect, it } from "vitest";+import * as htmlParser from "node-html-parser";+import { describe, expect, it, vi } from "vitest";import { convertEnexContent, rewriteEvernoteLinks } from "./enex_converter.js";@@ -239,4 +240,20 @@ describe("rewriteEvernoteLinks — internal note references", () => {`<p>See <a href="#root/noteA" class="reference-link">Orar legislație</a> and <a href="http://x.com">x</a>.</p>`);});++ it("only parses when an evernote link is present (fast path skips parsing otherwise)", () => {+ const parseSpy = vi.spyOn(htmlParser, "parse");+ try {+ // Content with no evernote link is returned untouched without paying for an HTML parse.+ const noLinks = `<p>No internal links</p><a href="http://triliumnotes.org">External</a>`;+ expect(rewriteEvernoteLinks(noLinks, resolve)).toBe(noLinks);+ expect(parseSpy).not.toHaveBeenCalled();++ // A present evernote link still triggers a parse so it can be rewritten.+ rewriteEvernoteLinks(`<a href="evernote://view-note/x">Orar legislație</a>`, resolve);+ expect(parseSpy).toHaveBeenCalledOnce();+ } finally {+ parseSpy.mockRestore();+ }+ });});diff --git a/packages/trilium-core/src/services/import/enex_converter.ts b/packages/trilium-core/src/services/import/enex_converter.tsindex 9799eb8e39..b1cac8b210 100644--- a/packages/trilium-core/src/services/import/enex_converter.ts+++ b/packages/trilium-core/src/services/import/enex_converter.ts@@ -42,6 +42,11 @@ export function convertEnexContent(html: string, tasks: EnexTask[] = []): string/** Prefix shared by Evernote internal-note link schemes: `evernote://view-note/<guid>` and `evernote:///view/.../<guid>/...`. */const EVERNOTE_LINK_SCHEME = "evernote:";+/** Whether `html` contains any Evernote internal-note link — the cheap guard that lets {@link rewriteEvernoteLinks} skip parsing. */+export function hasEvernoteLink(html: string): boolean {+ return html.includes(EVERNOTE_LINK_SCHEME);+}+/*** Rewrites Evernote internal note links into Trilium internal links. An ENEX export never carries a note's* own guid, so the `evernote://view-note/<guid>` target can't be matched by id; instead Evernote renders an@@ -51,6 +56,12 @@ const EVERNOTE_LINK_SCHEME = "evernote:";* known (a note can link to one imported later).*/export function rewriteEvernoteLinks(html: string, resolve: (linkText: string) => string | null): string {+ // Fast path: with no Evernote link anywhere there is nothing to rewrite, so skip the HTML parse —+ // this runs over every imported note's content, most of which have no internal links.+ if (!hasEvernoteLink(html)) {+ return html;+ }+const root = parse(html);let changed = false;diff --git a/packages/trilium-core/src/services/import/samples/Internal links whitespace.enex b/packages/trilium-core/src/services/import/samples/Internal links whitespace.enexnew file mode 100644index 0000000000..fba69778ae--- /dev/null+++ b/packages/trilium-core/src/services/import/samples/Internal links whitespace.enex@@ -0,0 +1,18 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">+<en-export export-date="20260101T000000Z" application="Evernote" version="11">+ <note>+ <title>Linker</title>+ <created>20200101T000000Z</created>+ <updated>20200102T000000Z</updated>+ <content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>+<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><a type="inline-richlink" rev="{"isCached":false}" href="evernote://view-note/cccc3333-0000-0000-0000-000000000003">Padded Note</a></div></en-note>]]></content>+ </note>+ <note>+ <title> Padded Note </title>+ <created>20200103T000000Z</created>+ <updated>20200104T000000Z</updated>+ <content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>+<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div>Body of the padded note.</div></en-note>]]></content>+ </note>+</en-export>