我可以在Jekyll中创建嵌套集合吗?

Gre*_*son 12 jekyll

我想使用Jekyll创建一个包含几个章节的手册,每个章节包含几个部分,并将每个部分存储在单独的Markdown文件中.我希望index.md看起来像这样:

<ol>
{% for chapter in site.chapters %}
  <li>
    <a href="{{chapter.url}}">{{chapter.title}}</a>
    <ol>
    {% for section in chapter.sections %}
      <li><a href="{{section.url}}">{{section.title}}</a></li>
    {% endfor %}
    </ol>
  </li>
{% endfor %}
<ol>
Run Code Online (Sandbox Code Playgroud)

如果每个章节都是Markdown文件_chapters,并且我添加了正确的行_config.yml,我可以迭代章节,从YAML标题中拉出标题字段,等等.有没有办法嵌套这个?我尝试_chapters使用各种名称创建子目录,但是(a)Jekyll不会将它们作为子集合进行选择,并且(b)没有明显的地方存储章级信息(如章节的整体标题).

(注意:我想我可以通过显式枚举YAML块中的章节和部分_config.yml,或者通过创建一个单独的YAML文件来实现这一点_data,但我不想担心保持枚举与实际同步章节:我希望Jekyll自动接收更改.)

Pad*_*ddy 17

因此,我知道如何做到这一点的最好方法是颠倒你的想法.

你不是在写章节,而是在编写章节并将它们组织章节.

假设您有这样的设置:

  • _sections
    • section01.md
    • section02.md
    • section03.md
    • section04.md

但是你希望它输出如下:

  • _现场
      • chapter1.html(包含section01.md后跟section03.md)
      • chapter2.html(包含section02.md后跟section04.md)

如果是这种情况,您可以使用集合来实现.设置chapter在的frontmatter财产section01.mdsection03.md01chapter中的frontmatter财产section02.mdsection04.md02.

问题是生成章节的页面.您确实需要为每个章节创建一个页面,但如果您使用布局也不错.

这是我使用的布局(in _layouts/chapter.html):

---
layout: default
---

<h1>Chapter {{ page.chapter }}</h1>

{% for section in site.sections %}
{% if section.chapter == page.chapter %}
{{ section.output }}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

然后_chapters,我有chapter01.md,看起来像这样:

---
chapter: 01
layout: chapter
---
Run Code Online (Sandbox Code Playgroud)

只需将其复制到chapter02.md并将chapter属性设置为02,现在您有第2章.

完成这项工作所需的唯一其他事情是更新您的配置:

collections:
        sections:
                output: false
        chapters:
                output: true
Run Code Online (Sandbox Code Playgroud)

当你跑步时jekyll build,你现在拥有_site/chapters/chapter01.html_site/chapters/chapter02.html.随着新部分的创建,它们将被添加到其前端的任何章节中.

这一切都让人感到困惑,所以我在http://paddycarver.github.io/jekyll-nested-collections-example/上设置了一个样本,源代码为https://github.com/paddycarver/jekyll-nested-collections - 例如.