是否可以在一个数组中对 Jekyll Collection 和 Jekyll Data 进行排序?

Is it possible do sort Jekyll Collection and Jekyll Data in one array?

如何对 2 for 语句混合的 <ul> 进行排序?
我已经尝试使用 javascript 和 jquery,但我只得到 <ul>NULL 的错误。

<ul>
  {% assign docs = site.documents | sort: "title" %} 
  {% for document in docs %}
  <li>
    <a href="{{ document.url }}">{{ document.title }} - {{ document.url }}</a>
  </li>
  {% endfor %} 
  {% assign dataDocs = site.data.documents | sort: "title" %} 
  {% for document in dataDocs %}
  <li>
    <a href="{{ document.url }}" download="{{ document.path }}">{{ document.title }} - {{ document.url }}</a>
  </li>
  {% endfor %}
</ul>

您正在对每个列表而不是一个列表进行排序。你可以在排序之前把它做成一个列表,并通过给它们一个额外的前端标识符来不同地呈现每个项目(is_from_data):

<ul>
  {% assign docs = site.documents | concat: site.data.documents | sort: "title" %}

  {% for document in docs %}
    <li>
      {% if document.is_from_data %}
        <a href="{{ document.url }}" download="{{ document.path }}">{{ document.title }} - {{ document.url }}</a>
      {% else %}
        <a href="{{ document.url }}">{{ document.title }} - {{ document.url }}</a>
      {% endif %}
    </li>
  {% endfor %}
</ul>

您可以将 is_from_data: true 添加到每个项目的前面或在您的配置中使用 Jekyll 默认值。


旧版本的 Jekyll 可能没有 concat 过滤器。您可以通过将每个元素推送到一个新数组来解决这个问题:

{% assign docs = "" | split: "" %}

{% for doc in site.documents %}
  {% assign docs = docs | push: doc %}
{% endfor %}

{% for doc in site.data.documents %}
  {% assign docs = docs | push: doc %}
{% endfor %}