From 35a1bb20cb94085e9838e1888800a5faaeb2add1 Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Thu, 18 Aug 2022 17:00:45 +0200 Subject: [PATCH] Make citavi annotations importer more resilient --- chrome/content/zotero/import/citavi.js | 42 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/import/citavi.js b/chrome/content/zotero/import/citavi.js index f65c0d98c3..8e4774a689 100644 --- a/chrome/content/zotero/import/citavi.js +++ b/chrome/content/zotero/import/citavi.js @@ -38,11 +38,23 @@ const ImportCitaviAnnotatons = async (translation) => { const quadsRaw = ZU.xpathText(annotationNodes[i], './Quads'); const locationID = ZU.xpathText(annotationNodes[i], './LocationID'); - const location = ZU.xpath(doc, `//Locations/Location[@${isCitavi5 ? 'ID' : 'id'}='${locationID}']`)[0]; + const location = ZU.xpath(doc, `//Locations/Location[@id='${locationID}']|//Locations/Location[@ID='${locationID}']`)[0]; + + if (!location) { + Zotero.debug(`Missing entry for annotation ${id}, skipping...`); + continue; + } + const referenceID = ZU.xpathText(location, './ReferenceID'); const entityLink = ZU.xpath(doc, `//EntityLinks/EntityLink[TargetID='${id}']`)[0]; + + if (!entityLink) { + Zotero.debug(`Missing entry for annotation ${id}, skipping...`); + continue; + } + const entitySourceID = ZU.xpathText(entityLink, './SourceID'); - const knowledgeItem = ZU.xpath(doc, `//KnowledgeItems/KnowledgeItem[@${isCitavi5 ? 'ID' : 'id'}='${entitySourceID}']`)[0]; + const knowledgeItem = ZU.xpath(doc, `//KnowledgeItems/KnowledgeItem[@id='${entitySourceID}']|//KnowledgeItems/KnowledgeItem[@ID='${entitySourceID}']`)[0]; const keywordsIDsText = ZU.xpathText(doc, `//KnowledgeItemKeywords/OnetoN[starts-with(text(), "${entitySourceID}")]`); const keywords = keywordsIDsText ? keywordsIDsText @@ -137,16 +149,22 @@ const ImportCitaviAnnotatons = async (translation) => { annotations.push(annotation); }); - - annotations = await Zotero.PDFWorker.processCitaviAnnotations( - itemAttachment.getFilePath(), annotations - ); - - annotations.forEach((annotation) => { - promises.push(Zotero.Annotations.saveFromJSON( - itemAttachment, annotation, { skipSelect: true } - )); - }); + + + try { + // eslint-disable-next-line no-await-in-loop + annotations = await Zotero.PDFWorker.processCitaviAnnotations( + itemAttachment.getFilePath(), annotations + ); + annotations.forEach((annotation) => { + promises.push(Zotero.Annotations.saveFromJSON( + itemAttachment, annotation, { skipSelect: true } + )); + }); + } + catch (e) { + Zotero.debug(`Could not process annotations for attachment item ${itemAttachment.key} (file path: ${itemAttachment.getFilePath()})`); + } progress = baseProgress + Math.ceil((i / annotationNodes.length) * stageProgress); translation._runHandler('itemDone', []);