gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:49:31 25 4
gpt4 key购买 nike

我想使用 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 header 中提取标题字段等。有没有办法嵌套这个?我尝试在 _chapters 下创建子目录有各种名称,但是 (a) Jekyll 不会将它们作为子集合来选择,并且 (b) 没有明显的位置来存储章节级别的信息(如章节的整体标题)。

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

最佳答案

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

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

假设您有这样的设置:

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

  • 但是你希望它像这样输出:
  • _地点
  • 章节
  • 第1章.html(包含section01.md后跟section03.md)
  • 第2章.html(包含section02.md后跟section04.md)

  • 如果是这种情况,您可以使用集合来执行此操作。设置 chapter section01.md 的前端内容中的属性和 section03.md01chapter section02.md 的前端内容中的属性和 section04.md02 .

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

    这是我使用的布局(在 _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属性(property)给 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 .

    关于jekyll - 我可以在 Jekyll 中创建嵌套集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277738/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com