You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Obsidian模板中自定义排序Zotero批注分组的实现问询

自定义Zotero批注分组顺序解决方案

要实现自定义顺序的批注分组,核心是先定义好期望的颜色优先级列表,再按该列表顺序遍历分组,而非依赖groupby的默认排序。具体实现如下:

1. 定义自定义颜色顺序列表

在模板中声明一个数组,严格对应你需要的分组顺序:

{% set customColorOrder = ["#ff6666", "#2ea8e5", "#ffd400", "#a28ae5", "#5fb236"] %}

这个顺序匹配你宏里的Questionable→TODO / follow up→Important→Interesting→Good优先级。

2. 重构分组遍历逻辑

替换原有的groupby遍历代码,改为先按自定义顺序处理指定颜色,再统一处理剩余默认颜色的批注:

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

{% set colorGroups = annots | groupby("color") %}
{% set customColorOrder = ["#ff6666", "#2ea8e5", "#ffd400", "#a28ae5", "#5fb236"] %}

{# 按自定义顺序遍历指定颜色的分组 #}
{% for color in customColorOrder %}
    {% if colorGroups[color] is defined and colorGroups[color].length > 0 %}
    #### {{colorValueToName(color)}}
        {% for annot in colorGroups[color] -%}
        > [!quote{% if annot.color %}|{{annot.color}}{% endif %}] {{calloutHeader(annot.type)}}
        {...保留你原有的批注内容渲染代码...}
        {% endfor -%}
    {% endif %}
{% endfor %}

{# 处理默认类别:收集所有不在自定义列表中的批注 #}
{% set defaultAnnots = [] %}
{% for color, group in colorGroups %}
    {% if color not in customColorOrder %}
        {% set defaultAnnots = defaultAnnots | merge(group) %}
    {% endif %}
{% endfor %}
{% if defaultAnnots.length > 0 %}
#### {{colorValueToName("default")}}
    {% for annot in defaultAnnots -%}
    > [!quote{% if annot.color %}|{{annot.color}}{% endif %}] {{calloutHeader(annot.type)}}
    {...保留你原有的批注内容渲染代码...}
    {% endfor -%}
{% endif %}

{% endif %}
{% endpersist %}

代码说明

  • colorGroups:提前将批注按颜色分组,得到「颜色值→对应批注数组」的映射字典
  • 自定义顺序遍历:只处理存在批注的指定颜色分组,避免空分组占位
  • 默认类别处理:统一归集所有未匹配自定义颜色的批注,对应宏中的Interesting but not relevant分类

内容的提问来源于stack exchange,提问作者CuttingWide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:05:26