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): import Obsidian notes preserving folder structure
Elian Doran4fec75c8b6c2
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 f1d69460ca..09a3113444 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@@ -6,6 +6,7 @@ import becca from "../../../becca/becca.js";import type BNote from "../../../becca/entities/bnote.js";import { getContext } from "../../context.js";import TaskContext from "../../task_context.js";+import { decodeUtf8 } from "../../utils/binary.js";import obsidianImporter from "./importer.js";/** Builds an in-memory zip from a map of entry name -> contents. */@@ -40,22 +41,45 @@ async function importObsidian(files: Record<string, string | Buffer>, fileName?:}describe("Obsidian importer — integration", () => {- // Scaffold only: structure/content processing (folders → notes, Markdown → HTML, links, …) is not wired- // up yet, so for now the importer just creates the import root. These assertions lock in that contract.it("creates an 'Obsidian import' text root with an import icon", async () => {- const importRoot = await importObsidian({- "Welcome.md": "# Welcome\n\nFirst note.",- "Folder/Nested.md": "Nested note."- });+ const importRoot = await importObsidian({ "Note.md": "body" });expect(importRoot.title).toBe("Obsidian import");expect(importRoot.type).toBe("text");expect(importRoot.getOwnedLabelValue("iconClass")).toBe("bx bx-import");});- it("does not import any notes yet (processing arrives in a later pass)", async () => {- const importRoot = await importObsidian({ "Note.md": "body" }, "My Vault.zip");+ it("mirrors the vault folder structure, rendering each note's Markdown to HTML", async () => {+ const importRoot = await importObsidian({+ "Welcome.md": "This is your new *vault*.",+ "Folder 1/First note.md": "Content goes here.",+ "Folder 2/Folder 2 Note.md": "Nested content."+ });++ // The root holds the top-level note plus one container note per folder, named after the folder.+ const children = importRoot.getChildNotes();+ expect(children.map((n) => n.title)).toEqual(["Folder 1", "Folder 2", "Welcome"]);++ // Each folder note holds its notes; the Markdown body is rendered to HTML.+ const folder1 = children.find((n) => n.title === "Folder 1");+ expect(folder1?.getChildNotes().map((n) => n.title)).toEqual(["First note"]);+ expect(decodeUtf8(folder1?.getChildNotes()[0]?.getContent() ?? "")).toBe("<p>Content goes here.</p>");++ const welcome = children.find((n) => n.title === "Welcome");+ expect(decodeUtf8(welcome?.getContent() ?? "")).toBe("<p>This is your new <em>vault</em>.</p>");+ });++ it("drops attachments, the .base/.canvas files and the .obsidian config folder", async () => {+ const importRoot = await importObsidian({+ "Note.md": "Body.",+ "Screenshot.png": Buffer.from("\x89PNG\r\n\x1a\nfake"),+ "Untitled.base": "filters: {}",+ "Untitled.canvas": "{}",+ ".obsidian/app.json": "{}",+ ".obsidian/workspace.json": "{}"+ });- expect(importRoot.getChildNotes()).toHaveLength(0);+ // Only the Markdown note is imported; nothing else creates a note.+ expect(importRoot.getChildNotes().map((n) => n.title)).toEqual(["Note"]);});});diff --git a/packages/trilium-core/src/services/import/obsidian/importer.ts b/packages/trilium-core/src/services/import/obsidian/importer.tsindex 78428056f6..c58312e791 100644--- a/packages/trilium-core/src/services/import/obsidian/importer.ts+++ b/packages/trilium-core/src/services/import/obsidian/importer.ts@@ -1,9 +1,13 @@/*** Imports an Obsidian vault (a zipped folder of Markdown files plus attachments) into a Trilium note tree.*- * This is the integration scaffold only: it creates a fresh "Obsidian import" root and returns it. Vault- * traversal and structure/content processing — folders → subtree, Markdown → HTML, wikilinks, embeds,- * frontmatter → labels, tags, callouts, attachments, `.canvas`/`.excalidraw.md` — land in later passes.+ * This pass reconstructs the *structure*: every `.md` file becomes a `text` note whose Markdown body is+ * rendered to HTML, and the vault's folder hierarchy is mirrored as the note tree (a folder becomes an empty+ * container note holding its notes). The `.obsidian/` config folder and any other dot-prefixed entry are+ * skipped, and non-Markdown files (attachments, `.canvas`, `.base`, …) are dropped for now.+ *+ * Deferred to later passes (so they currently render as literal text or placeholder links): wikilinks+ * `[[…]]`, embeds `![[…]]`, frontmatter → labels, tags, callouts, highlights/comments, and attachments.** Invoked from the shared file-import dispatcher (routes/api/import.ts) when the upload is tagged* `format=obsidian`, so progress, completion and failure are reported by that dispatcher's TaskContext —@@ -16,18 +20,114 @@ import type BNote from "../../../becca/entities/bnote.js";import noteService from "../../notes.js";import protectedSessionService from "../../protected_session.js";import type TaskContext from "../../task_context.js";+import { decodeUtf8 } from "../../utils/binary.js";+import { basename } from "../../utils/path.js";+import { getZipProvider } from "../../zip_provider.js";+import markdownService from "../markdown.js";++interface VaultNote {+ /** The note's normalized POSIX path within the vault, e.g. `Folder 1/First note.md`. */+ path: string;+ title: string;+ markdown: string;+}++async function importObsidian(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote, _fileName?: string): Promise<BNote> {+ const notes = await parseVault(fileBuffer);+ taskContext.setTotalCount(notes.length);-async function importObsidian(taskContext: TaskContext<"importNotes">, _fileBuffer: Uint8Array, importRootNote: BNote, _fileName?: string): Promise<BNote> {+ return createNotes(importRootNote, notes, taskContext);+}++/**+ * Reads the vault zip, collecting one {@link VaultNote} per Markdown file. The `.obsidian/` config folder+ * and any other dot-prefixed entry are skipped, as is every non-Markdown file (handled in later passes).+ * Sorted by path so the resulting tree is built (and ordered) deterministically.+ */+async function parseVault(fileBuffer: Uint8Array): Promise<VaultNote[]> {+ const provider = getZipProvider();+ const notes: VaultNote[] = [];+ const filenameEncoding = await provider.detectFilenameEncoding(fileBuffer);++ await provider.readZipFile(fileBuffer, async (entry, readContent) => {+ const path = normalizePath(entry.fileName);+ if (isDirectory(path) || isIgnored(path) || !isMarkdown(path)) {+ return;+ }+ notes.push({ path, title: noteTitle(path), markdown: decodeUtf8(await readContent()) });+ }, filenameEncoding);++ notes.sort((a, b) => a.path.localeCompare(b.path));+ return notes;+}++/**+ * Builds the note tree under a fresh "Obsidian import" root. Each note is parented under the container note+ * for its folder (created on demand by {@link ensureFolder}, so a folder note exists before its children),+ * with its Markdown rendered to HTML. Returns the import root.+ */+function createNotes(importRootNote: BNote, notes: VaultNote[], taskContext: TaskContext<"importNotes">): BNote {/* v8 ignore next -- the protected branch needs a protected import root with an active protected session, which the in-memory test DB has no way to set up */- const isProtected = importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable();+ const isProtected = !!(importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable());const rootNote = noteService.createNewNote({ parentNoteId: importRootNote.noteId, title: t("obsidian_import.root-title"), content: "", type: "text", mime: "text/html", isProtected }).note;rootNote.addLabel("iconClass", "bx bx-import");- // No notes imported yet — structure processing arrives in a later pass.- taskContext.setTotalCount(0);+ // Folder path (POSIX) -> its container note. The empty path maps to the import root.+ const folderNotes = new Map<string, BNote>();++ for (const note of notes) {+ const parent = ensureFolder(parentFolder(note.path), rootNote, folderNotes, isProtected);+ const content = markdownService.renderToHtml(note.markdown, note.title);+ noteService.createNewNote({ parentNoteId: parent.noteId, title: note.title, content, type: "text", mime: "text/html", isProtected });+ taskContext.increaseProgressCount();+ }return rootNote;}+/** Returns (creating on demand, parents first) the container note for `folderPath`; the empty path is the root. */+function ensureFolder(folderPath: string, rootNote: BNote, folderNotes: Map<string, BNote>, isProtected: boolean): BNote {+ if (folderPath === "") {+ return rootNote;+ }+ const cached = folderNotes.get(folderPath);+ if (cached) {+ return cached;+ }+ const parent = ensureFolder(parentFolder(folderPath), rootNote, folderNotes, isProtected);+ const { note } = noteService.createNewNote({ parentNoteId: parent.noteId, title: basename(folderPath), content: "", type: "text", mime: "text/html", isProtected });+ folderNotes.set(folderPath, note);+ return note;+}++/** The POSIX parent-folder path of `path` (everything before the last `/`), or `""` when at the vault root. */+function parentFolder(path: string): string {+ const slash = path.lastIndexOf("/");+ return slash === -1 ? "" : path.slice(0, slash);+}++function normalizePath(path: string): string {+ return path.replace(/\\/g, "/");+}++/** A zip directory entry (trailing slash) carries no content. */+function isDirectory(path: string): boolean {+ return path.endsWith("/");+}++/** Skips the `.obsidian/` config folder and every other dot-prefixed entry (`.trash/`, `.DS_Store`, …). */+function isIgnored(path: string): boolean {+ return path.split("/").some((segment) => segment.startsWith("."));+}++function isMarkdown(path: string): boolean {+ return path.toLowerCase().endsWith(".md");+}++/** The note title for a Markdown file: its base name without the `.md` extension. */+function noteTitle(path: string): string {+ return basename(path).replace(/\.md$/i, "");+}+export default { importObsidian };