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): keep OneNote debug source attachments from being erased
Elian Doran59177133e1a9
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex af5c9d3a5b..f86cd3535f 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -620,6 +620,11 @@ describe("getRenderingType detection", () => {expect((await getRenderedContent(att)).type).toBe("image");});+ it("renders an importSource attachment like a file", async () => {+ const att = buildAttachment({ role: "importSource", mime: "text/html" });+ expect((await getRenderedContent(att)).type).toBe("file");+ });+it("returns the raw role for an attachment with an unhandled role (no rendering branch)", async () => {const att = buildAttachment({ role: "unknownRole" });const { type, $renderedContent } = await getRenderedContent(att);diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 5c8b8c840a..681f18e906 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -449,6 +449,11 @@ function getRenderingType(entity: FNote | FAttachment) {type = entity.type;} else if ("role" in entity) {type = entity.role;+ // "importSource" attachments (e.g. the OneNote debug source HTML/InkML) are plain files kept+ // for reference; render them exactly like a "file" role.+ if (type === "importSource") {+ type = "file";+ }}const mime = "mime" in entity && entity.mime;diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex f9a0a89c01..6a7326353b 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -146,7 +146,10 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewconst imageViewerWrapper = useRef<HTMLDivElement>(null);const [ title, setTitle ] = useState(attachment.title);const [ textContent, setTextContent ] = useState<string | null>(null);- const supportsOcr = attachment.role === "image" || attachment.role === "file";+ // "importSource" attachments (e.g. OneNote debug source) behave like ordinary files for+ // preview, OCR and link-copying purposes.+ const isFileLike = attachment.role === "file" || attachment.role === "importSource";+ const supportsOcr = attachment.role === "image" || isFileLike;// Image attachments opened in full detail get the interactive zoom/pan viewer; everything// else is rendered imperatively via the content renderer.@@ -161,7 +164,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view});}- if (attachment.role === "file") {+ if (isFileLike) {attachment.getBlob().then(blob => setTextContent(blob?.content ?? null));}@@ -186,7 +189,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewif (attachment.role === "image") {const $img = refToJQuerySelector(isZoomableImage ? imageViewerWrapper : contentWrapper).find("img");if ($img.length) image.copyImageReferenceToClipboard($img.parent());- } else if (attachment.role === "file") {+ } else if (isFileLike) {const $link = await link.createLink(attachment.ownerId, {referenceLink: true,viewScope: {diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.tsindex b76fd2a2c5..b8f2a3a41c 100644--- a/apps/server/src/services/import/onenote/importer.ts+++ b/apps/server/src/services/import/onenote/importer.ts@@ -124,14 +124,14 @@ function createNotes(parentNoteId: string, sections: FetchedSection[], debug: bo// converted note so the two can be compared when diagnosing conversion issues.if (debug) {pageNote.saveAttachment({- role: "file",+ role: "importSource",mime: "text/html",title: "OneNote source.html",content: page.rawHtml});if (page.rawInkml) {pageNote.saveAttachment({- role: "file",+ role: "importSource",mime: "application/inkml+xml",title: "OneNote ink.inkml",content: page.rawInkmldiff --git a/apps/server/src/services/notes.spec.ts b/apps/server/src/services/notes.spec.tsindex e90fc662ce..17e15d5d70 100644--- a/apps/server/src/services/notes.spec.ts+++ b/apps/server/src/services/notes.spec.ts@@ -100,6 +100,17 @@ describe("checkImageAttachments", () => {expect(att.utcDateScheduledForErasureSince).toBeTruthy();});+ it("leaves non-embeddable roles untouched even when unreferenced", () => {+ const note = buildNote({ title: "Test", attachments: [{ title: "OneNote source.html", role: "importSource", mime: "text/html" }] });+ mockAttachmentSaves(note);+ const [att] = note.getAttachments();++ checkImageAttachments(note, "<p>No images here</p>");++ expect(att.save).not.toHaveBeenCalled();+ expect(att.utcDateScheduledForErasureSince).toBeFalsy();+ });+it("cancels erasure when attachment is re-referenced", () => {const note = buildNote({ title: "Test", attachments: [{ title: "test.png", role: "image", mime: "image/png" }] });mockAttachmentSaves(note);diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex c304f8839d..4f4fbb008f 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -443,6 +443,13 @@ export function checkImageAttachments(note: BNote, content: string) {const attachments = note.getAttachments();for (const attachment of attachments) {+ // Only attachments that are meant to be embedded in the note content (images, files) are+ // auto-scheduled for erasure when no longer referenced. Other roles (e.g. "viewConfig",+ // "importSource") are managed explicitly by their owners and must not be cleaned up here.+ if (attachment.role !== "image" && attachment.role !== "file") {+ continue;+ }+const attachmentInContent = attachment.attachmentId && foundAttachmentIds.has(attachment.attachmentId);if (attachment.utcDateScheduledForErasureSince && attachmentInContent) {