Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): don't resolve ENEX internal links on duplicate titles

Elian Doran09376909d5dc
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.ts
index 80c1fb429d..75afd000d7 100644
--- a/packages/trilium-core/src/services/import/enex.spec.ts
+++ b/packages/trilium-core/src/services/import/enex.spec.ts
@@ -159,4 +159,24 @@ describe("importEnex", () => {
// An internalLink relation is created so backlinks/the link map work.
expect(first.getRelations("internalLink").some(r => r.value === second.noteId)).toBe(true);
});
+
+ it("leaves a link to a duplicated title unresolved (avoids guessing the wrong same-named note)", async () => {
+ const { importedNote } = await testImport("Duplicate titles.enex");
+
+ const linker = importedNote.getChildNotes().find(n => n.title === "Linker");
+ const target = importedNote.getChildNotes().find(n => n.title === "Target");
+ if (!linker || !target) {
+ throw new Error("duplicate-title sample notes were not imported");
+ }
+ const content = decodeUtf8(linker.getContent());
+
+ // "Dup" is shared by two imported notes, so the link is ambiguous and kept as the original link.
+ expect(content).toContain(`href="evernote://view-note/dddddddd-dddd-dddd-dddd-dddddddddddd"`);
+ // A uniquely-titled target still resolves to a reference link.
+ expect(content).toContain(`href="#root/${target.noteId}"`);
+
+ // No internalLink relation is created toward either ambiguous "Dup" note.
+ const dupIds = importedNote.getChildNotes().filter(n => n.title === "Dup").map(n => n.noteId);
+ expect(linker.getRelations("internalLink").some(r => dupIds.includes(r.value))).toBe(false);
+ });
}, 60_000);
diff --git a/packages/trilium-core/src/services/import/enex.ts b/packages/trilium-core/src/services/import/enex.ts
index 8ba4867a63..1027500785 100644
--- a/packages/trilium-core/src/services/import/enex.ts
+++ b/packages/trilium-core/src/services/import/enex.ts
@@ -261,7 +261,8 @@ 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>();
+ // Value is the note's id, or null when the title is shared by 2+ imported notes (see saveNote).
+ const noteIdByTitle = new Map<string, string | null>();
const createdNotes: { note: BNote; content: string; utcDateCreated?: string; utcDateModified?: string }[] = [];
function saveNote() {
@@ -371,10 +372,10 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p
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);
- }
+ // 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);
createdNotes.push({ note: noteEntity, content, utcDateCreated, utcDateModified });
}
diff --git a/packages/trilium-core/src/services/import/samples/Duplicate titles.enex b/packages/trilium-core/src/services/import/samples/Duplicate titles.enex
new file mode 100644
index 0000000000..f130dd5d46
--- /dev/null
+++ b/packages/trilium-core/src/services/import/samples/Duplicate titles.enex
@@ -0,0 +1,28 @@
+<?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>Dup</title>
+ <created>20200101T000000Z</created>
+ <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>First note named Dup.</div></en-note>]]></content>
+ </note>
+ <note>
+ <title>Dup</title>
+ <created>20200102T000000Z</created>
+ <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>Second note named Dup.</div></en-note>]]></content>
+ </note>
+ <note>
+ <title>Target</title>
+ <created>20200103T000000Z</created>
+ <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>The uniquely-named target.</div></en-note>]]></content>
+ </note>
+ <note>
+ <title>Linker</title>
+ <created>20200104T000000Z</created>
+ <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" href="evernote://view-note/dddddddd-dddd-dddd-dddd-dddddddddddd">Dup</a></div><div><a type="inline-richlink" href="evernote://view-note/11111111-1111-1111-1111-111111111111">Target</a></div></en-note>]]></content>
+ </note>
+</en-export>