GitHub Pages can't run custom Jekyll plug-ins so when generating Tables of Contents (TOCs), you're stuck with either a JavaScript solution or using kramdown's {:toc}
option. However, by using {:toc}
, you are forced to have that code next to your actual markdown and you can't place it in a layout. This means every. single. post. will need to have the snippet. If you choose the JavaScript approach, that's perfectly fine but what if JS is disabled on someone's browser or your page is just really long and it becomes inefficient.
Instead, I wrote this solution entirely in Liquid and can be used as an {% include %}
in any website you want, in any layout you want. Want to see it in action? Here are some awesome websites that I know of using this solution ❤️.
- the docs.docker.com website
- the UK Ministry of Justice Technical Guidance site
- "Minimal Mistakes" Jekyll Theme
- Apache's Beam and PDFBox websites
- JetBrains' OSS Jekyll Theme
- Bitcoin.org
- CloudCannon
- British Antarctic Survey
- Travis CI Docs
- the City of Amsterdam
- Intuit's Karate
- Duality's developer docs
- "Bulma Clean" Jekyll Theme
- The Google APIs team homepage, Google AIP website and API Linter project
- Shopify App CLI Extensions
- AWS' Open Distro for Elasticsearch Documentation
- GitHub's Open Source Guides website
- Gitbook Jekyll Theme
For more information regarding how this include works, read the blog post.
Want anchors next to your Jekyll headings without JavaScript or a plug-in?
Check out the sister project over at allejo/jekyll-anchor-headings.
Alright, so how do you use it?
-
Download the
toc.html
file from the latest release or the master branch -
Toss that file in your
_includes
folder. -
Use it in your template layout where you have
{{ content }}
which is the HTML rendered from the markdown source with this liquid tag:{% include toc.html html=content %}
This snippet is highly customizable. Here are the available parameters to change the behavior of the snippet.
Parameter | Type | Default | Description |
---|---|---|---|
html |
string | * | the HTML of compiled markdown generated by kramdown in Jekyll |
sanitize |
bool | false | when set to true, the headers will be stripped of any HTML in the TOC |
class |
string | '' | a CSS class assigned to the TOC; concat multiple classes with '.' |
id |
string | '' | an ID to be assigned to the TOC |
h_min |
int | 1 | the minimum TOC header level to use; any heading lower than this value will be ignored |
h_max |
int | 6 | the maximum TOC header level to use; any heading greater than this value will be ignored |
ordered |
bool | false | when set to true, an ordered list will be outputted instead of an unordered list |
item_class |
string | '' | add custom class for each list item; has support for %level% placeholder, which is the current heading level |
submenu_class |
string | '' | add custom class(es) for each child group of headings; has support for %level% placeholder which is the current "submenu" heading level |
base_url |
string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content |
anchor_class |
string | '' | add custom class(es) for each anchor element |
skip_no_ids |
bool | false | skip headers that do not have an id attribute |
flat_toc |
bool | false | when set to true, the TOC will be a single level list |
* This is a required parameter
baseurl
has been deprecated since 1.1.0, usebase_url
insteadskipNoIDs
has been deprecated since 1.1.0, useskip_no_ids
instead
This happens when your headings are placed out of order or when you skip headings. For example, if you go from an <h2>
to an <h4>
with no <h3>
in between; see "Heading 2.A" in this example,
## Heading 1
### Heading 1.A
## Heading 2
#### Heading 2.A
This project expects headings to be nested/ordered correctly, which is the recommended way of headings working.
Skipping heading ranks can be confusing and should be avoided where possible: Make sure that a
<h2>
is not followed directly by an<h4>
, for example. It is ok to skip ranks when closing subsections, for instance, a<h2>
beginning a new section, can follow an<h4>
as it closes the previous section.- w3.org
While it is possible to fix this and have the TOC internally keep track of indentation, I've chosen not to allow for this bad practice of allowing headings to be out of order. If you'd like to proceed anyways, see #13 for a third-party patch that can enable this functionality.
The performance impact of this snippet on your site is pretty negligible. The stats below were gotten from Jekyll's --profile
option.
Filename | Count | Bytes | Time
--------------------------------------+-------+------------+--------
# performance on docs.docker.com from ~Feb 2017
_includes/toc.html | 813 | 524.17K | 6.422
# performance on the "Minimal Mistakes" Jekyll theme
_includes/toc.html | 94 | 29.43K | 0.413
This snippet may be redistributed under either the BSD-3 or MIT licenses.