toc.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. {% capture tocWorkspace %}
  2. {% comment %}
  3. Version 1.0.8
  4. https://github.com/allejo/jekyll-toc
  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. * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
  17. * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
  18. * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
  19. * anchor_class (string) : '' - add custom class(es) for each anchor element
  20. Output:
  21. An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
  22. generate the table of contents and will NOT output the markdown given to it
  23. {% endcomment %}
  24. {% capture my_toc %}{% endcapture %}
  25. {% assign orderedList = include.ordered | default: false %}
  26. {% assign minHeader = include.h_min | default: 1 %}
  27. {% assign maxHeader = include.h_max | default: 6 %}
  28. {% assign nodes = include.html | split: '<h' %}
  29. {% assign firstHeader = true %}
  30. {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
  31. {% for node in nodes %}
  32. {% if node == "" %}
  33. {% continue %}
  34. {% endif %}
  35. {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
  36. {% if headerLevel < minHeader or headerLevel > maxHeader %}
  37. {% continue %}
  38. {% endif %}
  39. {% if firstHeader %}
  40. {% assign firstHeader = false %}
  41. {% assign minHeader = headerLevel %}
  42. {% endif %}
  43. {% assign indentAmount = headerLevel | minus: minHeader %}
  44. {% assign _workspace = node | split: '</h' %}
  45. {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
  46. {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
  47. {% assign html_id = _idWorkspace[0] %}
  48. {% assign _classWorkspace = _workspace[0] | split: 'class="' %}
  49. {% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
  50. {% assign html_class = _classWorkspace[0] %}
  51. {% if html_class contains "no_toc" %}
  52. {% continue %}
  53. {% endif %}
  54. {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
  55. {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
  56. {% assign space = '' %}
  57. {% for i in (1..indentAmount) %}
  58. {% assign space = space | prepend: ' ' %}
  59. {% endfor %}
  60. {% unless include.item_class == blank %}
  61. {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
  62. {% endunless %}
  63. {% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
  64. {% capture my_toc %}{{ my_toc }}
  65. {{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
  66. {% endfor %}
  67. {% if include.class %}
  68. {% capture my_toc %}{:.{{ include.class }}}
  69. {{ my_toc | lstrip }}{% endcapture %}
  70. {% endif %}
  71. {% if include.id %}
  72. {% capture my_toc %}{: #{{ include.id }}}
  73. {{ my_toc | lstrip }}{% endcapture %}
  74. {% endif %}
  75. {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}