Jekyll:对集合中的项目进行排序,同时遍历集合

Jekyll: Sort items in collections, while looping over collections

我想遍历我的 Jekyll 站点中的所有集合,并在其中排序并列出该集合中的所有页面。

,我可以遍历所有的集合和项目:

{% for collection in site.collections %}
  <h2>Items from {{ collection.label }}</h2>
  <ul>
    {% assign pages = site[collection.label] %}
    {% for item in pages %}
      <li><a href="{{ item.url }}">{{ item.title }}</a></li>
    {% endfor %}
  </ul>
{% endfor %}

对于具体的集合,我也可以从frontmatter中对update字段进行排序:

{% assign sorted = site.programming | sort: 'update' %}

但是如果我尝试将其应用于第一个示例,它会失败:

{% assign pages = site[collection.label] | sort: 'update' %}

这给出了一个相当普遍的无用错误:

  Liquid Exception: Liquid error (line 30): comparison of Array with Array failed in index.md
             Error: Liquid error (line 30): comparison of Array with Array failed
             Error: Run jekyll build --trace for more information.

我猜测 site[collection.label] returns 与 site.programming 有所不同,但我不确定是什么或如何解决这个问题。

编辑:我尝试使用 collection.docs 而不是 site[collection.label] 并得到了同样的错误。

这个错误的原因原来不是collection.docssite[collection.label]之间的区别。相反,在其中一个集合中,有一个项目的 frontmatter 不包含 update 字段。结果,它无法按此字段排序,因为并非所有项目都有它。同样,如果 types 在所有情况下都不可比较,则会出现相同的错误。在这种情况下,它们都应该是日期;如果日期格式无效,则失败。

我仍然认为这是一条非常模棱两可的错误消息。