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): point an imported map's pictures at its own attachments
Elian Doran38ea98dde8a8
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.tsindex cac2d00447..a02b1c99df 100644--- a/packages/trilium-core/src/services/import/zip.spec.ts+++ b/packages/trilium-core/src/services/import/zip.spec.ts@@ -179,6 +179,52 @@ describe("processNoteContent", () => {expect(content).not.toContain("Markdown Note_image.jpg");});+ it("points the pictures of an imported mind map at the attachments as they were recreated", async () => {+ // The map carries the address each picture was served from where it was exported, and every+ // attachment is given a new id on the way in — so without rewriting, the pictures of an+ // imported map would point at attachments of the instance it left.+ const metaFile = {+ formatVersion: 2,+ appVersion: "0.0.0",+ files: [{+ noteId: "mindMapNote1",+ title: "Mind Map",+ type: "mindMap",+ mime: "application/json",+ dataFileName: "Mind Map.json",+ attachments: [{+ attachmentId: "mapPicture1",+ title: "photo.png",+ role: "image",+ mime: "image/png",+ position: 10,+ dataFileName: "Mind Map_photo.png"+ }]+ }]+ };+ const mapData = {+ nodeData: {+ id: "root",+ topic: "Root",+ image: { url: "api/attachments/mapPicture1/image/photo.png", width: 240, height: 180 }+ }+ };++ const zipBuffer = await createZipBuffer({+ "!!!meta.json": JSON.stringify(metaFile),+ "Mind Map.json": JSON.stringify(mapData),+ "Mind Map_photo.png": Buffer.from("fake image data")+ });++ const { importedNote } = await testImportBuffer(zipBuffer);+ const [ picture ] = importedNote.getAttachmentsByRole("image");+ const content = importedNote.getContent() as string;++ expect(picture.attachmentId).not.toBe("mapPicture1");+ expect(content).toContain(`api/attachments/${picture.attachmentId}/image/photo.png`);+ expect(content).not.toContain("mapPicture1");+ });+it("restores an embedded mermaid diagram as a note reference, not as a raw attachment", async () => {// The export points the <img> at the mermaid note's generated `mermaid-export.svg`.// On the way back in that has to resolve to `api/images/<noteId>` — which re-renders thediff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.tsindex 9c1f6de6c8..5df9c3c05d 100644--- a/packages/trilium-core/src/services/import/zip.ts+++ b/packages/trilium-core/src/services/import/zip.ts@@ -477,6 +477,10 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSoucontent = processMarkdownCodeNoteContent(content, filePath);}+ if (type === "mindMap" && typeof content === "string") {+ content = processMindMapContent(content);+ }+if (type === "relationMap" && noteMeta && typeof content === "string") {const relationMapLinks = (noteMeta.attributes || []).filter((attr) => attr.type === "relation" && attr.name === "relationMapLink");@@ -490,6 +494,18 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSoureturn content;}+ /**+ * Points the pictures of a mind map's nodes at the attachments as they were recreated here.+ *+ * The map JSON carries each picture as the `api/attachments/...` URL it was served from in the+ * instance the map came from, and every attachment is given a new id on the way in — so without+ * this the pictures of an imported map point at attachments of the instance it left.+ */+ function processMindMapContent(content: string) {+ return content.replace(/api\/attachments\/([a-zA-Z0-9_]+)\/image/g,+ (_match, attachmentId: string) => `api/attachments/${getNewAttachmentId(attachmentId)}/image`);+ }+/*** Rewrites relative file paths in markdown code notes back to Trilium internal* URLs (counterpart to `rewriteMarkdownContentLinks` in the export).