英文:
Combine GroupBy with sort/dictsort
问题
我试图使我的一些Obsidian模板与Zotero一起使用,并为此我想写一个相应的(改编后的)模板。
我差不多满意它了,但仍然在一个问题上苦苦挣扎:
Obsidian插件可以正确读取Zotero的注释,根据它们的高亮颜色提取它们,甚至分组也像我想要的那样工作。然而,我希望它们被导入时有一个特定的顺序。
以下是我用于不同功能的宏,显示了我希望将组按顺序排序的顺序:
{%- macro colorValueToName(color) -%}
{%- switch color -%}
{%- case "#ff6666" -%}
有疑问的
{%- case "#2ea8e5" -%}
待办事项 / 后续跟进
{%- case "#ffd400" -%}
重要的
{%- case "#a28ae5" -%}
有趣的
{%- case "#5fb236" -%}
好的
{%- default -%}
有趣但不相关
{%- endswitch -%}
{%- endmacro -%}
现在,当"好的"(绿色)在注释中出现在"有疑问的"(红色)之前时,顺序将相应地排序。我理解,我可能需要定义一个字典并使用dictsort,但我不确定如何做。
获取注释并分组的相关代码部分如下:
## 注释
{% persist "annotations" %}
{% set annots = annotations | filterby("date", "dateafter", lastImportDate) -%}
{% if annots.length > 0 %}
### 导入于 {{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)}}
[...删除了不相关的代码]
{% endfor -%}
{% endfor -%}
{% endif %}
{% endpersist %}
我愿意听取意见和建议。我认为在**{% for color, annots in annots | groupby("color") -%}**这里可能需要做一些事情。
英文:
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" -%}
Questionable
{%- case "#2ea8e5" -%}
TODO / follow up
{%- case "#ffd400" -%}
Important
{%- case "#a28ae5" -%}
Interesting
{%- case "#5fb236" -%}
Good
{%- 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
得分: 2
我也遇到了相同的问题,并发现通过结合"groupby"和"sort"来解决问题很困难。所以我使用了一个变通的解决方案,预定义了一个有序颜色列表,然后使用"filterby"来选择具有特定颜色的条目。请看下面的代码片段。
{%- set colorToColorCategorie = {
"#5fb236": "绿色",
"#ffd400": "黄色",
"#ff6666": "红色",
"#2ea8e5": "蓝色",
"#a28ae5": "紫色",
"#e56eee": "洋红色",
"#f19837": "橙色"
}
-%}
{% for color, colorCategorie in colorToColorCategorie.items() %}
{%- for annotation in annotations | filterby ("color", "startswith", color) -%}
做一些操作
{% endfor -%}
{% endfor %}
希望对你有所帮助。你可以在我的GitHub上查看我使用的整个模板:https://github.com/nano8259/obsidian-literature-note。由于缺少对笔记的支持,它还不完整。我会在有时间时完成它。
祝你有个美好的一天:)
英文:
I also encountered the same problem and found the problem is hard to solve by combining "groupby" and "sort". So I use a workaround solution that predefines an ordered list of colors and then uses "filterby" to select the entries with the specific color. Please take a look at the code snippet below.
{%- 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 %}
I hope this will be helpful to you. And you can see the entire template I'm using on my github: https://github.com/nano8259/obsidian-literature-note. It is incomplete for lacking support of notes. I will completeit when I have time.
Have a nice day : )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论