-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1474 from mstanleyjones/auto_toc_inpage2
POC 2 for in-page TOC
- Loading branch information
Showing
9 changed files
with
150 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{% capture tocWorkspace %} | ||
{% comment %} | ||
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe | ||
Usage: | ||
{% include toc_pure_liquid.html html=content sanitize=true class="inline_toc" id="my_toc" toc_min=2 toc_max=3 my_name="unnamed" %} | ||
|
||
Variables: | ||
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll | ||
* sanitize (bool) - when set to true, the headers will be sanitized in the TOC | ||
* class (string) - a CSS class assigned to the TOC | ||
* id (string) - an ID to assigned to the TOC | ||
* toc_min (int) - the minimum TOC header level to use (if not set, check page, then site, then default to 2) | ||
* toc_max (int) - the maximum TOC header level to use (if not set, check page, then site, then default to 3) | ||
* page_name (string) - the URL of the page | ||
|
||
Output: | ||
An 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 | ||
{% endcomment %} | ||
|
||
{% capture my_toc %}{% endcapture %} | ||
{% assign minHeader = include.toc_min | default: 2 %} | ||
{% assign maxHeader = include.toc_max | default: 3 %} | ||
{% assign my_name = include.page_name | default: "unnamed" %} | ||
{% assign nodes = include.html | split: '<h' %} | ||
|
||
{% for node in nodes %} | ||
{% if node == "" %} | ||
{% continue %} | ||
{% endif %} | ||
|
||
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 %} | ||
{% assign headerLevel = headerLevel | times: 1 %} | ||
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %} | ||
{% assign _workspace = node | split: '</h' %} | ||
|
||
{% unless headerLevel >= minHeader %} | ||
{% continue %} | ||
{% endunless %} | ||
|
||
{% if headerLevel > maxHeader %} | ||
{% continue %} | ||
{% endif %} | ||
|
||
{% assign _idWorkspace = _workspace[0] | split: '"' %} | ||
{% assign html_id = _idWorkspace[1] %} | ||
|
||
{% capture _hAttrToStrip %}{{ headerLevel }} id="{{ html_id }}">{% endcapture %} | ||
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} | ||
|
||
{% assign space = '' %} | ||
{% for i in (1..indentAmount) %} | ||
{% assign space = space | prepend: ' ' %} | ||
{% endfor %} | ||
|
||
{% capture my_toc %}{{ my_toc }} | ||
{{ space }}- [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}]({{ my_name }}#{{ html_id }}){: class="nomunge" }{% endcapture %} | ||
|
||
{% endfor %} | ||
|
||
{% if include.class %} | ||
{% capture my_toc %}{:.{{ include.class }}} | ||
{{ my_toc | lstrip }}{% endcapture %} | ||
{% endif %} | ||
|
||
{% if include.id %} | ||
{% capture my_toc %}{: #{{ include.id }}} | ||
{{ my_toc | lstrip }}{% endcapture %} | ||
{% endif %} | ||
{% endcapture %}{% assign tocWorkspace = '' %} | ||
{{ my_toc | markdownify }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters