【问题标题】:Can I create nested collections in Jekyll?我可以在 Jekyll 中创建嵌套集合吗?
【发布时间】:2016-09-13 15:34:32
【问题描述】:

我想使用 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>

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

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

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    所以我知道如何做到这一点的最好方法就是颠覆你的想法。

    您不是在编写章节,而是在编写章节并将它们组织成章节。

    假设您有这样的设置:

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

    但你希望它像这样输出:

    • _网站
      • 章节
        • chapter1.html(包含section01.md,后跟section03.md
        • chapter2.html(包含section02.md,后跟section04.md

    如果是这种情况,您可以使用集合来实现。将section01.mdsection03.md的frontmatter中的chapter属性设置为01,将section02.mdsection04.md的frontmatter中的chapter属性设置为02

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

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

    ---
    layout: default
    ---
    
    <h1>Chapter {{ page.chapter }}</h1>
    
    {% for section in site.sections %}
    {% if section.chapter == page.chapter %}
    {{ section.output }}
    {% endif %}
    {% endfor %}
    

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

    ---
    chapter: 01
    layout: chapter
    ---
    

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

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

    collections:
            sections:
                    output: false
            chapters:
                    output: true
    

    当您运行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-example

    【讨论】:

    • 感谢 Paddy - 这有点可怕,但它会起作用。
    • 我的荣幸!抱歉花了这么长时间。使用 jekyll 插件可以稍微改进(自动创建章节文件),但这在 Github Pages 上不起作用。
    • 我可能很笨,但我在源代码中找不到字符串“这是第 01 节的链接”。我错过了什么?
    • 检查 gh-pages 分支
    • 不嵌套if,另一种方法是定义一个类似{{ assign page_sections = site.sections | where "chapter",page.chapter }}的变量,然后对其进行迭代。
    【解决方案2】:

    _collection 需要注意的一点与_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
    

    然后像这样按他们的path 排序到索引文件

    <ol>
    {% assign items = site.chapter | sort: 'weight' %}
    {% for item in items %}
      {% if not item.path contains '/section' %}
      <li>
        <a href="{{item.url}}">{{item.title}}</a>
        <ol>
      {% else %}
          <li><a href="{{item.url}}">{{item.title}}</a></li>
      {% endif %}
        </ol>
      </li>
    {% endfor %}
    <ol>
    

    输出

    <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>
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2015-05-25
      • 2015-12-06
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多