onthispage.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!-- Shamelessly copied from minimal mistakes -->
  2. {% capture tocWorkspace %}
  3. {%- comment -%}
  4. Version 1.0.2
  5. "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
  6. Usage:
  7. {% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
  8. Parameters:
  9. * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
  10. Optional Parameters:
  11. * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
  12. * class (string) : '' - a CSS class assigned to the TOC
  13. * id (string) : '' - an ID to assigned to the TOC
  14. * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
  15. * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
  16. Output:
  17. 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
  18. {%- endcomment -%}
  19. {% capture my_toc %}{% endcapture %}
  20. {% assign minHeader = include.h_min | default: 1 %}
  21. {% assign maxHeader = include.h_max | default: 6 %}
  22. {% assign nodes = include.html | split: '<h' %}
  23. {% assign firstHeader = true %}
  24. {% for node in nodes %}
  25. {% if node == "" %}
  26. {% continue %}
  27. {% endif %}
  28. {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
  29. {% if headerLevel < minHeader or headerLevel > maxHeader %}
  30. {% continue %}
  31. {% endif %}
  32. {% if firstHeader %}
  33. {% assign firstHeader = false %}
  34. {% assign minHeader = headerLevel %}
  35. {% endif %}
  36. {% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
  37. {% assign _workspace = node | split: '</h' %}
  38. {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
  39. {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
  40. {% assign html_id = _idWorkspace[0] %}
  41. {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
  42. {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
  43. {% assign space = '' %}
  44. {% for i in (1..indentAmount) %}
  45. {% assign space = space | prepend: ' ' %}
  46. {% endfor %}
  47. {% capture my_toc %}{{ my_toc }}
  48. {{ space }}- [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
  49. {% endfor %}
  50. {% if include.class %}
  51. {% capture my_toc %}{:.{{ include.class }}}
  52. {{ my_toc | lstrip }}{% endcapture %}
  53. {% endif %}
  54. {% if include.id %}
  55. {% capture my_toc %}{: #{{ include.id }}}
  56. {{ my_toc | lstrip }}{% endcapture %}
  57. {% endif %}
  58. {% endcapture %}{% assign tocWorkspace = '' %}
  59. <!-- TOC will only show up if it has at least one item -->
  60. {% assign toc_items = my_toc | split: '#' %}
  61. {% if toc_items.size > 1 %}
  62. <aside class="sidebar__right">
  63. <nav class="onthispage">
  64. <header><h4 class="nav__title"><i class="fa fa-list"></i> {{ page.toc_label | default: site.data.ui-text[site.locale].toc_label }}</h4></header>
  65. {{ my_toc | markdownify | strip }}
  66. </nav>
  67. </aside>
  68. {% endif %}