Hierarchical Categories in GitHub Pages

前端 未结 2 1673
暖寄归人
暖寄归人 2020-12-31 03:33

I\'m using Jekyll on GitHub pages, and I want to have hierarchical categories like this:

  • animals -> mammals -> cats -> _posts -> housecat.md, tiger.md
相关标签:
2条回答
  • 2020-12-31 04:19

    page.categories is a list

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

    From jekyll's documentation about 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.

    You should be easily able to add a simple dynamic index.html to every folder and the categories should be hierarchical automatically.

    Update

    The above does NOT work. You need to treat the combination of categories of each hierarchy as a unique item. Concat the index page's categories, and compare that against all the posts in the site.

    /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>
    

    The categories are optional for the files in _posts, but they are required for the front matter of each index file.

    0 讨论(0)
  • 2020-12-31 04:28

    Modify your _config.yml accordingly

    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 which will index items with filter. It should look like this (maybe with more indexes) :

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

    then you create _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 %} 
    

    to list all animals in animals/index.html :

    ---
    title: animals
    ---
    {% include list_animals.html %}
    

    For example, to list all mammals in animals/mammals/index.html :

    ---
    title: animals
    ---
    {% include list_animals.html taxonomy="mammals" %}
    

    Finally the generated structure should look like this (with some more 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 讨论(0)
提交回复
热议问题