mgmeyers / obsidian-zotero-integration

Insert and import citations, bibliographies, notes, and PDF annotations from Zotero into Obsidian.
GNU General Public License v3.0
1.09k stars 55 forks source link

Extract annotations from multiple pdfs #378

Closed aarontimo closed 4 months ago

aarontimo commented 4 months ago

Is it possible to extract highlights from multiple pdfs stored within the same Zotero record? For example, a long doc/book is broken into Pt I and Pt II and I would like ZI to extract annotations from both pdfs. At the moment, it only appears to extract from the last pdf. Can this be achieved via a nunjuck template?

aarontimo commented 4 months ago

Thanks to Claude 3.5 Sonnet, this template will cycle through and extract the annotations from multiple attachments within the one record in Zotero. I thought I would post it in case it is useful to anyone else.

---
aliases:
Item Type: {{itemType}}
Title: "{{title}}"
Authors: {{authors}}
created: {{importDate | format("YYYY-MM-DD HH:mm")}}
Zotero Date Added: {{dateAdded | format("DD-MM-YYYY HH:mm")}}
URL: {{url}}
DOI: {{DOI}}
Cite key: {{citekey}}
tags: {{allTags}}
Zotero Links: {{uri}}
publisher: "{{publicationTitle}}"
{% if itemType and itemType.toLowerCase() == "case" %}
date decided:
Decided before:
Court:
{% endif %}
Related:: {% for relation in relations -%} {%if relation.title -%} [[{{relation.title}}]], {% endif -%} {%endfor%}
---

{% if attachments and attachments.length > 0 %}
## Attachments
{% for attachment in attachments %}
- [{{attachment.title}}]({{attachment.uri}})
{% endfor %}
{% endif %}

{% if isFirstImport %}
# {{title}}
> [!ABSTRACT] ABSTRACT
> {% if abstractNote %}
> {{abstractNote|replace("\\n"," ")}}
> {% else %} There is no abstract.
> {% endif %}
{% endif %}

{%set zoteroColors = {
"#ff6666": "red",
"#f19837": "orange",
"#5fb236": "green",
"#ffd400": "yellow",
"#2ea8e5": "blue",
"#aaaaaa": "gray",
"#e56eee": "magenta",
"#a28ae5": "purple"
}
-%}

{%macro calloutHeader(type) -%}
{%switch type -%}
{%case "highlight" -%}
{%case "image" -%}
Image
{%default -%}
Note
{%endswitch -%}
{%endmacro %}

{%set footnoteCounter = 1 -%}
{%set footnotes = [] -%}

{% for attachment in attachments %}
## Annotations from {{attachment.title}}

{%set newAnnotations = [] -%}
{%set annotations = attachment.annotations | filterby("date", "dateafter", lastImportDate) %}
{%for annot in annotations -%}
{%if annot.color in zoteroColors -%}
{%set customColor = zoteroColors[annot.color] -%}
{%elif annot.colorCategory|lower in zoteroColors -%}
{%set customColor = annot.colorCategory|lower -%}
{%else -%}
{%set customColor = "other" -%}
{%endif -%}
{%set newAnnotations = (newAnnotations.push({"annotation": annot, "customColor": customColor}), newAnnotations) -%}
{%endfor -%}

{%for entry in newAnnotations -%}
{%set annot = entry.annotation -%}
{%if entry.customColor == "blue" %}
# {{annot.annotatedText}}
{%elif entry.customColor == "green" %}
## {{annot.annotatedText}}
{%elif entry.customColor == "purple" %}
### {{annot.annotatedText}}
{%elif entry.customColor == "magenta" %}
=={{annot.annotatedText}}== {% if annot.hashTags %}{{annot.hashTags}}{% endif -%} ([page. {{annot.pageLabel}}](zotero://open-pdf/library/items/{{attachment.itemKey}}?page={{annot.pageLabel}}&annotation={{annot.id}}))
{%elif entry.customColor == "gray" %}
> {{annot.annotatedText}} ([page. {{annot.pageLabel}}](zotero://open-pdf/library/items/{{attachment.itemKey}}?page={{annot.pageLabel}}&annotation={{annot.id}}))
{%elif entry.customColor == "yellow" %}
{{calloutHeader(annot.type)}}
{%if annot.annotatedText -%}
{{annot.annotatedText|nl2br}} {% if annot.hashTags %}{{annot.hashTags}}{% endif -%} ([page. {{annot.pageLabel}}](zotero://open-pdf/library/items/{{attachment.itemKey}}?page={{annot.pageLabel}}&annotation={{annot.id}}))
{%endif -%}
{%elif entry.customColor == "orange" -%}
{%set footnotes = (footnotes.push("[^" + footnoteCounter + "]: " + annot.annotatedText + " ([page. " + annot.pageLabel + "](zotero://open-pdf/library/items/" + attachment.itemKey + "?page=" + annot.pageLabel + "&annotation=" + annot.id + "))"), footnotes) -%}
[^{{footnoteCounter}}]
{%set footnoteCounter = footnoteCounter + 1 -%}
{%endif -%}
{%if annot.imageRelativePath -%}
![[{{annot.imageRelativePath}}]]
{%endif -%}
{%if annot.ocrText -%}
> [!NOTE] OCR Text
> {{annot.ocrText}}
{%endif -%}
{%if annot.comment -%}
> [!NOTE] Comment
> {{annot.comment|nl2br}}
{%endif -%}
{%endfor -%}

{% endfor %}

{%if footnotes.length > 0 %}
---
{%for footnote in footnotes %}
{{footnote}}
{%endfor %}
{%endif %}

*Last imported: {{importDate | format("YYYY-MM-DD HH:mm")}}*