Blame _includes/toc.html

401d07
{% capture tocWorkspace %}
401d07
    {% comment %}
401d07
        Version 1.0.10
401d07
          https://github.com/allejo/jekyll-toc
401d07
401d07
        "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
401d07
401d07
        Usage:
401d07
            {% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
401d07
401d07
        Parameters:
401d07
            * html         (string) - the HTML of compiled markdown generated by kramdown in Jekyll
401d07
401d07
        Optional Parameters:
401d07
            * sanitize     (bool)   : false  - when set to true, the headers will be stripped of any HTML in the TOC
401d07
            * class        (string) :   ''   - a CSS class assigned to the TOC
401d07
            * id           (string) :   ''   - an ID to assigned to the TOC
401d07
            * h_min        (int)    :   1    - the minimum TOC header level to use; any header lower than this value will be ignored
401d07
            * h_max        (int)    :   6    - the maximum TOC header level to use; any header greater than this value will be ignored
401d07
            * ordered      (bool)   : false  - when set to true, an ordered list will be outputted instead of an unordered list
401d07
            * item_class   (string) :   ''   - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
401d07
            * baseurl      (string) :   ''   - add a base url to the TOC links for when your TOC is on another page than the actual content
401d07
            * anchor_class (string) :   ''   - add custom class(es) for each anchor element
401d07
401d07
        Output:
401d07
            An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
401d07
            generate the table of contents and will NOT output the markdown given to it
401d07
    {% endcomment %}
401d07
401d07
    {% capture my_toc %}{% endcapture %}
401d07
    {% assign orderedList = include.ordered | default: false %}
401d07
    {% assign minHeader = include.h_min | default: 1 %}
401d07
    {% assign maxHeader = include.h_max | default: 6 %}
401d07
    {% assign nodes = include.html | split: '
401d07
    {% assign firstHeader = true %}
401d07
401d07
    {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
401d07
401d07
    {% for node in nodes %}
401d07
        {% if node == "" %}
401d07
            {% continue %}
401d07
        {% endif %}
401d07
401d07
        {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
401d07
401d07
        {% if headerLevel < minHeader or headerLevel > maxHeader %}
401d07
            {% continue %}
401d07
        {% endif %}
401d07
401d07
        {% if firstHeader %}
401d07
            {% assign firstHeader = false %}
401d07
            {% assign minHeader = headerLevel %}
401d07
        {% endif %}
401d07
401d07
        {% assign indentAmount = headerLevel | minus: minHeader %}
401d07
        {% assign _workspace = node | split: '
401d07
401d07
        {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
401d07
        {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
401d07
        {% assign html_id = _idWorkspace[0] %}
401d07
401d07
        {% assign _classWorkspace = _workspace[0] | split: 'class="' %}
401d07
        {% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
401d07
        {% assign html_class = _classWorkspace[0] %}
401d07
401d07
        {% if html_class contains "no_toc" %}
401d07
            {% continue %}
401d07
        {% endif %}
401d07
401d07
        {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
401d07
        {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
401d07
401d07
        {% assign space = '' %}
401d07
        {% for i in (1..indentAmount) %}
401d07
            {% assign space = space | prepend: '    ' %}
401d07
        {% endfor %}
401d07
401d07
        {% if include.item_class and include.item_class != blank %}
401d07
            {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
401d07
        {% endif %}
401d07
401d07
        {% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
401d07
        {% capture my_toc %}{{ my_toc }}
401d07
{{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
401d07
    {% endfor %}
401d07
401d07
    {% if include.class and include.class != blank %}
401d07
        {% capture my_toc %}{:.{{ include.class }}}
401d07
{{ my_toc | lstrip }}{% endcapture %}
401d07
    {% endif %}
401d07
401d07
    {% if include.id %}
401d07
        {% capture my_toc %}{: #{{ include.id }}}
401d07
{{ my_toc | lstrip }}{% endcapture %}
401d07
    {% endif %}
401d07
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}