|
| 1 | +{% capture tocWorkspace %} |
| 2 | + {% comment %} |
| 3 | + Version 1.0.4 |
| 4 | + https://github.com/allejo/jekyll-toc |
| 5 | + |
| 6 | + "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe |
| 7 | + |
| 8 | + Usage: |
| 9 | + {% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %} |
| 10 | + |
| 11 | + Parameters: |
| 12 | + * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll |
| 13 | + |
| 14 | + Optional Parameters: |
| 15 | + * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC |
| 16 | + * class (string) : '' - a CSS class assigned to the TOC |
| 17 | + * id (string) : '' - an ID to assigned to the TOC |
| 18 | + * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored |
| 19 | + * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored |
| 20 | + * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list |
| 21 | + * item_class (string) : '' - add custom class for each list item; has support for '%level%' placeholder, which is the current heading level |
| 22 | + |
| 23 | + Output: |
| 24 | + An ordered or 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 |
| 25 | + {% endcomment %} |
| 26 | + |
| 27 | + {% capture my_toc %}{% endcapture %} |
| 28 | + {% assign orderedList = include.ordered | default: false %} |
| 29 | + {% assign minHeader = include.h_min | default: 1 %} |
| 30 | + {% assign maxHeader = include.h_max | default: 6 %} |
| 31 | + {% assign nodes = include.html | split: '<h' %} |
| 32 | + {% assign firstHeader = true %} |
| 33 | + |
| 34 | + {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %} |
| 35 | + |
| 36 | + {% for node in nodes %} |
| 37 | + {% if node == "" %} |
| 38 | + {% continue %} |
| 39 | + {% endif %} |
| 40 | + |
| 41 | + {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %} |
| 42 | + |
| 43 | + {% if headerLevel < minHeader or headerLevel > maxHeader %} |
| 44 | + {% continue %} |
| 45 | + {% endif %} |
| 46 | + |
| 47 | + {% if firstHeader %} |
| 48 | + {% assign firstHeader = false %} |
| 49 | + {% assign minHeader = headerLevel %} |
| 50 | + {% endif %} |
| 51 | + |
| 52 | + {% assign indentAmount = headerLevel | minus: minHeader | add: 1 %} |
| 53 | + {% assign _workspace = node | split: '</h' %} |
| 54 | + |
| 55 | + {% assign _idWorkspace = _workspace[0] | split: 'id="' %} |
| 56 | + {% assign _idWorkspace = _idWorkspace[1] | split: '"' %} |
| 57 | + {% assign html_id = _idWorkspace[0] %} |
| 58 | + |
| 59 | + {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %} |
| 60 | + {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} |
| 61 | + |
| 62 | + {% assign space = '' %} |
| 63 | + {% for i in (1..indentAmount) %} |
| 64 | + {% assign space = space | prepend: ' ' %} |
| 65 | + {% endfor %} |
| 66 | + |
| 67 | + {% unless include.item_class == blank %} |
| 68 | + {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %} |
| 69 | + {% endunless %} |
| 70 | + |
| 71 | + {% capture my_toc %}{{ my_toc }} |
| 72 | +{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %} |
| 73 | + |
| 74 | + {% endfor %} |
| 75 | + |
| 76 | + {% if include.class %} |
| 77 | + {% capture my_toc %}{:.{{ include.class }}} |
| 78 | +{{ my_toc | lstrip }}{% endcapture %} |
| 79 | + {% endif %} |
| 80 | + |
| 81 | + {% if include.id %} |
| 82 | + {% capture my_toc %}{: #{{ include.id }}} |
| 83 | +{{ my_toc | lstrip }}{% endcapture %} |
| 84 | + {% endif %} |
| 85 | +{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} |
0 commit comments