我可以在Jekyll中创建嵌套集合吗? [英] Can I create nested collections in 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 bysection03.md
) - chapter2.html (contains
section02.md
followed bysection04.md
)
在这种情况下,您可以使用集合来做到这一点.将
section01.md
和section03.md
的最前面的chapter
属性设置为01
,并将section02.md
和section04.md
的最前面的chapter
属性设置为02
.If that's the case, you can do so using collections. Set the
chapter
property in the frontmatter ofsection01.md
andsection03.md
to01
and thechapter
property in the frontmatter ofsection02.md
andsection04.md
to02
.问题是为各章生成页面.您要做需要为每一章制作一个页面,但是如果您使用布局也不错.
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 havechapter01.md
, which looks like this:--- chapter: 01 layout: chapter ---
只需将其复制到
chapter02.md
并将chapter
属性设置为02
,现在您便有了第二章.Just copy that to
chapter02.md
and set thechapter
property to02
, 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屋!
- chapter1.html (contains
- chapters
- chapter1.html(包含
- 章节