结合 GroupBy 和 sort/dictsort

问题描述 投票:0回答:1

我正在尝试让我在 Obsidian 中的一些模板与 Zotero 一起工作,为此我想编写一个相应的(改编的)模板。

我几乎对此感到满意,但仍在为一件事而苦苦挣扎: 来自 Zotero 的注释被 Obsidian 插件正确读取,该插件根据它们的突出显示颜色提取它们,甚至按照我的意愿分组工作。但是,我希望有一定的导入顺序。

以下是我用于不同功能的宏,它显示了我希望对组进行排序的顺序:

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

现在,当注释中“Good”(绿色)出现在“Questionable”之前时,顺序将是相应的。我知道,我很可能必须定义一个 dict 并以某种方式使用 dictsort,但我不确定该怎么做。

获取注释和分组的相关代码部分是这样的:

## 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 %}

我愿意接受想法和建议。我认为必须用 {% 来完成颜色,注释中的注释 | groupby("颜色") -%}

nunjucks obsidian
1个回答
0
投票

我也遇到了同样的问题,发现结合groupby和sort很难解决问题。所以我使用了一个变通解决方案,它预定义了一个有序的颜色列表,然后使用“filterby”来选择具有特定颜色的条目。请看下面的代码片段。

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

{% for color, colorCategorie in colorToColorCategorie %}
{%- for annotation in annotations | filterby ("color", "startswith", color) -%}
do something
{% endfor -%}
{% endfor %}

希望对您有所帮助。您可以在我的 github 上看到我正在使用的整个模板:https://github.com/nano8259/obsidian-literature-note。由于缺乏注释的支持,它是不完整的。有时间我会完成的

祝你有美好的一天:)

© www.soinside.com 2019 - 2024. All rights reserved.