Home

Awesome

Jekyll Pure Liquid Heading Anchors

Unit Tests Latest release ko-fi Buy me a coffee

GitHub Pages can't run custom Jekyll plug-ins so when generating anchors for your headings (i.e. h1 - h6), you're stuck with JavaScript solutions that will inject anchors. But what if your users don't have JavaScript enabled on their browsers? If you're building a static website, why not make your anchors static as well?

<p align="center"> <img src=".github/assets/screenshot.png" alt="Sample anchor next to heading"> </p>

As a part of my "Pure Liquid" series of Jekyll snippets, here is a Liquid snippet that will modify your generated HTML to inject anchors. Want to see it in action? Here are some awesome websites that I know of using this solution :heart:.

Want a Table of Contents in your Jekyll layouts without JavaScript or a plug-in?

Check out the sister project over at allejo/jekyll-toc.

Usage

Alright, so how do you use it?

  1. Download the anchor_headings.html file from the latest release or the master branch

  2. Toss that file in your _includes folder

  3. Where you typically would put {{ content }} in your layout, you would instead use this Liquid tag to output your page's content:

    {% include anchor_headings.html html=content anchorBody="#" %}
    

:wheelchair: Link Accessibility

Accessibility is hard and there are many different ways someone may want to handle it. For that reason, this project does not force onto developers any method of handling accessibility. That being said, this project can be configured to be accessible as seen in examples in the wiki.

:warning: Layout Inheritance

If this snippet is used in a layout that is inherited by a child layout, it will apply to the child layout as well. If the child layout uses this snippet in addition to the parent layout, then heading anchors will be duplicated. You should only use this snippet in one layout.

:art: Customization

You may remove anchorBody and add an icon via CSS' content property instead. You may also use HTML in anchorBody and add screen reader friendly markup.

Take a look at the unit tests directory and the wiki page for more examples of how to use this script.

Parameters

This snippet is highly customizable. Here are the available parameters to change the behavior of the snippet.

ParameterTypeDefaultDescription
htmlstring<sup>*</sup>the HTML of compiled markdown generated by kramdown in Jekyll
beforeHeadingboolfalseSet to true if the anchor should be placed before the heading's content
headerAttrsstring''Any custom HTML attributes that will be added to the heading tag; you may NOT use id; the %heading% and %html_id% placeholders are available
anchorAttrsstring''Any custom HTML attributes that will be added to the <a> tag; you may NOT use href, class or title; the %heading% and %html_id% placeholders are available
anchorBodystring''The content that will be placed inside the anchor; the %heading% placeholder is available
anchorClassstring''The class(es) that will be used for each anchor. Separate multiple classes with a space
anchorTitlestring''The title attribute that will be used for anchors; the %heading% placeholder is available
h_minint1The minimum header level to build an anchor for; any header lower than this value will be ignored
h_maxint6The maximum header level to build an anchor for; any header greater than this value will be ignored
bodyPrefixstring''Anything that should be inserted inside of the heading tag before its anchor and content
bodySuffixstring''Anything that should be inserted inside of the heading tag after its anchor and content
generateIdboolfalseSet to true if a header without id should generate an id to use.

<sup>*</sup> This is a required parameter

Performance

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.travis-ci.com from ~Aug 2018
_includes/anchor_headings.html                   |   193 | 1667.96K | 0.695

License

This snippet may be redistributed under the MIT license.