toc.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. {% capture tocWorkspace %}
  2. {% comment %}
  3. Copyright (c) 2017 Vladimir "allejo" Jimenez
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. {% endcomment %}
  23. {% comment %}
  24. Version 1.1.0
  25. https://github.com/allejo/jekyll-toc
  26. "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
  27. Usage:
  28. {% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
  29. Parameters:
  30. * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
  31. Optional Parameters:
  32. * sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
  33. * class (string) : '' - a CSS class assigned to the TOC
  34. * id (string) : '' - an ID to assigned to the TOC
  35. * h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
  36. * h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
  37. * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
  38. * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
  39. * submenu_class (string) : '' - add custom class(es) for each child group of headings; has support for '%level%' placeholder which is the current "submenu" heading level
  40. * base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
  41. * anchor_class (string) : '' - add custom class(es) for each anchor element
  42. * skip_no_ids (bool) : false - skip headers that do not have an `id` attribute
  43. Output:
  44. An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
  45. generate the table of contents and will NOT output the markdown given to it
  46. {% endcomment %}
  47. {% capture newline %}
  48. {% endcapture %}
  49. {% assign newline = newline | rstrip %} <!-- Remove the extra spacing but preserve the newline -->
  50. {% capture deprecation_warnings %}{% endcapture %}
  51. {% if include.baseurl %}
  52. {% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "baseurl" has been deprecated, use "base_url" instead -->{{ newline }}{% endcapture %}
  53. {% endif %}
  54. {% if include.skipNoIDs %}
  55. {% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %}
  56. {% endif %}
  57. {% capture jekyll_toc %}{% endcapture %}
  58. {% assign orderedList = include.ordered | default: false %}
  59. {% assign baseURL = include.base_url | default: include.baseurl | default: '' %}
  60. {% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %}
  61. {% assign minHeader = include.h_min | default: 1 %}
  62. {% assign maxHeader = include.h_max | default: 6 %}
  63. {% assign nodes = include.html | strip | split: '<h' %}
  64. {% assign firstHeader = true %}
  65. {% assign currLevel = 0 %}
  66. {% assign lastLevel = 0 %}
  67. {% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %}
  68. {% for node in nodes %}
  69. {% if node == "" %}
  70. {% continue %}
  71. {% endif %}
  72. {% assign currLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
  73. {% if currLevel < minHeader or currLevel > maxHeader %}
  74. {% continue %}
  75. {% endif %}
  76. {% assign _workspace = node | split: '</h' %}
  77. {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
  78. {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
  79. {% assign htmlID = _idWorkspace[0] %}
  80. {% assign _classWorkspace = _workspace[0] | split: 'class="' %}
  81. {% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
  82. {% assign htmlClass = _classWorkspace[0] %}
  83. {% if htmlClass contains "no_toc" %}
  84. {% continue %}
  85. {% endif %}
  86. {% if firstHeader %}
  87. {% assign minHeader = currLevel %}
  88. {% endif %}
  89. {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
  90. {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
  91. {% if include.item_class and include.item_class != blank %}
  92. {% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %}
  93. {% endif %}
  94. {% if include.submenu_class and include.submenu_class != blank %}
  95. {% assign subMenuLevel = currLevel | minus: 1 %}
  96. {% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %}
  97. {% endif %}
  98. {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
  99. {% if htmlID %}
  100. {% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %}
  101. {% if include.anchor_class %}
  102. {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %}
  103. {% endif %}
  104. {% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %}
  105. {% elsif skipNoIDs == true %}
  106. {% continue %}
  107. {% else %}
  108. {% capture listItem %}{{ anchorBody }}{% endcapture %}
  109. {% endif %}
  110. {% if currLevel > lastLevel %}
  111. {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %}
  112. {% elsif currLevel < lastLevel %}
  113. {% assign repeatCount = lastLevel | minus: currLevel %}
  114. {% for i in (1..repeatCount) %}
  115. {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
  116. {% endfor %}
  117. {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
  118. {% else %}
  119. {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
  120. {% endif %}
  121. {% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %}
  122. {% assign lastLevel = currLevel %}
  123. {% assign firstHeader = false %}
  124. {% endfor %}
  125. {% assign repeatCount = minHeader | minus: 1 %}
  126. {% assign repeatCount = lastLevel | minus: repeatCount %}
  127. {% for i in (1..repeatCount) %}
  128. {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
  129. {% endfor %}
  130. {% if jekyll_toc != '' %}
  131. {% assign rootAttributes = '' %}
  132. {% if include.class and include.class != blank %}
  133. {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %}
  134. {% endif %}
  135. {% if include.id and include.id != blank %}
  136. {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %}
  137. {% endif %}
  138. {% if rootAttributes %}
  139. {% assign nodes = jekyll_toc | split: '>' %}
  140. {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %}
  141. {% endif %}
  142. {% endif %}
  143. {% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }}