Skip to content

Instantly share code, notes, and snippets.

@barnett
Created August 12, 2020 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnett/085d8dd79fdf330e6b64830e30464dc7 to your computer and use it in GitHub Desktop.
Save barnett/085d8dd79fdf330e6b64830e30464dc7 to your computer and use it in GitHub Desktop.
Jekyll + Liquid dynamic navigation
{% assign pages = site.docs | where: 'hidden', false %}
{% if pages.size > 0 %}
{% assign head_pages = pages | where: 'parent', nil | sort: 'nav_priority' %}
{% for head_page in head_pages %}
<h5>
<a href="{{ head_page.url }}">{{ head_page.title }}</a>
</h5>
<ul>
{% assign child_pages = pages | where: 'parent', head_page.title | sort: 'nav_priority' %}
{% for child in child_pages %}
<li>
<a href="{{ child.url }}">{{ child.title }}</a>
</li>
{% endfor %}
</ul>
{% endfor %}
{% endif %}

Simple way to dynamically manage documentation through front matter without the need for a pesky config file.

Three variables one can set in front matter:

  • parent: Represents if this should be nested within another page for navigation
  • hidden: false for a page to be publicly listed, it'll be accessible via direct link regardless
  • nav_priority: Weights which document to show first, 1 is highest (ie top)

Logic

  • Can default to hide/show everything within _config.yml
  • A page shows up as a header if it:
    • has no parent OR
    • is a parent page
  • If a page has a parent, it will show up underneath the parent
    • Can only have 2 levels
  • If a parent is hidden, then all child pages are also hidden
  • Weighting can be defaulted for documents in _config.yml to 100 to keep them at the bottom, or 1 for at top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment