{% comment %} Adapted from: github.com/allejo/jekyll-toc Required Parameters:

* folder_index (int)    :   ''   - the path of the files for the toc to be generated
* folder_name  (int)    :   ''   - the path of the files for the toc to be generated

Optional Parameters:

* 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
* class        (string) :   ''   - class to be applied to root toc
* item_class   (string) :   ''   - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
* anchor_class (string) :   ''   - add custom class(es) for each anchor element

{% endcomment %}

{%- comment -%} Get every page in the site and traverse them {%- endcomment -%} {% assign folder_pages = “” | split: ',' %} {% assign site_pages = site.html_pages %} {% for site_page in site_pages %}

{% if site_page.name == 'index.md' or site_page.name == 'index.html' %}
    {% continue %}
{% endif %}
{%- comment -%} 
If folder_name is not passed in, assign all pages to this TOC
{%- endcomment -%}
{% if include.folder_name == null %}
    {% assign folder_pages = folder_pages | push: site_page %}
{%- comment -%} 
else, find all pages beloing to this folder name
{%- endcomment -%}
{% else %}
    {% assign path_split = site_page.path | split: '/' %}
    {% if path_split.size == 1 %}
        {% assign site_page_folder = '/' %}
    {% else %}
        {% assign site_page_folder = path_split | first %}
    {% endif %}
    {%- comment -%} 
    if site_page_folder corresponds to folder_name passed in
    and path_split.size is love 
    {%- endcomment -%}
    {% if site_page_folder == include.folder_name and path_split.size < 3 %}
        {% assign folder_pages = folder_pages | push: site_page %}
    {% endif %}        
{% endif %}

{% endfor %}

{%- comment -%} Then, if order is present, match order by filename or title {%- endcomment -%} {% assign pages = “” | split: ',' %} {% assign page_order = site.folders.order %} {% if page_order and page_order.size > 0 %}

{% for order in page_order %}
    {% for folder_page in folder_pages %}
        {% if folder_page.name == order or folder_page.title == order %}
            {% assign pages = pages | push: folder_page %}
            {% break %}
        {% endif %}
    {% endfor %}
{% endfor %}

{% else %} {%- comment -%} Else, sort alphabetically {%- endcomment -%}

{% assign pages = folder_pages | sort: 'title' %}

{% endif %}

{% capture tocWorkspace %}

{% capture my_toc %}{% endcapture %} 
{% assign minHeader = include.h_min | default: 1 %}
{% assign maxHeader = include.h_max | default: 6 %}
{% assign firstHeader = true %}
{% for page in pages %}
    {% assign nodes = page.content | markdownify | split: '<h' %}
    {% for node in nodes %}
        {% if node == "" %}
            {% continue %}
        {% endif %}
        {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
        {% if headerLevel < minHeader or headerLevel > maxHeader %}
            {% continue %}
        {% endif %}
        {% if firstHeader %}
            {% assign firstHeader = false %}
            {% assign minHeader = headerLevel %}
        {% endif %}
        {% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
        {% assign _workspace = node | split: '</h' %}
        {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
        {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
        {% assign html_id = _idWorkspace[0] %}
        {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
        {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
        {% comment %}
            Strips out footnotes links then strips out all other html tags
            This naively assumes that all footnote links are appended at the end of the header
        {% endcomment %}
        {% assign header = header | split: '<sup' | first %}
        {% assign header = header | strip_html | strip %}
        {% assign space = '' %}
        {% for i in (1..indentAmount) %}
            {% assign space = space | prepend: '    ' %}
        {% endfor %}
        {% unless include.item_class == blank %}
            {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
        {% endunless %}
        {% capture my_toc %}{{ my_toc }}

{{ space }} - {{ listItemClass }}<a href=“{% if page.url %}{{ page.url | relative_url }}{% endif %}{% if headerLevel > minHeader %}#{{ html_id }}{% endif %}” class=“{% if include.anchor_class %}{{ include.anchor_class }}{% endif %}”><span class=“directory-item”> {{ header }} </span></a>{% endcapture %}

    {% endfor %}
{% endfor %}

{% if include.class %} {% capture my_toc %}{:.{{ include.class }}} {{ my_toc | lstrip }}{% endcapture %} {% endif %} {% endcapture %} {% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip | strip_newlines }}