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): strip Obsidian embeds of .base/.canvas files
Elian Dorana20fd9f1df31
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.tsindex 25e5709db0..e373123660 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@@ -150,16 +150,16 @@ describe("Obsidian importer — integration", () => {expect(page?.getAttachmentsByRole("image").map((a) => a.title)).toEqual(["pic.png"]);});- it("leaves note embeds and non-imported targets untouched (handled by later passes)", async () => {+ it("strips an embed of a database (.base) that has no Trilium representation yet", async () => {+ // Modeled on the vault's "Formatting test" note, which embeds a Base via `![[Untitled.base]]`.const importRoot = await importObsidian({- "Note.md": "![[Other note]]\n\n![[Untitled.base]]",- "Other note.md": "I am a note.",+ "Note.md": "Body.\n\n![[Untitled.base]]","Untitled.base": "filters: {}"});const note = importRoot.getChildNotes().find((n) => n.title === "Note");- // Neither a note embed nor a .base resolves to an attachment, so nothing is saved in this pass.- expect(note?.getAttachmentsByRole("image")).toHaveLength(0);+ // The base embed is removed cleanly — no broken image and no empty paragraph left behind.+ expect(decodeUtf8(note?.getContent() ?? "")).toBe("<p>Body.</p>");expect(note?.getAttachmentsByRole("file")).toHaveLength(0);});diff --git a/packages/trilium-core/src/services/import/obsidian/links.ts b/packages/trilium-core/src/services/import/obsidian/links.tsindex 985e3e21b6..e07507eed4 100644--- a/packages/trilium-core/src/services/import/obsidian/links.ts+++ b/packages/trilium-core/src/services/import/obsidian/links.ts@@ -15,7 +15,7 @@* for recording `internalLink` relations (backlinks).*/-import { parse } from "node-html-parser";+import { HTMLElement, parse } from "node-html-parser";import { escapeHtml } from "../../utils/index.js";import { basename } from "../../utils/path.js";@@ -84,6 +84,12 @@ export function resolveLinks(html: string, index: NoteIndex): { html: string; inconst { target } = splitWikilink(safeDecode(src.slice(1)));const noteId = resolveNote(index, target);if (!noteId) {+ // An embed of a database (.base) or whiteboard (.canvas) has no Trilium representation yet, so+ // drop the placeholder rather than leave a broken image; other unresolved embeds are left alone.+ if (isUnsupportedEmbed(target)) {+ removeEmbed(img);+ changed = true;+ }continue;}img.insertAdjacentHTML("beforebegin", `<section class="include-note" data-note-id="${noteId}" data-box-size="medium"> </section>`);@@ -95,6 +101,21 @@ export function resolveLinks(html: string, index: NoteIndex): { html: string; inreturn { html: changed ? root.toString() : html, internalLinks: [...internalLinks], includeLinks: [...includeLinks] };}+/** Whether an embed targets a database (`.base`) or whiteboard (`.canvas`) — both unsupported for now. */+function isUnsupportedEmbed(target: string): boolean {+ const lower = target.toLowerCase();+ return lower.endsWith(".base") || lower.endsWith(".canvas");+}++/** Removes an embed element, also dropping its wrapping paragraph if that leaves it empty. */+function removeEmbed(img: HTMLElement): void {+ const parent = img.parentNode;+ img.remove();+ if (parent instanceof HTMLElement && parent.tagName?.toLowerCase() === "p" && (parent.textContent ?? "").trim() === "" && parent.querySelectorAll("img").length === 0) {+ parent.remove();+ }+}+/** Resolves a wikilink target to a noteId: an exact vault path first, then a unique base-name match. */function resolveNote(index: NoteIndex, target: string): string | null {if (!target) {