decentralized-id.github.io/_includes/toc.html

183 lines
8.7 KiB
HTML
Raw Permalink Normal View History

2019-03-28 19:40:33 -04:00
{% capture tocWorkspace %}
{% comment %}
2020-10-28 20:56:49 -04:00
Copyright (c) 2017 Vladimir "allejo" Jimenez
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
{% endcomment %}
{% comment %}
2023-02-10 04:44:06 -05:00
Version 1.1.0
2019-03-28 19:40:33 -04:00
https://github.com/allejo/jekyll-toc
"...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:
2020-10-28 20:56:49 -04:00
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
2019-03-28 19:40:33 -04:00
Optional Parameters:
2020-10-28 20:56:49 -04:00
* 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
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
* 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
2023-02-10 04:44:06 -05:00
* base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
2020-10-28 20:56:49 -04:00
* anchor_class (string) : '' - add custom class(es) for each anchor element
2023-02-10 04:44:06 -05:00
* skip_no_ids (bool) : false - skip headers that do not have an `id` attribute
2019-03-28 19:40:33 -04:00
Output:
2020-10-28 20:56:49 -04:00
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
2019-03-28 19:40:33 -04:00
{% endcomment %}
2023-02-10 04:44:06 -05:00
{% capture newline %}
{% endcapture %}
{% assign newline = newline | rstrip %} <!-- Remove the extra spacing but preserve the newline -->
{% capture deprecation_warnings %}{% endcapture %}
{% if include.baseurl %}
{% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "baseurl" has been deprecated, use "base_url" instead -->{{ newline }}{% endcapture %}
{% endif %}
{% if include.skipNoIDs %}
{% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %}
{% endif %}
{% capture jekyll_toc %}{% endcapture %}
2019-03-28 19:40:33 -04:00
{% assign orderedList = include.ordered | default: false %}
2023-02-10 04:44:06 -05:00
{% assign baseURL = include.base_url | default: include.baseurl | default: '' %}
{% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %}
2019-03-28 19:40:33 -04:00
{% assign minHeader = include.h_min | default: 1 %}
{% assign maxHeader = include.h_max | default: 6 %}
2023-02-10 04:44:06 -05:00
{% assign nodes = include.html | strip | split: '<h' %}
2019-03-28 19:40:33 -04:00
{% assign firstHeader = true %}
2023-02-10 04:44:06 -05:00
{% assign currLevel = 0 %}
{% assign lastLevel = 0 %}
2019-03-28 19:40:33 -04:00
2023-02-10 04:44:06 -05:00
{% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %}
2019-03-28 19:40:33 -04:00
{% for node in nodes %}
{% if node == "" %}
{% continue %}
{% endif %}
2023-02-10 04:44:06 -05:00
{% assign currLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
2019-03-28 19:40:33 -04:00
2023-02-10 04:44:06 -05:00
{% if currLevel < minHeader or currLevel > maxHeader %}
2019-03-28 19:40:33 -04:00
{% continue %}
{% endif %}
{% assign _workspace = node | split: '</h' %}
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
2023-02-10 04:44:06 -05:00
{% assign htmlID = _idWorkspace[0] %}
2019-03-28 19:40:33 -04:00
2020-10-28 20:56:49 -04:00
{% assign _classWorkspace = _workspace[0] | split: 'class="' %}
{% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
2023-02-10 04:44:06 -05:00
{% assign htmlClass = _classWorkspace[0] %}
2020-10-28 20:56:49 -04:00
2023-02-10 04:44:06 -05:00
{% if htmlClass contains "no_toc" %}
2020-10-28 20:56:49 -04:00
{% continue %}
{% endif %}
{% if firstHeader %}
2023-02-10 04:44:06 -05:00
{% assign minHeader = currLevel %}
2020-10-28 20:56:49 -04:00
{% endif %}
2019-03-28 19:40:33 -04:00
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
2020-10-28 20:56:49 -04:00
{% if include.item_class and include.item_class != blank %}
2023-02-10 04:44:06 -05:00
{% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %}
{% endif %}
{% if include.submenu_class and include.submenu_class != blank %}
{% assign subMenuLevel = currLevel | minus: 1 %}
{% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %}
2020-10-28 20:56:49 -04:00
{% endif %}
2023-02-10 04:44:06 -05:00
{% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
{% if htmlID %}
{% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %}
{% if include.anchor_class %}
{% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %}
{% endif %}
2020-10-28 20:56:49 -04:00
2023-02-10 04:44:06 -05:00
{% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %}
2020-10-28 20:56:49 -04:00
{% elsif skipNoIDs == true %}
{% continue %}
{% else %}
2023-02-10 04:44:06 -05:00
{% capture listItem %}{{ anchorBody }}{% endcapture %}
2020-10-28 20:56:49 -04:00
{% endif %}
2023-02-10 04:44:06 -05:00
{% if currLevel > lastLevel %}
{% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %}
{% elsif currLevel < lastLevel %}
{% assign repeatCount = lastLevel | minus: currLevel %}
{% for i in (1..repeatCount) %}
{% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
{% endfor %}
{% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
{% else %}
{% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %}
2020-10-28 20:56:49 -04:00
{% endif %}
2019-03-28 19:40:33 -04:00
2023-02-10 04:44:06 -05:00
{% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %}
2020-10-28 20:56:49 -04:00
2023-02-10 04:44:06 -05:00
{% assign lastLevel = currLevel %}
{% assign firstHeader = false %}
2019-03-28 19:40:33 -04:00
{% endfor %}
2023-02-10 04:44:06 -05:00
{% assign repeatCount = minHeader | minus: 1 %}
{% assign repeatCount = lastLevel | minus: repeatCount %}
{% for i in (1..repeatCount) %}
{% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %}
{% endfor %}
2019-03-28 19:40:33 -04:00
2023-02-10 04:44:06 -05:00
{% if jekyll_toc != '' %}
{% assign rootAttributes = '' %}
{% if include.class and include.class != blank %}
{% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %}
{% endif %}
2020-10-28 20:56:49 -04:00
2023-02-10 04:44:06 -05:00
{% if include.id and include.id != blank %}
{% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %}
{% endif %}
2020-10-28 20:56:49 -04:00
2023-02-10 04:44:06 -05:00
{% if rootAttributes %}
{% assign nodes = jekyll_toc | split: '>' %}
{% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %}
{% endif %}
2020-10-28 20:56:49 -04:00
{% endif %}
2023-02-10 04:44:06 -05:00
{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }}