Zotero Integration Nunjucks - Combine groupby with dictsort?

I’m trying to get some of my templates in Obsidian working together with using Zotero and for this I want to write a corresponding (adapted) template.

I’m almost happy with it but still struggling with one thing: Annotations from Zotero are properly read by the Obsidian plugin which extracts them based on their highlighting color and even grouping works as I would like to. However, I would like to have a certain order in which they’re imported.

The following is a macro I’m using for a different function and it shows the Order I would like to have the groups sorted:

{%- macro colorValueToName(color) -%}
    {%- switch color -%}
        {%- case "#ff6666" -%}
        {%- case "#2ea8e5" -%}
            TODO / follow up
        {%- case "#ffd400" -%}
        {%- case "#a28ae5" -%}
        {%- case "#5fb236" -%}
        {%- default -%}
            Interesting but not relevant
    {%- endswitch -%}
{%- endmacro -%}

Now, when “Good” (green) comes before “Questionable” in the annotations, the order will be accordingly. I understand, that I’ll have to define most likely a dict and use dictsort somehow but I’m unsure how to do so.

The relevant code part where the annotations are fetched and group is this:

## Annotations
{% persist "annotations" %}
{% set annots = annotations | filterby("date", "dateafter", lastImportDate) -%}
{% if annots.length > 0 %}
### Imported on {{importDate | format("YYYY-MM-DD h:mm a")}}

{% for color, annots in annots | groupby("color") -%}
#### {{colorValueToName(color)}}

{% for annot in annots -%}
> [!quote{% if annot.color %}|{{annot.color}}{% endif %}] {{calloutHeader(annot.type)}}

[...removed unrelated code]

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

I’m open for thoughts and suggestions. I would think that somewhat has to be done with {% for color, annots in annots | groupby(“color”) -%}


1 Like

looking for the same thing :slight_smile:

found a solution to work around it. this information here helped! nunjucks - Combine GroupBy with sort/dictsort - Stack Overflow

In case it’s still needed, I couldn’t get groupby to work, but filterby does: zotero template that uses this code

A minimal example would be:

   set categoryHeading = {
		"orange":  "Main ideas and conclusions",
        "yellow":  "Ordinary notes",
		"blue":    "Quote / quotable",
		"green":   "Important To Me",
		"red":     "Disagree With Author",
        "purple":  "Interesting side-point",
        "magenta": "Methodology",
		"grey":    "Definitions and concepts"
    set zoteroColors = {
        "#ff6666": "red",
        "#f19837": "orange",
        "#5fb236": "green",
        "#ffd400": "yellow",
        "#2ea8e5": "blue",
        "#a28ae5": "purple",
        "#e56eee": "magenta",
        "#aaaaaa": "grey"
{% for color, colorCategorie in zoteroColors %}
{%- for entry in annotations | filterby ("color", "startswith", color) -%}

{%- if entry and loop.first %}## {{categoryHeading[zoteroColors[color]]}}{% endif -%}


{% endfor -%}
{% endfor -%}

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