Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): route a dropped Obsidian vault to its own importer

Elian Doran7d4197e29af6
diff --git a/packages/trilium-core/src/services/import/dispatch.spec.ts b/packages/trilium-core/src/services/import/dispatch.spec.ts
index 5896b82795..86af611047 100644
--- a/packages/trilium-core/src/services/import/dispatch.spec.ts
+++ b/packages/trilium-core/src/services/import/dispatch.spec.ts
@@ -115,7 +115,7 @@ describe("importFile (dispatch)", () => {
it("routes a .zip to the generic zip importer when archives are exploded", async () => {
const file = makeFile({ originalname: "backup.zip", path: "/tmp/b.zip" });
await importFile(taskContext, file, parentNote, makeOptions());
- expect(stubbed.importZip).toHaveBeenCalledWith(taskContext, { path: "/tmp/b.zip" }, parentNote);
+ expect(stubbed.importZip).toHaveBeenCalledWith(taskContext, { path: "/tmp/b.zip" }, parentNote, { onObsidianVault: expect.any(Function) });
});
it("imports a .zip as a single file when archives are not exploded", async () => {
@@ -150,4 +150,16 @@ describe("importFile (dispatch)", () => {
expect(stubbed.importSingleFile).toHaveBeenCalledWith(taskContext, file, parentNote);
});
});
+
+ describe("Obsidian detection (an untagged .zip, e.g. dropped on the note tree)", () => {
+ it("hands the zip importer an Obsidian fallback that imports the vault in place of the archive", async () => {
+ const file = makeFile({ originalname: "MyVault.zip", path: "/tmp/v.zip" });
+ await importFile(taskContext, file, parentNote, makeOptions());
+
+ // The zip importer's scan pass decides whether to call this; recognition is covered in zip.spec.ts.
+ const opts = stubbed.importZip.mock.calls[0]?.[3];
+ await opts?.onObsidianVault?.();
+ expect(stubbed.importObsidian).toHaveBeenCalledWith(taskContext, { path: "/tmp/v.zip" }, parentNote, "MyVault.zip");
+ });
+ });
});
diff --git a/packages/trilium-core/src/services/import/dispatch.ts b/packages/trilium-core/src/services/import/dispatch.ts
index c3a52ba8ef..54db284599 100644
--- a/packages/trilium-core/src/services/import/dispatch.ts
+++ b/packages/trilium-core/src/services/import/dispatch.ts
@@ -80,7 +80,13 @@ async function routeToImporter(taskContext: TaskContext<"importNotes">, file: Fi
// Trilium export by extension alone; the Obsidian import dialog tags the upload to route it here.
return await obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname);
} else if (extension === ".zip" && options.explodeArchives && (file.path || typeof file.buffer !== "string")) {
- return await zipImportService.importZip(taskContext, zipSource, parentNote);
+ // Nothing tags a vault dropped straight onto the note tree, so the zip importer's scan pass hands
+ // one back here instead of importing it: the generic importer would strip it of wikilinks, callouts
+ // and frontmatter properties. That scan already reads every entry name for `!!!meta.json`, so
+ // recognizing a vault adds no pass over the archive.
+ return await zipImportService.importZip(taskContext, zipSource, parentNote, {
+ onObsidianVault: () => obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname)
+ });
} else if (extension === ".opml" && options.explodeArchives) {
return await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
} else if (extension === ".enex" && options.explodeArchives) {
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.ts
index 29578e99dd..54d2165928 100644
--- a/packages/trilium-core/src/services/import/zip.spec.ts
+++ b/packages/trilium-core/src/services/import/zip.spec.ts
@@ -21,7 +21,7 @@ async function testImport(fileName: string) {
return testImportBuffer(buffer);
}
-async function testImportBuffer(buffer: Buffer, taskId = "import-mdx", taskData: Record<string, unknown> = { textImportedAsText: true }, opts?: { restoreAsRoot?: boolean; preserveIds?: boolean }) {
+async function testImportBuffer(buffer: Buffer, taskId = "import-mdx", taskData: Record<string, unknown> = { textImportedAsText: true }, opts?: Parameters<typeof zip.importZip>[3]) {
const taskContext = TaskContext.getInstance(taskId, "importNotes", taskData);
// `init` returns the callback's promise, so a failing import rejects here rather than hanging.
@@ -1498,3 +1498,39 @@ describe("removeTriliumTags", () => {
expect(output).toEqual(expected);
});
});
+
+describe("Obsidian vault detection", () => {
+ async function importWithFallback(files: Record<string, string>, taskId: string) {
+ const onObsidianVault = vi.fn().mockResolvedValue(becca.getNoteOrThrow("root"));
+ const buffer = await createZipBuffer(files);
+ const { importedNote } = await testImportBuffer(buffer, taskId, { textImportedAsText: true }, { onObsidianVault });
+ return { onObsidianVault, importedNote };
+ }
+
+ it("hands a vault to the fallback, zipped as its folder or as its contents", async () => {
+ const wrapped = await importWithFallback({ "MyVault/.obsidian/app.json": "{}", "MyVault/Note.md": "# Note" }, "obsidian-wrapped");
+ expect(wrapped.onObsidianVault).toHaveBeenCalled();
+ // The fallback's note is what the import returns, so the archive was handed over rather than imported.
+ expect(wrapped.importedNote).toBe(becca.getNoteOrThrow("root"));
+
+ const bare = await importWithFallback({ ".obsidian/app.json": "{}", "Note.md": "# Note" }, "obsidian-bare");
+ expect(bare.onObsidianVault).toHaveBeenCalled();
+ });
+
+ it("imports normally when the archive is a Trilium export or holds no vault", async () => {
+ // `!!!meta.json` marks a Trilium export and wins over a stray `.obsidian/` entry inside it.
+ const triliumExport = await importWithFallback({ "!!!meta.json": JSON.stringify({ files: [] }), ".obsidian/app.json": "{}" }, "obsidian-meta");
+ expect(triliumExport.onObsidianVault).not.toHaveBeenCalled();
+
+ const plain = await importWithFallback({ "docs/Note.md": "# Note" }, "obsidian-plain");
+ expect(plain.onObsidianVault).not.toHaveBeenCalled();
+ expect(plain.importedNote?.title).toBe("docs");
+ });
+
+ it("imports a vault generically when no fallback is offered", async () => {
+ const buffer = await createZipBuffer({ "MyVault/.obsidian/app.json": "{}", "MyVault/Note.md": "# Note" });
+ const { importedNote } = await testImportBuffer(buffer, "obsidian-no-fallback");
+
+ expect(importedNote?.title).toBe("MyVault");
+ });
+});
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index 5e22956bb9..2345de6568 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -60,6 +60,12 @@ interface ImportZipOpts {
* archived "root" is remapped to a fresh id like any other note (see {@link getNewNoteId}).
*/
restoreAsRoot?: boolean;
+ /**
+ * Imports the archive instead when the scan pass recognizes an Obsidian vault: a `.obsidian/` config
+ * folder and no `!!!meta.json`. Set by the dispatcher for an untagged `.zip`, so a vault dropped on the
+ * note tree reaches the Obsidian importer without anyone reading the archive an extra time.
+ */
+ onObsidianVault?: () => Promise<BNote>;
}
async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSource, importRootNote: BNote, opts?: ImportZipOpts): Promise<BNote> {
@@ -792,6 +798,8 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSou
// count of entries the processing pass will handle, used as the progress denominator so the
// client can show a progress bar ("X of N") instead of a bare running count
let entriesToProcess = 0;
+ // Whether an entry sits under an Obsidian `.obsidian/` config folder, which marks the vault root.
+ let hasObsidianConfig = false;
await zipProvider.readZipFile(source, async (entry, readContent) => {
const filePath = normalizeFilePath(entry.fileName);
@@ -808,6 +816,12 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSou
metaFile = JSON.parse(new TextDecoder("utf-8").decode(content));
}
+ // Matched by segment so every shape counts: the folder entry itself, a file under it, and either
+ // at the archive root or under the vault's outer folder.
+ if (filePath.split("/").includes(".obsidian")) {
+ hasObsidianConfig = true;
+ }
+
// determine the root of the .zip (i.e. if it has only one top-level folder then the root is that folder, or the root of the archive if there are multiple top-level folders).
const firstSlash = filePath.indexOf("/");
const topLevelPath = (firstSlash !== -1 ? filePath.substring(0, firstSlash) : filePath);
@@ -815,6 +829,12 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSou
}, filenameEncoding);
timing.scan = Date.now() - timingMark;
+ // The scan has just read every entry name, so recognizing a vault costs nothing on top of it. Nothing
+ // has been created yet, so the archive can still be handed over whole.
+ if (opts?.onObsidianVault && hasObsidianConfig && !metaFile) {
+ return await opts.onObsidianVault();
+ }
+
topLevelPath = (topLevelItems.size > 1 ? "" : topLevelItems.values().next().value ?? "");
// The import runs in two labelled phases, each driving its own 0→100% bar: first "extracting" counts