Zotero Integration template for Literature Notes

Hello. So basically, I am using Zotero Integration to import annotations from articles I have in my library in Zotero so I can create annotation notes, since Obsidian is much more powerful in that regard, as I can create links between notes and visual MOCs.

I found a neat template for this purpose (made by Ric Raftis), which structures the annotaions in sections according to the respective highlight color. It also provides the meatdata of the article, as well as a link to the PDF (which I find very useful).

The template is the following.

---
type: literaturenote
tags: 
keywords: {{allTags}}
aliases: 
Import Date: {{importDate | format("YYYY-MM-DD HH:mm")}}
---
# {{title}}

> [!info]- Metadata – {% for attachment in attachments | filterby("path", "endswith", ".pdf") %}[PDF{% if not loop.first %} {{loop.index}}{% endif %}]({{attachment.desktopURI|replace("/select/", "/open-pdf/")}}){% if not loop.last %}, {% endif %}{% endfor %}
>- **Title**:: {{title}}  
>- **Authors**:: {%- for creator in creators %} {%- if creator.name == null %} {{creator.firstName}} {{creator.lastName}}, {%- endif -%} {%- if creator.name %}**{{creator.creatorType | capitalize}}**:: {{creator.name}}{%- endif -%}{%- endfor %}.
>- **Year**:: {{date | format("YYYY")}}   
>- **Citekey**:: {{citekey}} {%- if itemType %}  
>- **itemType**:: {{itemType}}{%- endif %}{%- if itemType == "journalArticle" %}  
>- **Journal**:: *{{publicationTitle}}* {%- endif %}{%- if volume %}  
>- **Volume**:: {{volume}} {%- endif %}{%- if issue %}  
>- **Issue**:: {{issue}} {%- endif %}{%- if itemType == "bookSection" %}  
>- **Book**:: {{publicationTitle}} {%- endif %}{%- if publisher %}  
>- **Publisher**:: {{publisher}} {%- endif %}{%- if place %}  
>- **Location**:: {{place}} {%- endif %}{%- if pages %}   
>- **Pages**:: {{pages}} {%- endif %}{%- if DOI %}  
>- **DOI**:: {{DOI}} {%- endif %}{%- if ISBN %}  
>- **ISBN**:: {{ISBN}} {%- endif %}    
>***
>- **Keywords**:: {% if allTags %} {{allTags}} {% endif %}
>- **Bibliography:**: {%- if bibliography %} {{bibliography|replace("\n"," ")}} {%- endif %}
>- **Related**:: {% for relation in relations -%} {%- if relation.title -%} [[{{relation.title}}]], {% endif -%} {%- endfor%}

> [!ABSTRACT]- ABSTRACT
> {% if abstractNote %} 
> {{abstractNote|replace("\n"," ")}}
> {% endif %}

## Summary
## Commentary

## Anotações
{%-
    set zoteroColors = {
        "#ff6666": "red",
        "#ffd400": "yellow",
        "#f19837": "orange",
        "#5fb236": "green",
        "#2ea8e5": "blue",
        "#aaaaaa": "grey"
    }
-%}

{%-
   set colorHeading = {
		"red": "🟥 Muito Importante ou Crítico ",
		"yellow": "🟨 Relevante",
		"orange": "🛑 Partes questionáveis ou conflituosas",
		"green": "âś… Argumentos ou exemplos demonstrativos",
	    "blue": "❕ Lembretes - Definições, conceitos, nomes, datas",
	    "grey": "đź“š ReferĂŞncias interessantes"
   }
-%}

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

{% persist "annotations" %}
{%- set newAnnot = [] -%}
{%- set newAnnotations = [] -%}
{%- set annotations = annotations | filterby("date", "dateafter", lastImportDate) %}

{% if annotations.length > 0 %}

{%- for annot in annotations -%}

    {%- if annot.color in zoteroColors -%}
        {%- set customColor = zoteroColors[annot.color] -%}
    {%- elif annot.colorCategory|lower in colorHeading -%}
    	{%- set customColor = annot.colorCategory|lower -%}
    {%- else -%}
	    {%- set customColor = "other" -%}
    {%- endif -%}

    {%- set newAnnotations = (newAnnotations.push({"annotation": annot, "customColor": customColor}), newAnnotations) -%}

{%- endfor -%}

{#- INSERT ANNOTATIONS -#}
{#- Loops through each of the available colors and only inserts matching annotations -#}
{#- This is a workaround for inserting categories in a predefined order (instead of using groupby & the order in which they appear in the PDF) -#}

{%- for color, heading in colorHeading -%}
{%- for entry in newAnnotations | filterby ("customColor", "startswith", color) -%}
{%- set annot = entry.annotation -%}

{%- if entry and loop.first %}

#### {{colorHeading[color]}}
{%- endif %}

> [!quote{{"|" + color if color != "other"}}]+ {{calloutHeader(annot.type)}} ([page. {{annot.pageLabel}}](zotero://open-pdf/library/items/{{annot.attachment.itemKey}}?page={{annot.pageLabel}}&annotation={{annot.id}}))

{%- if annot.annotatedText %}
> {{annot.annotatedText|nl2br}} {% if annot.hashTags %}{{annot.hashTags}}{% endif -%}
{%- endif %}

{%- if annot.imageRelativePath %}
> ![[{{annot.imageRelativePath}}]]
{%- endif %}

{%- if annot.ocrText %}
> {{annot.ocrText}}
{%- endif %}

{%- if annot.comment %}
> - **{{annot.comment|nl2br}}**
{%- endif -%}

{%- endfor -%}
{%- endfor -%}
{% endif %}
{% endpersist %}

What I’m trying to do

This template should see some improvments.
There are some flaws when facing updates (i.e, when I import a second time, in case I change something in an article’s annotations/highlights) :

  • Some annotations disappear (IMPORTANT)
  • “Make annotations (and block references; internal links) persistent so I can write notes in between each annotation that are retained every time I update the file” (VERY IMPORTANT). The block references between the annotations and commentary are very useful, so I do not want to lose them.
    • “The structured commentary (summary and commentary sections) must be retained with each iteration”
      • {{FirstImport}} doesn’t seem to work (it is in [[zot-temp]])
      • {{persist}}
  • 2 forms of update:
  • ADDITION: “When new annotations are added they should be appended to the existing list, not put into a separate lists of new annotations” (OPTIONAL)
  • REMOVAL: When highlights are removed from source in Zotero, the template should recognize it and thus updating the literature note with associated annotation removal (INTERESTED)
    • (=true) function??
  • “When the file is updated any new annotations that imported to the file are put in ascending page order. Currently, if I wrap the annotations with {% persist “annotations” %} {% endpersist %} the new annotations are always put at the end of the list and are therefore not in page order” (NOT INTERESTED)

It should be noted that my knowledge of Nunjucks is very limited (the programming language used in the template), this being the reason why I haven’t figured this out yet.

Things I have tried

Firstly, I tried to apply {{persist}} to Summary and Commentary, besides the annotations. This worked for the annotations, as It kept all the links and extra notes. However, this was not the case for Summary and Commentary.

I tried using {{FirstImport}}. Of course, it didn’t work.

For the rest of the items, I don’t know what to do to solve them. This is why I am posting this here, in hopes someone already figured this out or is sufficiently knowledgable in Nunjucks.

NOTE: @cheesycrips may have solved this, as the post “Zotero Template with Ordered Persistent notes” addresses these issues. However, I do not have a means of contacting this person, as this forum doesn’t allow me to DM yet.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.