GitHub 页面中的分层类别

问题描述 投票:0回答:3

我在 GitHub 页面上使用 Jekyll,我希望有这样的分层类别:

  • 动物 -> 哺乳动物 -> 猫 -> _posts -> housecat.md, Tiger.md
  • 动物 -> 哺乳动物 -> 狗 -> _posts -> poodle.md, doberman.md
  • 动物 -> 爬行动物 -> 蜥蜴 -> _posts -> iguana.md, chameleon.md

我希望用户能够访问

/animals
并查看每个类别的每个帖子的列表。但如果他们去
/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
文件都使用它作为其布局。这几乎可行,但似乎仅限于一个类别,而不是多个层次类别。

是否有更简单的方法来为分层类别创建列表?

jekyll liquid github-pages
3个回答
5
投票

page.categories 是一个列表

https://stackoverflow.com/a/23927986

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

该帖子所属的类别列表。类别有 派生自 _posts 目录上方的目录结构。为了 例如,/work/code/_posts/2008-12-24-closures.md 上的帖子会有 此字段设置为 ['work', 'code']。这些也可以在 YAML 前面的内容。

您应该能够轻松地向每个文件夹添加一个简单的动态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 中的文件,类别是可选的,但对于每个索引文件的前文,类别是必需的。


4
投票

相应地修改你的 _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

0
投票

我觉得如果这是在主题中开箱即用而不是 DIY 提供的,那就太好了。

这个主题,just-the-docs - 一个现代的、高度可定制的、响应式的 Jekyll 主题,用于带有内置搜索的文档,确实提供了父/子/孙子类型的组织。

导航结构 |只是文档 |示例(按集合分组)

© www.soinside.com 2019 - 2024. All rights reserved.