Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): resolve Evernote internal note links to Trilium links

Elian Doranbf2e0072c184
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.ts
index 7331443be7..80c1fb429d 100644
--- a/packages/trilium-core/src/services/import/enex.spec.ts
+++ b/packages/trilium-core/src/services/import/enex.spec.ts
@@ -134,4 +134,29 @@ describe("importEnex", () => {
// The Evernote `--en-*` style markers must not leak into the imported note.
expect(content).not.toContain("--en-");
});
+
+ it("converts Evernote internal note links into Trilium reference links, leaving external links intact", async () => {
+ const { importedNote } = await testImport("Internal links.enex");
+
+ const first = importedNote.getChildNotes().find(n => n.title === "First Note");
+ const second = importedNote.getChildNotes().find(n => n.title === "Second Note");
+ if (!first || !second) {
+ throw new Error("cross-linked notes were not imported");
+ }
+
+ const firstContent = decodeUtf8(first.getContent());
+ // The internal link is resolved (by the inline-richlink's text = target title) to a reference link.
+ expect(firstContent).toContain(`href="#root/${second.noteId}"`);
+ expect(firstContent).toContain(`class="reference-link"`);
+ expect(firstContent).not.toContain("evernote://");
+ // The external link is left untouched.
+ expect(firstContent).toContain(`href="http://triliumnotes.org"`);
+
+ // The reverse link resolves too.
+ const secondContent = decodeUtf8(second.getContent());
+ expect(secondContent).toContain(`href="#root/${first.noteId}"`);
+
+ // An internalLink relation is created so backlinks/the link map work.
+ expect(first.getRelations("internalLink").some(r => r.value === second.noteId)).toBe(true);
+ });
}, 60_000);
diff --git a/packages/trilium-core/src/services/import/enex.ts b/packages/trilium-core/src/services/import/enex.ts
index 695e06f01d..8ba4867a63 100644
--- a/packages/trilium-core/src/services/import/enex.ts
+++ b/packages/trilium-core/src/services/import/enex.ts
@@ -10,7 +10,7 @@ import type TaskContext from "../task_context.js";
import { escapeHtml, md5 } from "../utils/index.js";
import { decodeBase64 } from "../utils/binary.js";
import type { File } from "./common.js";
-import { convertEnexContent, type EnexTask } from "./enex_converter.js";
+import { convertEnexContent, type EnexTask, rewriteEvernoteLinks } from "./enex_converter.js";
import { sanitizeHtml } from "../sanitizer.js";
/**
@@ -258,6 +258,12 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p
}
};
+ // Resolving Evernote internal note links needs every imported note's title -> id, which isn't fully
+ // known until all notes are created (a note can link to one parsed later). Collect the mapping and each
+ // note's final content during the first pass, then resolve the links in a second pass below.
+ const noteIdByTitle = new Map<string, string>();
+ const createdNotes: { note: BNote; content: string; utcDateCreated?: string; utcDateModified?: string }[] = [];
+
function saveNote() {
// make a copy because stream continues with the next call and note gets overwritten
let { title, content, attributes, resources, tasks, utcDateCreated, utcDateModified } = note;
@@ -364,6 +370,12 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p
noteService.asyncPostProcessContent(noteEntity, content);
noteEntity.setDateCreatedAndModified(utcDateCreated, utcDateModified);
+
+ // Record for the internal-link resolution pass (first occurrence wins on duplicate titles).
+ if (!noteIdByTitle.has(title)) {
+ noteIdByTitle.set(title, noteEntity.noteId);
+ }
+ createdNotes.push({ note: noteEntity, content, utcDateCreated, utcDateModified });
}
parser.onclosetag = (tag) => {
@@ -388,6 +400,20 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p
}
parser.close();
+ // Second pass: now that every imported note's title is known, resolve Evernote internal note links
+ // (`evernote://view-note/<guid>`) to Trilium reference links. setContent re-stamps the modification
+ // date, so the original timestamps are restored afterwards.
+ for (const { note: noteEntity, content, utcDateCreated, utcDateModified } of createdNotes) {
+ const rewritten = rewriteEvernoteLinks(content, (title) => noteIdByTitle.get(title) ?? null);
+ if (rewritten === content) {
+ continue;
+ }
+
+ noteEntity.setContent(rewritten);
+ noteService.asyncPostProcessContent(noteEntity, rewritten);
+ noteEntity.setDateCreatedAndModified(utcDateCreated, utcDateModified);
+ }
+
return rootNote;
}
diff --git a/packages/trilium-core/src/services/import/enex_converter.spec.ts b/packages/trilium-core/src/services/import/enex_converter.spec.ts
index 53061c920a..6777c73608 100644
--- a/packages/trilium-core/src/services/import/enex_converter.spec.ts
+++ b/packages/trilium-core/src/services/import/enex_converter.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { convertEnexContent } from "./enex_converter.js";
+import { convertEnexContent, rewriteEvernoteLinks } from "./enex_converter.js";
// Mirrors the canonical CKEditor todo-list serialization the converter emits (checked before disabled).
const todoItem = (desc: string, opts: { checked?: boolean; nested?: string } = {}) =>
@@ -183,3 +183,36 @@ describe("convertEnexContent — composition and inertness", () => {
);
});
});
+
+describe("rewriteEvernoteLinks — internal note references", () => {
+ // Evernote renders an inline-richlink with the target note's title as its text, so the target is
+ // resolved by that text. The map mirrors a title -> imported noteId lookup built during import.
+ const resolve = (text: string) => (({ "Orar legislație": "noteA", "Acte necesare înscriere": "noteB" }) as Record<string, string>)[text] ?? null;
+
+ it("rewrites an evernote://view-note link into a Trilium reference link when the text matches a note title", () => {
+ const input = `<div><a href="evernote://view-note/73bdd2cd-f542-4dc6-8e23-55e3566dd01d">Orar legislație</a></div>`;
+ expect(rewriteEvernoteLinks(input, resolve)).toBe(`<div><a href="#root/noteA" class="reference-link">Orar legislație</a></div>`);
+ });
+
+ it("handles the classic evernote:///view/... link format", () => {
+ const input = `<a href="evernote:///view/83639451/s1/4f73f255-780d-44aa-a2eb-20da435ea52d/4f73f255/">Acte necesare înscriere</a>`;
+ expect(rewriteEvernoteLinks(input, resolve)).toBe(`<a href="#root/noteB" class="reference-link">Acte necesare înscriere</a>`);
+ });
+
+ it("leaves external links untouched", () => {
+ const input = `<a href="http://triliumnotes.org">Text</a>`;
+ expect(rewriteEvernoteLinks(input, resolve)).toBe(input);
+ });
+
+ it("leaves an unresolvable internal link as-is", () => {
+ const input = `<a href="evernote://view-note/unknown">Some other note</a>`;
+ expect(rewriteEvernoteLinks(input, resolve)).toBe(input);
+ });
+
+ it("rewrites only the internal links, preserving surrounding content and external links", () => {
+ const input = `<p>See <a href="evernote://view-note/x">Orar legislație</a> and <a href="http://x.com">x</a>.</p>`;
+ expect(rewriteEvernoteLinks(input, resolve)).toBe(
+ `<p>See <a href="#root/noteA" class="reference-link">Orar legislație</a> and <a href="http://x.com">x</a>.</p>`
+ );
+ });
+});
diff --git a/packages/trilium-core/src/services/import/enex_converter.ts b/packages/trilium-core/src/services/import/enex_converter.ts
index 17eb377bd1..f86fad6958 100644
--- a/packages/trilium-core/src/services/import/enex_converter.ts
+++ b/packages/trilium-core/src/services/import/enex_converter.ts
@@ -37,6 +37,39 @@ export function convertEnexContent(html: string, tasks: EnexTask[] = []): string
return root.toString();
}
+// #region Internal note links
+/** Prefix shared by Evernote internal-note link schemes: `evernote://view-note/<guid>` and `evernote:///view/.../<guid>/...`. */
+const EVERNOTE_LINK_SCHEME = "evernote:";
+
+/**
+ * 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
+ * inline-richlink with the *target note's title* as its text, so the link is resolved by that text. A match
+ * becomes a Trilium reference link (`#root/<noteId>`, the live-title chip); an unresolved internal link and
+ * every external link are left untouched. Meant to run as a second pass, once every imported note's title is
+ * known (a note can link to one imported later).
+ */
+export function rewriteEvernoteLinks(html: string, resolve: (linkText: string) => string | null): string {
+ const root = parse(html);
+ let changed = false;
+
+ for (const anchor of root.querySelectorAll("a")) {
+ if (!(anchor.getAttribute("href") ?? "").startsWith(EVERNOTE_LINK_SCHEME)) {
+ continue;
+ }
+ const noteId = resolve(anchor.textContent.trim());
+ if (!noteId) {
+ continue;
+ }
+ anchor.setAttribute("href", `#root/${noteId}`);
+ anchor.setAttribute("class", "reference-link");
+ changed = true;
+ }
+
+ return changed ? root.toString() : html;
+}
+// #endregion
+
// #region Tasks
/**
* Evernote's newer Tasks feature exports each task group as a "Content not supported" placeholder div
diff --git a/packages/trilium-core/src/services/import/samples/Internal links.enex b/packages/trilium-core/src/services/import/samples/Internal links.enex
new file mode 100644
index 0000000000..c868f81a58
--- /dev/null
+++ b/packages/trilium-core/src/services/import/samples/Internal links.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>First Note</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="{&quot;isCached&quot;:false}" href="evernote://view-note/bbbb2222-0000-0000-0000-000000000002">Second Note</a></div><div><a href="http://triliumnotes.org">External</a></div></en-note>]]></content>
+ </note>
+ <note>
+ <title>Second 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><a type="inline-richlink" rev="{&quot;isCached&quot;:false}" href="evernote://view-note/aaaa1111-0000-0000-0000-000000000001">First Note</a></div></en-note>]]></content>
+ </note>
+</en-export>