Jekyll 遍历自定义文件夹

Jekyll iterate over custom folder

我正在使用 Jekyll 并有一个名为 docs 的自定义文件夹,其结构如下:

该文件夹被组织为嵌套类别,但据我从 this pull request 中了解到,如果我这样使用 collections_dir,我只能多一层:

collections:
  js:
    output: true
  ruby:
    output: true
collections_dir: docs

我想要的是能够遍历 docs 文件夹下的所有文件,以便为 simple-jekyll-search.

创建一个搜索索引

我需要在此处更改 site.pages 以在 docs 文件夹上进行迭代,但我不知道我该怎么做,也不知道我是否可以以我喜欢的方式来做不需要自己定义一个 .yml 文件来列出文件夹中的所有文件。

---
layout: null
---
[
  {% for page in site.pages %}
   {
     {% if page.title != nil %}
        "title"    : "{{ page.title | escape }}",
        "url"      : "{{ site.baseurl }}{{ page.url }}",
        "date"     : "{{ page.date }}"
     {% endif %}
   } {% unless forloop.last %},{% endunless %}
  {% endfor %}
]

您可以在 Jekyll 中遍历所有集合,然后访问每个集合中的 documents/files。请参阅 iterating over collections and accessing their documents.

上的 Jekyll 文档

所以你只需要稍微调整一下你得到的东西就可以变成这样:

---
layout: null
---
[
  {%- for collection in site.collections %}
  {%- for page in collection.docs -%}
   {
     {%- if page.title != nil %}
        "title"    : "{{ page.title | escape }}",
        "url"      : "{{ site.baseurl }}{{ page.url }}",
        "date"     : "{{ page.date }}"
     {% endif -%}
   }{% unless forloop.last %},{% endunless %}
  {%- endfor -%}
  {% endfor -%}
]