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
fix(import): recognize only the vault shapes the importer handles
Elian Doran27629789ff00
diff --git a/packages/trilium-core/src/services/import/dispatch.ts b/packages/trilium-core/src/services/import/dispatch.tsindex 12c16b0b5e..dc2fafbd69 100644--- a/packages/trilium-core/src/services/import/dispatch.ts+++ b/packages/trilium-core/src/services/import/dispatch.ts@@ -81,11 +81,8 @@ async function routeToImporter(taskContext: TaskContext<"importNotes">, file: Fireturn await obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname);} else if (extension === ".zip" && options.explodeArchives && (file.path || typeof file.buffer !== "string")) {if (format === "auto") {- // What a drag-and-drop onto the note tree sends: no importer was picked, so the zip importer's- // scan pass hands an Obsidian vault back here instead of importing it as a plain archive, which- // 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.- // A `.zip` chosen in the import dialog is a decision already made, and is left alone.+ // "auto" is what the note tree's drop handler sends, naming no importer. A `.zip` chosen in the+ // import dialog names one, so only the dropped archive is checked for an Obsidian vault.return await zipImportService.importZip(taskContext, zipSource, parentNote, {onObsidianVault: () => obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname)});diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.tsindex 54d2165928..4ed3319839 100644--- a/packages/trilium-core/src/services/import/zip.spec.ts+++ b/packages/trilium-core/src/services/import/zip.spec.ts@@ -1527,6 +1527,18 @@ describe("Obsidian vault detection", () => {expect(plain.importedNote?.title).toBe("docs");});+ it("leaves a vault nested among unrelated content to this importer", async () => {+ // importObsidian strips only its own vault root, so everything beside the vault would be imported+ // with Obsidian semantics under a root named after it.+ const nested = await importWithFallback({+ "Documents/MyVault/.obsidian/app.json": "{}",+ "Documents/MyVault/Note.md": "# Note",+ "Documents/Unrelated.md": "# Unrelated"+ }, "obsidian-nested");++ expect(nested.onObsidianVault).not.toHaveBeenCalled();+ });+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");diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.tsindex 2345de6568..472091c253 100644--- a/packages/trilium-core/src/services/import/zip.ts+++ b/packages/trilium-core/src/services/import/zip.ts@@ -62,8 +62,9 @@ interface ImportZipOpts {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.+ * folder at the archive root or under a single wrapper folder, and no `!!!meta.json`. Set by the+ * dispatcher for an untagged `.zip`, so a dropped vault reaches the Obsidian importer without anyone+ * reading the archive an extra time.*/onObsidianVault?: () => Promise<BNote>;}@@ -798,8 +799,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 countlet entriesToProcess = 0;- // Whether an entry sits under an Obsidian `.obsidian/` config folder, which marks the vault root.- let hasObsidianConfig = false;+ // Depth of the shallowest `.obsidian` segment, which sits at an Obsidian vault's root; -1 when absent.+ let obsidianDepth = -1;await zipProvider.readZipFile(source, async (entry, readContent) => {const filePath = normalizeFilePath(entry.fileName);@@ -816,10 +817,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSoumetaFile = 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;+ const depth = filePath.split("/").indexOf(".obsidian");+ if (depth !== -1 && (obsidianDepth === -1 || depth < obsidianDepth)) {+ obsidianDepth = depth;}// 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).@@ -829,9 +829,11 @@ 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) {+ // Only the two shapes importObsidian supports: `.obsidian/` at the archive root, or under a single+ // wrapper folder. A vault nested among unrelated content stays with this importer, which leaves that+ // content alone.+ const isObsidianVault = obsidianDepth === 0 || (obsidianDepth === 1 && topLevelItems.size === 1);+ if (opts?.onObsidianVault && isObsidianVault && !metaFile) {return await opts.onObsidianVault();}