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

我可以在Jekyll中创建嵌套集合吗?,jekyll,Jekyll,我想使用Jekyll创建一个包含多个章节的手册,每个章节包含多个章节,并将每个章节存储在单独的标记文件中。我希望index.md看起来像这样: <ol> {% for chapter in site.chapters %} <li> <a href="{{chapter.url}}">{{chapter.title}}</a> <ol> {% for section in chapter.sections %

我想使用Jekyll创建一个包含多个章节的手册,每个章节包含多个章节,并将每个章节存储在单独的标记文件中。我希望
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>
---
chapter: 01
layout: chapter
---

{site.chapters%%中的章的百分比}
  • {chapter.sections%中的节的%s}
  • {%endfor%} {%endfor%}
    如果每个章节都是
    \u chapters
    中的一个标记文件,并且我在
    \u config.yml
    中添加了正确的行,那么我可以迭代章节,从YAML标题中拉出标题字段,等等。有没有办法嵌套它?我曾尝试在
    \u chapters
    下创建具有不同名称的子目录,但(a)Jekyll不会将它们作为子集合来收集,(b)没有明显的位置来存储章节级信息(如章节的总标题)


    (注意:我想我可以通过在
    \u config.yml
    中显式枚举YAML块中的章节和部分,或者通过在
    \u data
    中创建单独的YAML文件来实现这一点,但我不想担心使枚举与实际的章节和部分保持同步:我希望Jekyll自动获取更改。)所以我知道的最好的方法就是改变你的想法

    你不是在写章节,你是在写章节并把它们组织成章节

    假设您有如下设置:

    <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>
    
    ---
    chapter: 01
    layout: chapter
    ---
    
    • _部分
      • 第01.md节
      • 第02.md节
      • 第03.md节
      • 第04.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>
    
    ---
    chapter: 01
    layout: chapter
    ---
    
    • _场地
      • 章节
        • chapter1.html(包含
          section01.md
          后接
          section03.md
        • chapter2.html(包含
          section02.md
          后接
          section04.md
    如果是这样的话,您可以使用集合来实现。将
    section01.md
    section03.md
    前面的
    chapter
    属性设置为
    01
    ,将
    section02.md
    section04.md
    前面的
    chapter
    属性设置为
    02

    问题是为章节生成页面。你确实需要为每一章制作一个页面,但是如果你使用一个布局也不错

    这是我使用的布局(在
    \u layouts/chapter.html
    中):

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

    要实现此功能,唯一需要做的是更新配置:

    collections:
            sections:
                    output: false
            chapters:
                    output: true
    
    当您运行
    jekyll build
    时,您现在将拥有
    \u site/chapters/chapter01.html
    \u site/chapters/chapter02.html
    。随着新的章节的创建,它们将被添加到其frontmatter中的任何章节中


    这真的很让人困惑,所以我用源代码在设置了一个示例。

    对于
    \u collection
    需要注意的一点与
    \u post
    相同:

    嵌套文件的处理方式与根文件夹中的文件相同

    您可以使用
    weight
    按如下方式排列降价文件:

    _chapter
       chapter1.md <- put weight: 10
       chapter2.md <- put weight: 20
       _chapter/section1
          section11.md <- put weight: 11
          section12.md <- put weight: 12
       _chapter/section2
          section21.md <- put weight: 21
          section22.md <- put weight: 22
    
    输出

    <ol>
      <li>
        <a href="chapter1.html">chapter1</a>
        <ol>
          <li><a href="section11.html">section11</a></li>
          <li><a href="section12.html">section12</a></li>
        </ol>
      </li>
      <li>
        <a href="chapter2.html">chapter2</a>
        <ol>
          <li><a href="section21.html">section21</a></li>
          <li><a href="section22.html">section22</a></li>
        </ol>
      </li>
    <ol>
    
    
    

  • 谢谢你,帕迪-这有点可怕,但它会起作用的。我的荣幸!抱歉花了这么长时间。使用jekyll插件可以稍微改进(自动创建章节文件),但这在Github页面上不起作用。我可能很笨,但在源代码中找不到字符串“This is a link to Section 01”。我遗漏了什么?检查gh pages分支而不是嵌套
    if
    ,另一种方法是定义一个变量,如
    {assign page_sections=site.sections | where“chapter”,page.chapter}
    ,然后迭代该变量。