Jekyll 中不同的标签列表
Distinct list of tags in Jekyll
我正在重写我的博客以使用 Jekyll。 Jekyll 使用 Liquid 模板语言,因此学习如何自定义有点困难。
我有很多 .md
个文件(markdown),每个 post 个文件。对于我在前面的以下内容中的每个文件:
---
layout: portfolio
title: "Project Title"
date: 2015-12-12 17:53:00
categories: portfolio
tag: web
featured: true
---
在标签部分,我为每个项目使用一个或多个标签。我知道:
{% for project in site.categories['project']%}
do some stuff
{% endfor %}
我对每个项目进行迭代。但是我有多个文件的相同标签,我想有一个不同的标签列表。我怎样才能做到这一点?
我认为您正在寻找类似 this:
的内容
<!-- Create empty arrays -->
{% assign tags = '' | split: ',' %}
{% assign unique_tags = '' | split: ',' %}
<!-- Map and flatten -->
{% assign article_tags = site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' %}
{% assign tutorial_tags = site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' %}
<!-- Push to tags -->
{% for tag in article_tags '%}
{% assign tags = tags | push: tag %}
{% endfor %}
{% for tag in tutorial_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 as sorted -->
{% unless tag == previous %}
<!-- Push to unique_tags -->
{% assign unique_tags = unique_tags | push: tag %}
{% endunless %}
{% assign previous = tag %}
{% endfor %}
那么unique_tags应该就是你想要的结果了吧
2018 年更新的解决方案:
<!-- Gather unique tags from articles -->
{% assign tags_articles = site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Article tags: {{ tags_articles | join: "," | prepend: "[" | append: "]" }}</h2>
<!-- Gather unique tags from tutorials -->
{% assign tags_tutorials = site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Tutorial tags: {{ tags_tutorials | join: "," | prepend: "[" | append: "]" }}</h2>
<!-- Combine and leave unique only -->
{% assign combo = tags_articles | concat: tags_tutorials | uniq | sort %}
<h2>Combo: {{ combo | join: "," | prepend: "[" | append: "]" }}</h2>
我正在重写我的博客以使用 Jekyll。 Jekyll 使用 Liquid 模板语言,因此学习如何自定义有点困难。
我有很多 .md
个文件(markdown),每个 post 个文件。对于我在前面的以下内容中的每个文件:
---
layout: portfolio
title: "Project Title"
date: 2015-12-12 17:53:00
categories: portfolio
tag: web
featured: true
---
在标签部分,我为每个项目使用一个或多个标签。我知道:
{% for project in site.categories['project']%}
do some stuff
{% endfor %}
我对每个项目进行迭代。但是我有多个文件的相同标签,我想有一个不同的标签列表。我怎样才能做到这一点?
我认为您正在寻找类似 this:
的内容<!-- Create empty arrays -->
{% assign tags = '' | split: ',' %}
{% assign unique_tags = '' | split: ',' %}
<!-- Map and flatten -->
{% assign article_tags = site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' %}
{% assign tutorial_tags = site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' %}
<!-- Push to tags -->
{% for tag in article_tags '%}
{% assign tags = tags | push: tag %}
{% endfor %}
{% for tag in tutorial_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 as sorted -->
{% unless tag == previous %}
<!-- Push to unique_tags -->
{% assign unique_tags = unique_tags | push: tag %}
{% endunless %}
{% assign previous = tag %}
{% endfor %}
那么unique_tags应该就是你想要的结果了吧
2018 年更新的解决方案:
<!-- Gather unique tags from articles -->
{% assign tags_articles = site.articles | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Article tags: {{ tags_articles | join: "," | prepend: "[" | append: "]" }}</h2>
<!-- Gather unique tags from tutorials -->
{% assign tags_tutorials = site.tutorials | map: 'tags' | join: ',' | join: ',' | split: ',' | uniq | sort %}
<h2>Tutorial tags: {{ tags_tutorials | join: "," | prepend: "[" | append: "]" }}</h2>
<!-- Combine and leave unique only -->
{% assign combo = tags_articles | concat: tags_tutorials | uniq | sort %}
<h2>Combo: {{ combo | join: "," | prepend: "[" | append: "]" }}</h2>