Sorted navigation menu with Jekyll and Liquid

后端 未结 10 2209
清酒与你
清酒与你 2020-12-07 11:57

I\'m constructing a static site (no blog) with Jekyll/Liquid. I want it to have an auto-generated navigation menu that lists all existing pages and highlight the current pag

10条回答
  •  半阙折子戏
    2020-12-07 12:38

    Below solution works on Github (doesn't require a plugin):

    {% assign sorted_pages = site.pages | sort:"name" %}
    {% for node in sorted_pages %}
      
  • {{node.title}}
  • {% endfor %}

    Above snippet sorts pages by file name (name attribute on Page object is derived from file name). I renamed files to match my desired order: 00-index.md, 01-about.md – and presto! Pages are ordered.

    One gotcha is that those number prefixes end up in the URLs, which looks awkward for most pages and is a real problem in with 00-index.html. Permalilnks to the rescue:

    ---
    layout: default
    title: News
    permalink: "index.html"
    ---
    

    P.S. I wanted to be clever and add custom attributes just for sorting. Unfortunately custom attributes are not accessible as methods on Page class and thus can't be used for sorting:

    {% assign sorted_pages = site.pages | sort:"weight" %} #bummer
    

提交回复
热议问题