gpt4 book ai didi

jekyll - 在 Jekyll 中计算集合标签

转载 作者:行者123 更新时间:2023-12-05 01:02:08 28 4
gpt4 key购买 nike

我正在尝试计算和列出 Jekyll 中名为 _note 的集合中的标签。我认为,我非常接近解决它,但我对标签的实际计数有点困惑(列出唯一标签工作正常)并且可以用第二双眼睛查看液体标记看看我错过了什么。

_note YAML header 中的标签组织为:

标签:[tag1, tag2, tag3, tag4]

到目前为止:

<!-- Create empty arrays -->
{% assign tags = '' | split: ',' %}
{% assign unique_tags = '' | split: ',' %}
{% assign counter = 0 %}

<!-- Map and flatten -->
{% assign note_tags = site.note | map: 'tags' | join: ',' | split: ',' %}

<!-- Push to tags -->
{% for tag in note_tags '%}
{% assign tags = tags | push: tag %}
{% endfor %}

<!-- Uniq -->
{% assign tags = tags | sort %}
{% for tag in tags %}
<!-- If not equal to previous then it must be unique -->
{% unless tag == previous %}
<!-- Push to unique_tags and count -->
{% assign unique_tags = unique_tags | push: tag %}
{% assign counter = counter | plus: 1 %}
{% endunless %}
{% assign previous = tag %}
{% endfor %}

{% for tag in unique_tags %}
{{ tag }} ({{ counter }}
{% endfor %}

在液体中使用 size 方法似乎不会返回正确的值。

最佳答案

新答案

{% comment %}map, flatten and sort{% endcomment %}
{% assign tags = site.note | map: 'tags' | join: ',' | split: ',' | sort %}
{% assign previousTag = "" %}
{% assign counter = 0 %}

{% for currentTag in tags %}

{% comment %}first loop : initializing previousTag{% endcomment %}
{% if previousTag == "" %}
{% assign previousTag = currentTag %}
{% endif %}

{% if currentTag == previousTag %}
{% assign counter = counter | plus: 1 %}
{% else %}
{{ previousTag }} ({{ counter }})
{% assign counter = 1 %}
{% endif %}

{% comment %}last loop : flushing what's left to print{% endcomment %}
{% if forloop.last %}
{{ currentTag }} ({{ counter }})
{% endif %}

{% assign previousTag = currentTag %}

{% endfor %}

旧答案(我是否遗漏了问题中的某些内容:是的!)

Liquid uniq filter来救援!

{% assign uniq_tags = site.note
| map: 'tags'
| join: ","
| split: ","
| uniq %}

<p>{{ uniq_tags | array_to_sentence_string }}</p>

关于jekyll - 在 Jekyll 中计算集合标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36479756/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com