我可以在Jekyll中创建嵌套集合吗? [英] Can I create nested collections in Jekyll?

查看:44
本文介绍了我可以在Jekyll中创建嵌套集合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to use Jekyll to create a manual that contains several chapters, each of which contains several sections, and store each section in a separate Markdown file. I want index.md to look something like this:

<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)没有明显的地方可以存储章级别的信息(例如,章的整体信息).标题).

If each chapter is a Markdown file in _chapters, and I add the right lines to _config.yml, I can iterate over the chapters, pull out the title field from the YAML header, etc. Is there a way to nest this? I've tried creating sub-directories under _chapters with various names, but (a) Jekyll doesn't pick them up as sub-collections and (b) there's no obvious place to store chapter-level information (like the chapter's overall title).

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

(Note: I think I can do this by explicitly enumerating chapters and sections in a block of YAML in _config.yml, or by creating a separate YAML file in _data, but I don't want to have to worry about keeping that enumeration in sync with the actual chapters and sections: I'd like Jekyll to pick up changes automatically.)

推荐答案

所以我知道如何做到这一点的最好方法是颠倒你的思维.

So the best way I know how to do this is to invert your thinking.

您不是在编写章节,而是在编写章节并将其组织为章节.

You're not writing chapters, you're writing sections and organising them into chapters.

假设您具有这样的设置:

Assume you have a setup like this:

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

      但是您希望它输出如下:

      But you want it output like this:

      • _site
        • 章节
          • chapter1.html(包含section01.md后跟section03.md)
          • chapter2.html(包含section02.md后跟section04.md)
          • _site
            • chapters
              • chapter1.html (contains section01.md followed by section03.md)
              • chapter2.html (contains section02.md followed by section04.md)

              在这种情况下,您可以使用集合来做到这一点.将section01.mdsection03.md的最前面的chapter属性设置为01,并将section02.mdsection04.md的最前面的chapter属性设置为02.

              If that's the case, you can do so using collections. Set the chapter property in the frontmatter of section01.md and section03.md to 01 and the chapter property in the frontmatter of section02.md and section04.md to 02.

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

              The problem is generating the pages for the chapters. You do need to make a page for each chapter, but it's not bad if you use a layout.

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

              Here's the layout I used (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 %}
              

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

              Then in _chapters, I have chapter01.md, which looks like this:

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

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

              Just copy that to chapter02.md and set the chapter property to 02, and now you have Chapter 2.

              要做此工作的唯一另一件事是更新您的配置:

              The only other thing required to make this work is updating your config:

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

              运行jekyll build时,现在将具有_site/chapters/chapter01.html_site/chapters/chapter02.html.创建新的部分后,它们将被添加到其最前面的任何章节中.

              When you run jekyll build, you'll now have _site/chapters/chapter01.html and _site/chapters/chapter02.html. As new sections get created, they'll be added to whatever chapter is in their frontmatter.

              这真的很令人困惑,因此我在 http://上设置了一个示例paddycarver.github.io/jekyll-nested-collections-example/,其源代码位于 https://github.com/paddycarver/jekyll-nested-collections-example .

              This is all really confusing, so I set up a sample at http://paddycarver.github.io/jekyll-nested-collections-example/ with source code at https://github.com/paddycarver/jekyll-nested-collections-example.

              这篇关于我可以在Jekyll中创建嵌套集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆