Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): convert Obsidian note embeds to Trilium include-notes

Elian Doranb28d04634b87
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
index 987a3af802..25e5709db0 100644
--- a/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
+++ b/packages/trilium-core/src/services/import/obsidian/importer.integration.spec.ts
@@ -179,6 +179,26 @@ describe("Obsidian importer — integration", () => {
expect(b.getTargetRelations().filter((r) => r.name === "internalLink").map((r) => r.noteId)).toEqual([a.noteId]);
});
+ it("turns a note embed ![[Note]] into a Trilium include-note with an includeNoteLink relation", async () => {
+ // Modeled on the vault's "Reference links" note (`[[Formatting test]]` then `![[Formatting test]]`).
+ const importRoot = await importObsidian({ "Reference links.md": "[[Target]]\n\n![[Target]]", "Target.md": "I am the target." });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Reference links");
+ const target = importRoot.getChildNotes().find((n) => n.title === "Target");
+ if (!note || !target) {
+ throw new Error("notes were not imported");
+ }
+ const content = decodeUtf8(note.getContent());
+
+ // The plain wikilink is a reference link; the embed becomes an include-note pointing at the same note.
+ expect(content).toContain(`<a class="reference-link" href="#root/${target.noteId}">`);
+ expect(content).toContain(`<section class="include-note" data-note-id="${target.noteId}" data-box-size="medium">`);
+
+ // The embed records an includeNoteLink relation (the wikilink an internalLink).
+ expect(note.getRelations().filter((r) => r.name === "includeNoteLink").map((r) => r.value)).toEqual([target.noteId]);
+ expect(note.getRelations().filter((r) => r.name === "internalLink").map((r) => r.value)).toEqual([target.noteId]);
+ });
+
it("uses the alias as the link text for [[Target|alias]]", async () => {
const importRoot = await importObsidian({ "A.md": "[[B|the bee]]", "B.md": "b" });
diff --git a/packages/trilium-core/src/services/import/obsidian/importer.ts b/packages/trilium-core/src/services/import/obsidian/importer.ts
index c7a67cb7c4..000a250831 100644
--- a/packages/trilium-core/src/services/import/obsidian/importer.ts
+++ b/packages/trilium-core/src/services/import/obsidian/importer.ts
@@ -124,17 +124,20 @@ function createNotes(importRootNote: BNote, notes: VaultNote[], attachments: Map
taskContext.increaseProgressCount();
}
- // Second pass: now that every note's name -> id is known, resolve wikilinks to Trilium internal links and
- // record the resulting `internalLink` relations (so backlinks / "what links here" work).
+ // Second pass: now that every note's name -> id is known, resolve wikilinks and note embeds to Trilium
+ // internal links / include-notes, recording the relations that drive backlinks and "what links here".
const noteIndex = buildNoteIndex(created);
for (const { note, rendered, content } of created) {
- const { html, targets } = resolveLinks(content, noteIndex);
+ const { html, internalLinks, includeLinks } = resolveLinks(content, noteIndex);
if (html !== rendered) {
note.setContent(html);
}
- for (const target of targets) {
+ for (const target of internalLinks) {
note.addRelation("internalLink", target);
}
+ for (const target of includeLinks) {
+ note.addRelation("includeNoteLink", target);
+ }
}
return rootNote;
diff --git a/packages/trilium-core/src/services/import/obsidian/links.ts b/packages/trilium-core/src/services/import/obsidian/links.ts
index 7bf81e0161..985e3e21b6 100644
--- a/packages/trilium-core/src/services/import/obsidian/links.ts
+++ b/packages/trilium-core/src/services/import/obsidian/links.ts
@@ -39,13 +39,14 @@ export function buildNoteIndex(notes: { note: { noteId: string }; path: string }
return { byPath, byName };
}
-export function resolveLinks(html: string, index: NoteIndex): { html: string; targets: string[] } {
- // The wikilink extension emits a vault-relative `href="/…"`; nothing to do without one.
- if (!html.includes(`href="/`)) {
- return { html, targets: [] };
+export function resolveLinks(html: string, index: NoteIndex): { html: string; internalLinks: string[]; includeLinks: string[] } {
+ // Wikilinks render as a vault-relative `href="/…"` and note embeds as `src="/…"`; nothing to do without one.
+ if (!html.includes(`href="/`) && !html.includes(`src="/`)) {
+ return { html, internalLinks: [], includeLinks: [] };
}
const root = parse(html);
- const targets = new Set<string>();
+ const internalLinks = new Set<string>();
+ const includeLinks = new Set<string>();
let changed = false;
for (const anchor of root.querySelectorAll("a")) {
@@ -64,7 +65,7 @@ export function resolveLinks(html: string, index: NoteIndex): { html: string; ta
anchor.removeAttribute("class");
anchor.set_content(escapeHtml(alias));
}
- targets.add(noteId);
+ internalLinks.add(noteId);
} else {
anchor.insertAdjacentHTML("beforebegin", escapeHtml(alias ?? target));
anchor.remove();
@@ -72,7 +73,26 @@ export function resolveLinks(html: string, index: NoteIndex): { html: string; ta
changed = true;
}
- return { html: changed ? root.toString() : html, targets: [...targets] };
+ // Note embeds `![[Note]]` arrive as `<img src="/Note">` (the transclusion). One that resolves to a note
+ // becomes a Trilium include-note; an attachment image (already `api/attachments/…`) or an unresolved/.base
+ // embed is left untouched (the latter for a later pass).
+ for (const img of root.querySelectorAll("img")) {
+ const src = img.getAttribute("src");
+ if (!src || !src.startsWith("/")) {
+ continue;
+ }
+ const { target } = splitWikilink(safeDecode(src.slice(1)));
+ const noteId = resolveNote(index, target);
+ if (!noteId) {
+ continue;
+ }
+ img.insertAdjacentHTML("beforebegin", `<section class="include-note" data-note-id="${noteId}" data-box-size="medium">&nbsp;</section>`);
+ img.remove();
+ includeLinks.add(noteId);
+ changed = true;
+ }
+
+ return { html: changed ? root.toString() : html, internalLinks: [...internalLinks], includeLinks: [...includeLinks] };
}
/** Resolves a wikilink target to a noteId: an exact vault path first, then a unique base-name match. */