Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): scan a mind map for the pictures it carries

Elian Doranbab74ca08640
diff --git a/apps/server/src/services/notes.spec.ts b/apps/server/src/services/notes.spec.ts
index ca46d6ee6d..0708f17aec 100644
--- a/apps/server/src/services/notes.spec.ts
+++ b/apps/server/src/services/notes.spec.ts
@@ -369,6 +369,52 @@ describe("checkImageAttachments", () => {
});
});
+ describe("Mind map content", () => {
+ /** Wraps picture URLs into the JSON shape a mind map persists (one node per URL). */
+ function mindMapContent(...urls: string[]) {
+ return JSON.stringify({
+ nodeData: {
+ id: "root",
+ topic: "Root",
+ children: urls.map((url, index) => ({
+ id: `node-${index}`,
+ topic: `Node ${index}`,
+ image: { url, width: 240, height: 180 }
+ }))
+ }
+ });
+ }
+
+ it("keeps a picture referenced by a node alive, and schedules one taken off a node for erasure", () => {
+ const note = buildNote({ title: "Map", type: "mindMap", mime: "application/json", attachments: [{ title: "photo.png", role: "image", mime: "image/png" }] });
+ mockAttachmentSaves(note);
+ const [att] = note.getAttachments();
+
+ checkImageAttachments(note, mindMapContent(`api/attachments/${att.attachmentId}/image/photo.png`));
+ expect(att.save).not.toHaveBeenCalled();
+
+ checkImageAttachments(note, mindMapContent("api/attachments/someOtherId/image/photo.png"));
+ expect(att.save).toHaveBeenCalled();
+ expect(att.utcDateScheduledForErasureSince).toBeTruthy();
+
+ // Put back on a node (e.g. undo), it is kept again.
+ checkImageAttachments(note, mindMapContent(`api/attachments/${att.attachmentId}/image/photo.png`));
+ expect(att.utcDateScheduledForErasureSince).toBeNull();
+ });
+
+ it("never schedules the SVG export preview for erasure even though it is unreferenced", () => {
+ const note = buildNote({ title: "Map", type: "mindMap", mime: "application/json", attachments: [{ title: "mindmap-export.svg", role: "image", mime: "image/svg+xml" }] });
+ mockAttachmentSaves(note);
+ const [preview] = note.getAttachments();
+
+ // A map with no pictures at all — the preview is the only "image" attachment.
+ checkImageAttachments(note, mindMapContent());
+
+ expect(preview.save).not.toHaveBeenCalled();
+ expect(preview.utcDateScheduledForErasureSince).toBeFalsy();
+ });
+ });
+
describe("foreign attachment copying", () => {
it("replaces foreign attachment IDs in HTML content", () => {
const note = buildNote({ title: "Test" });
@@ -435,11 +481,11 @@ describe("saveLinks", () => {
return attr;
}
- // `checkImageAttachments` exempts the canvas and spreadsheet rendered images by title, but not
- // the mermaid and mindMap ones. That looks like an oversight until you notice `saveLinks` bails
- // out before it for those two types, so their SVGs are never reachable by orphan erasure at all.
- // Pinned here: if either type ever gains a `saveLinks` branch, it needs an exemption first, and
- // this test is what will say so.
+ // `checkImageAttachments` exempts the canvas, spreadsheet and mind map rendered images by title,
+ // but not the mermaid one. That looks like an oversight until you notice `saveLinks` bails out
+ // before it for mermaid, so its SVG is never reachable by orphan erasure at all. Pinned here: if
+ // mermaid ever gains a `saveLinks` branch, it needs an exemption first, and this test is what
+ // will say so — as it did for mind maps, which now carry pictures and so have both.
it.each([
[ "mermaid", "text/mermaid", "mermaid-export.svg", "flowchart TD\n A --> B" ],
[ "mindMap", "application/json", "mindmap-export.svg", `{"nodeData":{}}` ]
@@ -455,6 +501,16 @@ describe("saveLinks", () => {
expect(rendered.utcDateScheduledForErasureSince).toBeFalsy();
});
+ it("schedules a picture taken off a mind map's nodes for erasure", () => {
+ const note = buildNote({ title: "Map", type: "mindMap", mime: "application/json", attachments: [{ title: "photo.png", role: "image", mime: "image/png" }] });
+ mockAttachmentSaves(note);
+ const [picture] = note.getAttachments();
+
+ saveLinks(note, JSON.stringify({ nodeData: { id: "root", topic: "Root" } }));
+
+ expect(picture.utcDateScheduledForErasureSince).toBeTruthy();
+ });
+
it("does not delete existing imageLink relations on markdown notes that reference images", () => {
const note = buildNote({ title: "Test", type: "code", mime: "text/x-markdown" });
const targetNote = buildNote({ title: "Image Note", type: "image" });
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index bc91299f2d..02cf7b0141 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -452,9 +452,10 @@ export function checkImageAttachments(note: BNote, content: string) {
if (!isCanvas) {
let match;
- // Spreadsheet content is JSON storing inline images as bare `api/attachments/{id}/image/...`
- // URLs (no `src="..."` wrapper), so it scans with the same loose pattern as Markdown.
- const patterns = (note.isMarkdown() || note.type === "spreadsheet")
+ // Spreadsheet and mind map content is JSON storing images as bare
+ // `api/attachments/{id}/image/...` URLs (no `src="..."` wrapper), so they scan with the same
+ // loose pattern as Markdown.
+ const patterns = (note.isMarkdown() || note.type === "spreadsheet" || note.type === "mindMap")
? [
// ![...](api/attachments/{id}/image/...) or similar markdown image syntax
/api\/attachments\/([a-zA-Z0-9_]+)\/image/g,
@@ -495,6 +496,11 @@ export function checkImageAttachments(note: BNote, content: string) {
continue;
}
+ // Likewise for the mind map SVG export preview, which the map JSON never references.
+ if (note.type === "mindMap" && attachment.title === NOTE_TYPE_IMAGE_ATTACHMENTS.mindMap) {
+ continue;
+ }
+
// Likewise for the canvas SVG export preview, which the scene JSON never references.
if (isCanvas && attachment.title === NOTE_TYPE_IMAGE_ATTACHMENTS.canvas) {
continue;
@@ -998,7 +1004,7 @@ function stripStaleSrcset(content: string): string {
export function saveLinks(note: BNote, content: string | Uint8Array) {
- if ((note.type !== "text" && note.type !== "relationMap" && note.type !== "llmChat" && note.type !== "spreadsheet" && note.type !== "canvas" && !note.isMarkdown()) || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {
+ if ((note.type !== "text" && note.type !== "relationMap" && note.type !== "llmChat" && note.type !== "spreadsheet" && note.type !== "canvas" && note.type !== "mindMap" && !note.isMarkdown()) || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {
return {
forceFrontendReload: false,
content
@@ -1032,6 +1038,11 @@ export function saveLinks(note: BNote, content: string | Uint8Array) {
// the scene JSON; scan for orphans (inserted-then-removed images) so they get scheduled for
// erasure. There are no Trilium internal links to extract from canvas content.
({ forceFrontendReload, content } = checkImageAttachments(note, content));
+ } else if (note.type === "mindMap" && typeof content === "string") {
+ // Mind map node images are stored as attachments referenced by URL from the map JSON; scan
+ // for orphans (inserted-then-removed images) so they get scheduled for erasure. No Trilium
+ // internal links are extracted from map content: a node's link is stored as a plain URL.
+ ({ forceFrontendReload, content } = checkImageAttachments(note, content));
} else if (note.type === "relationMap" && typeof content === "string") {
findRelationMapLinks(content, foundLinks);
} else if (note.type === "llmChat" && typeof content === "string") {