GitHub 页中的分层类别
Hierarchical Categories in GitHub Pages
我在 GitHub 页面上使用 Jekyll,我希望有这样的分层类别:
- 动物 -> 哺乳动物 -> 猫 -> _posts -> housecat.md, tiger.md
- 动物 -> 哺乳动物 -> 狗 -> _posts -> poodle.md, doberman.md
- 动物 -> 爬行动物 -> 蜥蜴 -> _posts -> iguana.md, chameleon.md
我希望用户能够访问 /animals
并查看每个 post 每个类别的列表。但如果他们去 /animals/mammals
,他们只会看到哺乳动物。如果他们去 /animals/mammals/cats
,那么他们只会看到猫。
我知道我可以通过在每个目录中放置一个 index.html
文件然后循环遍历 site.categories.mammals
或 site.categories.cats
来手动执行此操作。
但这似乎有点太暴力了,我希望有更好的方法。如果我想更改显示列表的方式,我将不得不在每个子类别中进行更改。当子类别共享名称时,我也会遇到问题,例如 /ABC/XYZ/_posts/one.md
和 /DEF/XYZ/_posts/two.md
.
我已尝试关注 this 文章,该文章使用一个主 category.html
页面循环 page.category
:
{% for post in site.categories.[page.category] %}
<h2><a href=""></a></h2>
<p></p>
{% endfor %}
然后每个 index.html
文件都使用它作为其布局。这几乎可行,但它似乎仅限于一个类别,而不是多个层次类别。
是否有更简单的方法来为分层类别创建列表?
page.categories 是一个列表
{% for cat in page.categories %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.categories[cat] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
来自 jekyll 关于 page.category http://jekyllrb.com/docs/variables/#page-variables
的文档
The list of categories to which this post belongs. Categories are
derived from the directory structure above the _posts directory. For
example, a post at /work/code/_posts/2008-12-24-closures.md would have
this field set to ['work', 'code']. These can also be specified in the
YAML Front Matter.
您应该能够轻松地向每个文件夹添加一个简单的动态 index.html,并且类别应该自动分层。
更新
以上方法无效。您需要将每个层次结构的类别组合视为唯一项。合并索引页的类别,并将其与站点中的所有帖子进行比较。
/foo/bar/_posts/2016-08-01-foo-bar-test.html
---
categories:
- foo
- bar
title: test foo bar
---
<h2>Foo Bar</h2>
/var/bar/_posts/2016-08-01-test-var-bar.html
---
categories:
- var
- bar
title: test var bar
---
<h2>Var Bar</h2>
/foo/bar/index.html
---
categories:
- foo
- bar
---
{% assign pagecat = page.categories | join ' ' | append: ' '%}
{% assign pagecatlen = page.categories.size %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.posts %}
{% assign postcat = '' %}
{% for thispostcat in post.categories limit: pagecatlen %}
{% assign postcat = postcat | append: thispostcat %}
{% assign postcat = postcat | append: ' ' %}
{% endfor %}
{% if (postcat == pagecat) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
类别对于 _posts 中的文件是可选的,但它们对于每个索引文件的前题是必需的。
相应地修改您的_config.yml
collections:
animals:
output: true
mammals:
output: true
cats:
output: true
dogs:
output: true
reptiles:
output: true
lizards:
output: true
然后创建结构:
mkdir -p _animals/reptiles/lizards
mkdir -p _animals/mammals/cats
mkdir _animals/mammals/dogs
添加您的 md 文件和所有 index.html 将使用过滤器索引项目。它应该看起来像这样(可能有更多的索引):
_animals/
├── index.html
├── mammals
│ ├── cats
│ │ ├── housecat.md
│ │ └── tiger.md
│ ├── dogs
│ │ ├── doberman.md
│ │ └── poodle.md
│ └── index.html
└── reptiles
└── lizards
├── chameleon.md
└── iguana.md
然后你创建_includes/list_animals.html
{% assign animals = site.animals| sort:'title' %}
{% for animal in animals %}
{% if page.url != animal.url and include.taxonomy == nil or animal.url contains include.taxonomy %}
<a href={{ animal.url | prepend: site.baseurl }}>{{animal.title}}</a>
{% endif %}
{% endfor %}
列出animals/index.html
中的所有动物:
---
title: animals
---
{% include list_animals.html %}
例如,要列出 animals/mammals/index.html
中的所有哺乳动物:
---
title: animals
---
{% include list_animals.html taxonomy="mammals" %}
最终生成的结构应该如下所示(还有一些 index.html):
_site
└── animals
├── index.html
├── mammals
│ ├── cats
│ │ ├── housecat.html
│ │ └── tiger.html
│ ├── dogs
│ │ ├── doberman.html
│ │ └── poodle.html
│ └── index.html
└── reptiles
└── lizards
├── chameleon.html
└── iguana.html
我在 GitHub 页面上使用 Jekyll,我希望有这样的分层类别:
- 动物 -> 哺乳动物 -> 猫 -> _posts -> housecat.md, tiger.md
- 动物 -> 哺乳动物 -> 狗 -> _posts -> poodle.md, doberman.md
- 动物 -> 爬行动物 -> 蜥蜴 -> _posts -> iguana.md, chameleon.md
我希望用户能够访问 /animals
并查看每个 post 每个类别的列表。但如果他们去 /animals/mammals
,他们只会看到哺乳动物。如果他们去 /animals/mammals/cats
,那么他们只会看到猫。
我知道我可以通过在每个目录中放置一个 index.html
文件然后循环遍历 site.categories.mammals
或 site.categories.cats
来手动执行此操作。
但这似乎有点太暴力了,我希望有更好的方法。如果我想更改显示列表的方式,我将不得不在每个子类别中进行更改。当子类别共享名称时,我也会遇到问题,例如 /ABC/XYZ/_posts/one.md
和 /DEF/XYZ/_posts/two.md
.
我已尝试关注 this 文章,该文章使用一个主 category.html
页面循环 page.category
:
{% for post in site.categories.[page.category] %}
<h2><a href=""></a></h2>
<p></p>
{% endfor %}
然后每个 index.html
文件都使用它作为其布局。这几乎可行,但它似乎仅限于一个类别,而不是多个层次类别。
是否有更简单的方法来为分层类别创建列表?
page.categories 是一个列表
{% for cat in page.categories %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.categories[cat] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
来自 jekyll 关于 page.category http://jekyllrb.com/docs/variables/#page-variables
的文档The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter.
您应该能够轻松地向每个文件夹添加一个简单的动态 index.html,并且类别应该自动分层。
更新
以上方法无效。您需要将每个层次结构的类别组合视为唯一项。合并索引页的类别,并将其与站点中的所有帖子进行比较。
/foo/bar/_posts/2016-08-01-foo-bar-test.html
---
categories:
- foo
- bar
title: test foo bar
---
<h2>Foo Bar</h2>
/var/bar/_posts/2016-08-01-test-var-bar.html
---
categories:
- var
- bar
title: test var bar
---
<h2>Var Bar</h2>
/foo/bar/index.html
---
categories:
- foo
- bar
---
{% assign pagecat = page.categories | join ' ' | append: ' '%}
{% assign pagecatlen = page.categories.size %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.posts %}
{% assign postcat = '' %}
{% for thispostcat in post.categories limit: pagecatlen %}
{% assign postcat = postcat | append: thispostcat %}
{% assign postcat = postcat | append: ' ' %}
{% endfor %}
{% if (postcat == pagecat) %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
类别对于 _posts 中的文件是可选的,但它们对于每个索引文件的前题是必需的。
相应地修改您的_config.yml
collections:
animals:
output: true
mammals:
output: true
cats:
output: true
dogs:
output: true
reptiles:
output: true
lizards:
output: true
然后创建结构:
mkdir -p _animals/reptiles/lizards
mkdir -p _animals/mammals/cats
mkdir _animals/mammals/dogs
添加您的 md 文件和所有 index.html 将使用过滤器索引项目。它应该看起来像这样(可能有更多的索引):
_animals/
├── index.html
├── mammals
│ ├── cats
│ │ ├── housecat.md
│ │ └── tiger.md
│ ├── dogs
│ │ ├── doberman.md
│ │ └── poodle.md
│ └── index.html
└── reptiles
└── lizards
├── chameleon.md
└── iguana.md
然后你创建_includes/list_animals.html
{% assign animals = site.animals| sort:'title' %}
{% for animal in animals %}
{% if page.url != animal.url and include.taxonomy == nil or animal.url contains include.taxonomy %}
<a href={{ animal.url | prepend: site.baseurl }}>{{animal.title}}</a>
{% endif %}
{% endfor %}
列出animals/index.html
中的所有动物:
---
title: animals
---
{% include list_animals.html %}
例如,要列出 animals/mammals/index.html
中的所有哺乳动物:
---
title: animals
---
{% include list_animals.html taxonomy="mammals" %}
最终生成的结构应该如下所示(还有一些 index.html):
_site
└── animals
├── index.html
├── mammals
│ ├── cats
│ │ ├── housecat.html
│ │ └── tiger.html
│ ├── dogs
│ │ ├── doberman.html
│ │ └── poodle.html
│ └── index.html
└── reptiles
└── lizards
├── chameleon.html
└── iguana.html