From a66783cef7766aea9137c09f753563b7a29cabe9 Mon Sep 17 00:00:00 2001 From: Yehonal Date: Sun, 12 Jan 2020 19:20:51 +0100 Subject: Improved TOC Added title and minHeaders parameters --- _includes/git-wiki/components/toc/toc-lib.html | 33 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to '_includes/git-wiki/components/toc/toc-lib.html') diff --git a/_includes/git-wiki/components/toc/toc-lib.html b/_includes/git-wiki/components/toc/toc-lib.html index 4aa9c88..8851400 100644 --- a/_includes/git-wiki/components/toc/toc-lib.html +++ b/_includes/git-wiki/components/toc/toc-lib.html @@ -12,15 +12,17 @@ * html (string) - the HTML of compiled markdown generated by kramdown in Jekyll Optional Parameters: - * 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 - * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content - * anchor_class (string) : '' - add custom class(es) for each anchor element + * title (string) : Contents: - title for the TOC + * minHeaders (int) : 1 - minimum number of headers required to show the TOC + * 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 + * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content + * anchor_class (string) : '' - add custom class(es) for each anchor element Output: An ordered or unordered list representing the table of contents of a markdown block. This snippet will only @@ -28,6 +30,8 @@ {% endcomment %} {% capture my_toc %}{% endcapture %} + {% assign title = include.title | default: "Contents:" %} + {% assign minHeaders = include.minHeaders | default: 1 %} {% assign orderedList = include.ordered | default: false %} {% assign minHeader = include.h_min | default: 1 %} {% assign maxHeader = include.h_max | default: 6 %} @@ -36,6 +40,8 @@ {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %} + {% assign hCount = 0 %} + {% for node in nodes %} {% if node == "" %} {% continue %} @@ -79,6 +85,8 @@ {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %} {% endif %} + {% assign hCount = hCount | plus: 1 %} + {% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} {% capture my_toc %}{{ my_toc }} {{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} @@ -93,4 +101,9 @@ {% capture my_toc %}{: #{{ include.id }}} {{ my_toc | lstrip }}{% endcapture %} {% endif %} -{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} \ No newline at end of file +{% endcapture %}{% assign tocWorkspace = '' %} + +{% if hCount >= minHeaders %} + {{ title }} + {{ my_toc | markdownify | strip }} +{% endif %} \ No newline at end of file -- cgit