Skip to content

Commit

Permalink
Merge pull request #7 from ministryofjustice/fix-table-of-contents-li…
Browse files Browse the repository at this point in the history
…quid-rendering

Use a more robust table of contents renderer
  • Loading branch information
SteveMarshall authored Aug 31, 2017
2 parents fa26b85 + e51091f commit 17d0458
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
6 changes: 6 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ Ministry of Justice's technical guidance site incorporates code from
Copyright (c) 2016 Crown Copyright (Government Digital Service).
GDS's Tech Docs Template is distributed under the terms of the MIT
License

Ministry of Justice's technical guidance site incorporates [@allejo's
Pure Liquid Jekyll Table of Contents](https://allejo.io/blog/a-jekyll-toc-in-liquid-only/),
Copyright (c) 2017 Vladimir Jimenez.
Pure Liquid Jekyll Table of Contents is distributed under the terms of
the BSD-3 License
62 changes: 62 additions & 0 deletions _includes/toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% capture tocWorkspace %}
{% comment %}
[This code is taken from https://allejo.io/blog/a-jekyll-toc-in-liquid-only/, and used with permissions under the BSD-3 licence]

"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe

Usage:
{% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}

Parameters:
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll

Optional Parameters:
* 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
* id (string) : '' - an ID to assigned to the TOC
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored

Output:
An unordered list representing the table of contents of a markdown block. This snippet will only generate the table of contents and will NOT output the markdown given to it
{% endcomment %}

{% capture my_toc %}{% endcapture %}
{% assign minHeader = include.h_min | default: 1 %}
{% assign maxHeader = include.h_max | default: 6 %}
{% assign nodes = include.html | split: '<h' %}
{% for node in nodes %}
{% if node == "" %}
{% continue %}
{% endif %}
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 %}
{% assign headerLevel = headerLevel | times: 1 %}
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
{% assign _workspace = node | split: '</h' %}
{% unless headerLevel >= minHeader %}
{% continue %}
{% endunless %}
{% if headerLevel > maxHeader %}
{% continue %}
{% endif %}
{% assign _idWorkspace = _workspace[0] | split: '"' %}
{% assign html_id = _idWorkspace[1] %}
{% capture _hAttrToStrip %}{{ headerLevel }} id="{{ html_id }}">{% endcapture %}
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
{% assign space = '' %}
{% for i in (1..indentAmount) %}
{% assign space = space | prepend: ' ' %}
{% endfor %}
{% capture my_toc %}{{ my_toc }}
{{ space }}- [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
{% endfor %}
{% if include.class %}
{% capture my_toc %}{:.{{ include.class }}}
{{ my_toc | lstrip }}{% endcapture %}
{% endif %}
{% if include.id %}
{% capture my_toc %}{: #{{ include.id }}}
{{ my_toc | lstrip }}{% endcapture %}
{% endif %}
{% endcapture %}{% assign tocWorkspace = '' %}
{{ my_toc | markdownify }}
9 changes: 1 addition & 8 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@
<div class="toc" data-module="table-of-contents">
<a href="#" class="toc__close js-toc-close" aria-controls="toc" aria-label="Hide table of contents"></a>
<nav id="toc" class="js-toc-list toc__list" aria-labelledby="toc-heading">
{% capture toc_markdown %}
{{ page.content }}
<!-- TOC-start-marker -->
* auto-gen TOC:
{:toc}
{% endcapture %}
{% assign rendered_markdown_with_toc = toc_markdown | markdownify | split: '<!-- TOC-start-marker -->' %}
{{ rendered_markdown_with_toc[1] }}
{% include toc.html html=content %}
</nav>
</div>
</div>
Expand Down
2 changes: 0 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ which covers service design more broadly.
{% for standard_group in standards %}
{% if standard_group.name != "" %}
### {{ standard_group.name }}
{:.no_toc}
{% else %}
### General standards
{:.no_toc}
{% endif %}

{% for standard in standard_group.items %}
Expand Down

0 comments on commit 17d0458

Please sign in to comment.