简体   繁体   English

列出GitHub页面中的子类别

[英]List Subcategories in GitHub Pages

Edit: I've created a repository here that tests jibe's answer below. 编辑:我在这里创建了一个存储库用于测试下面的jibe答案。 I just end up getting a blank page when I visit /animals , so any help is appreciated! 当我访问/animals时,我最终得到一个空白页面,所以任何帮助都表示赞赏!

This is a follow-up to this question: Hierarchical Categories in GitHub Pages 这是对这个问题的后续跟进: GitHub页面中的分层类别

In that question, I found out how to list the posts of a particular hierarchical category. 在那个问题中,我发现了如何列出特定层次类别的帖子 Now I'm trying to figure out how to list the subcategories of a particular hierarchical category. 现在我想弄清楚如何列出特定层次类别的类别。

I'm using Jekyll on GitHub pages, and I want to have hierarchical categories like this: 我在GitHub页面上使用Jekyll,我想要像这样的分层类别:

  • animals -> mammals -> cats -> _posts -> housecat.md, tiger.md 动物 - >哺乳动物 - >猫 - > _posts - > housecat.md,tiger.md
  • animals -> mammals -> dogs -> _posts -> poodle.md, doberman.md 动物 - >哺乳动物 - >狗 - > _posts - > poodle.md,doberman.md
  • animals -> reptiles -> lizards -> _posts -> iguana.md, chameleon.md 动物 - >爬行动物 - >蜥蜴 - > _posts - > iguana.md,chameleon.md

I'd like users to be able to visit /animals and see a listing of the subcategories ( mammals and reptiles ). 我希望用户能够访问/animals并查看子类别( mammalsreptiles )的列表。 Then if they go to /animals/mammals , they'd see cats and dogs listed as subcategories. 然后,如果他们去/animals/mammals ,他们会看到catsdogs列为子类别。

I'm currently doing this manually by putting an index.html file inside each subcategory. 我目前通过在每个子类别中放入一个index.html文件来手动执行此操作。 But that makes updating things much more complicated than it probably should be. 但这使得更新事情变得比它应该更加复杂。

I've tried following this answer , but that's meant for single tags, not multiple categories. 我已经尝试过这个答案 ,但这意味着单个标签,而不是多个类别。

The catch is that any particular category might not be unique, so I can have stuff like this: 问题是任何特定的类别可能都不是唯一的,所以我可以有这样的东西:

  • animals -> mammals -> bats -> _posts -> vampire.md, fruit.md 动物 - >哺乳动物 - > 蝙蝠 - > _posts - > vampire.md,fruit.md
  • sports -> baseball -> bats -> _posts -> wiffle.md, teeball.md 体育 - >棒球 - > 蝙蝠 - > _posts - > wiffle.md,teeball.md

I'd also like to be able to define frontmatter attributes in the subcategories, maybe in the index.html file of each? 我还希望能够在子类别中定义frontmatter属性,可能在每个子类的index.html文件中? For example the animals->mammals->bats->index.html file would contain a frontmatter variable thumbnail with a value of "VampireBat.jpg" , and sports->baseball->bats->index.html would have a thumbnail of "YellowWiffleBat.png" . 例如, animals->mammals->bats->index.html文件将包含值为"VampireBat.jpg"的frontmatter变量thumbnail"VampireBat.jpg" sports->baseball->bats->index.html将包含thumbnail "YellowWiffleBat.png" I'd like to be able to access these variables from the parent level (to show a thumbnail and a link to the subcategory). 我希望能够从父级别访问这些变量(以显示缩略图和子类别的链接)。

My first thought was to access the subcategories directly, like this: 我的第一个想法是直接访问子类别,如下所示:

{% for mammalType in site.categories.animals.mammals %}
   <p>{{ mammalType.title }}</p>
   <img src="(( mammalType.thumbnail }}" />
{% endfor %}

Which I'd generalize using the categories from the page itself: 我将使用页面本身的类别进行概括:

{% for subcategory in page.categories %}
   <p>{{ subcategory.title }}</p>
   <img src="(( subcategory.thumbnail }}" />
{% endfor %}

But that doesn't work at all, since site.categories.whatever is a list of all of the posts in that category, ignoring any hierarchical information. 但这根本不起作用,因为site.categories.whatever是该类别中所有帖子的列表,忽略任何分层信息。

Is there a better way to approach this other than doing it manually? 除了手动操作之外,还有更好的方法吗?

As it was suggested in my deleted answer, I post an improved version of my answer of your previous question. 正如我在删除的答案中建议的那样,我发布了我之前问题答案的改进版本。 I also add information to answer your new questions (also deleted) : 我还添加了回答新问题的信息(也已删除):

Thanks for the reply. 谢谢回复。 This almost works, but it's got a few problems. 这几乎可行,但它有一些问题。 Most importantly, it doesn't support subcategories with the same name (like animals->bats and baseball->bats). 最重要的是,它不支持具有相同名称的子类别(如animals-> bats和baseball-> bats)。 It also lists every subcategory and every post under a particular category. 它还列出了特定类别下的每个子类别和每个帖子。 I only want to list the subcategories, not the posts. 我只想列出子类别,而不是帖子。 Is there a way to modify your approach to meet those requirements? 有没有办法修改您的方法以满足这些要求? – Kevin Workman yesterday - Kevin Workman昨天

Modify your _config.yml accordingly 相应地修改_config.yml

collections:
    animals:
        output: true
        mammals:
            output: true
            cats:
                output: true
            dogs:
                output: true
        reptiles:
            output: true
            lizards:
                output: true

then created the structure: 然后创建了结构:

mkdir -p _animals/reptiles/lizards
mkdir -p _animals/mammals/cats
mkdir _animals/mammals/dogs

add your md files and all index.html you need to make the list you want. 添加您的md文件和所有index.html,您需要创建所需的列表。 which will index items with filter. 它将使用过滤器索引项目。 From the top directory, your animals collection should look like this (with index.html in each folder) : 从顶部目录开始,您的animals集合应该如下所示(每个文件夹中都有index.html):

cleaner 清洁器

root/
└── _animals/
    ├── index.html
    ├── mammals
    │   ├── cats
    │   │   ├── housecat.md
    │   │   └── tiger.md
    │   ├── dogs
    │   │   ├── doberman.md
    │   │   └── poodle.md
    │   └── index.html
    └── reptiles
        └── lizards
            ├── chameleon.md
            └── iguana.md

new you can list only subcategories with or without going deeply (with an optional parameters) _includes/list_subcategories.html 新的你可以列出只有或没有深入的子类别(使用可选参数) _includes/list_subcategories.html

 {% assign animals = site.animals| sort:'title' %}
 {% assign from = page.url | remove: '/index.html' %}
 {% assign deep = (page.url | split: '/' | size) + 1 %}
 {% for animal in animals %}
 {% assign d = animal.url | remove: '/index.html' | split: '/' | size %}
 {% if animal.url != page.url and animal.url contains from and animal.url contains "index" and (include.dig or deep == d) %}
 <a  href={{ animal.url | prepend: site.baseurl }}>{{animal.title}}</a>
 {% endif %}
 {% endfor %}

improved similarly to list animals _includes/list_animals.html 改进类似于列出动物_includes/list_animals.html

{% assign animals = site.animals| sort:'title' %}
{% assign from = page.url | remove: '/index.html' %}
{% assign deep = (page.url | split: '/' | size) + 1 %}
{% for animal in animals %}
{% assign d = animal.url | remove: '/index.html' | split: '/' | size %}
{% if animal.url contains "index" or animal.url == page.url %}
{% else %}
    {% if animal.url contains from  and (include.dig or deep == d) %}
    <a  href={{ animal.url | prepend: site.baseurl }}>{{animal.title}}</a>
    {% endif %}
{% endif %}
{% endfor %}

list all subcategories and all animals in animals/index.html : 列出所有子类别和animals/index.html所有动物:

---
title: animals
---
{% include list_subcategories.html dig=true %}
<hr>
{% include list_animals.html dig=true %}

For example, to list all mammals and subcategoeries in animals/mammals/index.html : 例如,列出animals/mammals/index.html中的所有哺乳动物和次级animals/mammals/index.html

---
title: animals
---
{% include list_subcategories.html %}
<hr>
{% include list_animals.html %}

Finally the generated structure should look like this (with some more index.html): 最后生成的结构应该是这样的(更多的index.html):

cleaner 清洁器

root/
├─ _animals/
│   └─── ...
└── _site
    └── animals
        ├── index.html
        ├── mammals
        │   ├── cats
        │   │   ├── housecat.html
        │   │   └── tiger.html
        │   ├── dogs
        │   │   ├── doberman.html
        │   │   └── poodle.html
        │   └── index.html
        └── reptiles
            └── lizards
                ├── chameleon.html
                └── iguana.html

it solves your question. 它解决了你的问题。 I changed from taxonomy to dig, but you could also have distinguished between animals->bats and baseball->bats by putting taxonomy="baseball/bats" or taxonomy="animals/bats" . 我从分类学改为挖掘,但你也可以通过taxonomy="baseball/bats"taxonomy="animals/bats"来区分动物 - >蝙蝠和棒球 - >蝙蝠。

See simpyll.com for demo 有关演示,请参阅simpyll.com

See github for website code 有关网站代码,请参阅github

assign var page_depth as the current pages depth using the path '/' as a count variable 使用路径'/'作为计数变量,将var page_depth指定为当前页面深度

{% assign page_depth = page.url | split: '/' | size %}

assign var page_parent as the slug of the last directory housing 'index.md' 将var page_parent指定为最后一个目录'index.md'的slug

{% assign page_parent = page.url | split: '/' | last %}

loop through every page in the website 循环遍历网站中的每个页面

{% for node in site.pages offset:1 %}

skip website root 跳过网站root

{% if node.url == '/' %}
{{ continue }}
{% else %}

remove backslashed from each page in website 从网站的每个页面中删除backslashed

{% assign split_path = node.url | split: "/" %}

assign var node_last for each page in website 为网站中的每个页面分配var node_last

{% assign node_last = split_path | last %}

assign var node_parent as the slug of the last directory housing 'index.md' for each page in website 将var node_parent指定为网站中每个页面的最后一个目录“index.md”的slug

{% assign node_parent = node.url | remove: node_last | split: '/' | last %}

assign node_url for each page in website 为网站中的每个页面分配node_url

{% assign node_url = node.url %}

loop through each slug in each page path in website 循环遍历网站中每个页面路径中的每个slug

{% for slug in split_path offset:1 %}

assign var slug as the name of each slug therefore giving it a name 将var slug指定为每个slug的名称,从而为其命名

{% assign slug = slug %}

assign slug_depth with a forloop.index 使用forloop.index分配slug_depth

{% assign slug_depth = forloop.index %}

close for 靠近

{% endfor %}

obtain sub-directories for every page in website comparing depth and parent of current page to that of every other page in website 获取网站中每个页面的子目录,将当前页面的深度和父级与网站中每个其他页面的深度和父级进行比较

{% if slug_depth == page_depth and page_parent == node_parent %}<li><a href="{{ node_url }}">{{ slug }}</a></li>{% endif %}

obtain sub-directories for root (which we skipped early in this script). 获取root的子目录(我们在此脚本的早期跳过)。 we can use depth alone to define this. 我们可以单独使用深度来定义它。

{% if slug_depth == 1 and page.url == '/' and slug != 'search.json' and slug != 'sitemap.xml' %}<li><a href="{{ node_url }}">{{{slug}}</a></li>{% endif %}

close if and for 关闭if和for

{% endif %}
{% endfor %}

altogether: 共:

  {% assign page_depth = page.url | split: '/' | size %}
  {% assign page_parent = page.url | split: '/' | last %}
  {% for node in site.pages offset:1 %}
  {% if node.url == '/' %}
  {{ continue }}
  {% else %}
  {% assign split_path = node.url | split: "/" %}
  {% assign node_last = split_path | last %}
  {% assign node_parent = node.url | remove: node_last | split: '/' | last %}
  {% assign node_url = node.url %}
  {% for slug in split_path offset:1 %}
  {% assign slug = slug %}
  {% assign slug_depth = forloop.index %}
  {% endfor %}
  {% if slug_depth == page_depth and page_parent == node_parent %}
  <li><a href="{{ node_url }}">{{ slug }}</a></li>
  {% endif %}
  {% if slug_depth == 1 and page.url == '/' and slug != 'search.json' and   slug != 'sitemap.xml' %}
  <li><a href="{{ node_url }}">{{{slug}}</a></li>
  {% endif %}
  {% endif %}
  {% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM