diff --git a/src/modules/workspace/link.ts b/src/modules/workspace/link.ts index cc784a6..2cf73cf 100644 --- a/src/modules/workspace/link.ts +++ b/src/modules/workspace/link.ts @@ -114,7 +114,7 @@ async function renderSection( if (!targetItem) { continue; } - count ++; + count++; const linkParams = { workspaceUID: (body.closest("bn-workspace") as Workspace)?.dataset.uid, diff --git a/src/utils/note.ts b/src/utils/note.ts index b88799b..c82394a 100644 --- a/src/utils/note.ts +++ b/src/utils/note.ts @@ -322,6 +322,10 @@ async function copyEmbeddedImagesFromNote( ) { await Zotero.DB.executeTransaction(async () => { for (const fromNote of sourceNotes) { + // Do not copy to itself, otherwise the note may break the DB + if (!fromNote.id || !targetNote.id || fromNote.id === targetNote.id) { + continue; + } await Zotero.Notes.copyEmbeddedImages(fromNote, targetNote); } }); @@ -361,17 +365,28 @@ async function copyEmbeddedImagesInHTML( doc.querySelectorAll(`img[data-attachment-key="${attachment.key}"]`), ) as HTMLImageElement[]; if (nodes.length) { - let copiedAttachment: Zotero.Item; + let copiedAttachment: Zotero.Item | undefined; await Zotero.DB.executeTransaction(async () => { Zotero.DB.requireTransaction(); + // Do not copy to itself, otherwise the note may break the DB + if ( + !attachment.parentID || + !targetNote.id || + attachment.parentID === targetNote.id + ) { + return; + } copiedAttachment = await Zotero.Attachments.copyEmbeddedImage({ attachment, note: targetNote, }); }); + if (!copiedAttachment) { + continue; + } nodes.forEach( (node) => - node?.setAttribute("data-attachment-key", copiedAttachment.key), + node?.setAttribute("data-attachment-key", copiedAttachment!.key), ); } }