diff options
| author | Yehonal <yehonal.azeroth@gmail.com> | 2020-01-11 23:45:58 +0100 |
|---|---|---|
| committer | Yehonal <yehonal.azeroth@gmail.com> | 2020-01-11 23:45:58 +0100 |
| commit | a81a933ab806dabbc6720d98f470c6a5edcd9a15 (patch) | |
| tree | 6340a4ced8b8517e567c8a02746c8673caf5717f | |
| parent | 2b94ffd1e14066696f3cb7e7286a5e2425561df8 (diff) | |
| download | wiki-a81a933ab806dabbc6720d98f470c6a5edcd9a15.tar.gz wiki-a81a933ab806dabbc6720d98f470c6a5edcd9a15.tar.bz2 wiki-a81a933ab806dabbc6720d98f470c6a5edcd9a15.zip | |
implemented static TOC #57
Thanks to @vzickus for the suggestion
| -rw-r--r-- | _includes/git-wiki/components/toc/toc-lib.html | 96 | ||||
| -rw-r--r-- | _includes/git-wiki/components/toc/toc.html | 12 | ||||
| -rw-r--r-- | _includes/git-wiki/sections/content/content.html | 10 | ||||
| -rw-r--r-- | _includes/git-wiki/sections/head/scripts.html | 1 | ||||
| -rw-r--r-- | _includes/git-wiki/sections/tail/tail.html | 11 | ||||
| -rw-r--r-- | _layouts/git-wiki-bs-github.html | 10 | ||||
| -rw-r--r-- | _layouts/git-wiki-bs-lux.html | 10 | ||||
| -rw-r--r-- | _layouts/git-wiki-bs-united.html | 10 | ||||
| -rw-r--r-- | assets/js/toc.js | 120 |
9 files changed, 112 insertions, 168 deletions
diff --git a/_includes/git-wiki/components/toc/toc-lib.html b/_includes/git-wiki/components/toc/toc-lib.html new file mode 100644 index 0000000..4aa9c88 --- /dev/null +++ b/_includes/git-wiki/components/toc/toc-lib.html @@ -0,0 +1,96 @@ +{% capture tocWorkspace %} + {% comment %} + Version 1.0.9 + 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: + * 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 + + Output: + 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 + {% endcomment %} + + {% capture my_toc %}{% endcapture %} + {% assign orderedList = include.ordered | default: false %} + {% assign minHeader = include.h_min | default: 1 %} + {% assign maxHeader = include.h_max | default: 6 %} + {% assign nodes = include.html | split: '<h' %} + {% assign firstHeader = true %} + + {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %} + + {% 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 %} + {% assign _workspace = node | split: '</h' %} + + {% assign _idWorkspace = _workspace[0] | split: 'id="' %} + {% assign _idWorkspace = _idWorkspace[1] | split: '"' %} + {% assign html_id = _idWorkspace[0] %} + + {% assign _classWorkspace = _workspace[0] | split: 'class="' %} + {% assign _classWorkspace = _classWorkspace[1] | split: '"' %} + {% assign html_class = _classWorkspace[0] %} + + {% if html_class contains "no_toc" %} + {% continue %} + {% endif %} + + {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %} + {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} + + {% assign space = '' %} + {% for i in (1..indentAmount) %} + {% assign space = space | prepend: ' ' %} + {% endfor %} + + {% if include.item_class and include.item_class != blank %} + {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %} + {% endif %} + + {% 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 %} + {% endfor %} + + {% if include.class and include.item_class != blank %} + {% 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 | strip }}
\ No newline at end of file diff --git a/_includes/git-wiki/components/toc/toc.html b/_includes/git-wiki/components/toc/toc.html new file mode 100644 index 0000000..c354b42 --- /dev/null +++ b/_includes/git-wiki/components/toc/toc.html @@ -0,0 +1,12 @@ +{% if site.inc_before_toc %} +{% include {{ site.inc_before_toc }} %} +{% endif %} + +<div> + <p>Contents:</p> + {% include git-wiki/components/toc/toc-lib.html html=content sanitize=true class="inline_toc" id="git-wiki-toc" h_min=1 h_max=3 ordered=1 %} +</div> + +{% if site.inc_after_toc %} +{% include {{ site.inc_after_toc }} %} +{% endif %} diff --git a/_includes/git-wiki/sections/content/content.html b/_includes/git-wiki/sections/content/content.html index 73c7b5b..b356968 100644 --- a/_includes/git-wiki/sections/content/content.html +++ b/_includes/git-wiki/sections/content/content.html @@ -3,15 +3,7 @@ {% include git-wiki/sections/tools/tools.html %} - {% if site.inc_before_toc %} - {% include {{ site.inc_before_toc }} %} - {% endif %} - - <div id="git-wiki-toc"></div> - - {% if site.inc_after_toc %} - {% include {{ site.inc_after_toc }} %} - {% endif %} + {% include git-wiki/components/toc/toc.html %} {% if site.inc_before_content %} {% include {{ site.inc_before_content }} %} diff --git a/_includes/git-wiki/sections/head/scripts.html b/_includes/git-wiki/sections/head/scripts.html index 65ee4ff..9cf23f3 100644 --- a/_includes/git-wiki/sections/head/scripts.html +++ b/_includes/git-wiki/sections/head/scripts.html @@ -4,7 +4,6 @@ <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> -<script src="{{ '/assets/js/toc.js' | relative_url }}"></script> <script src="{{ '/assets/js/red-links.js' | relative_url }}"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> diff --git a/_includes/git-wiki/sections/tail/tail.html b/_includes/git-wiki/sections/tail/tail.html index 6d32583..692867f 100644 --- a/_includes/git-wiki/sections/tail/tail.html +++ b/_includes/git-wiki/sections/tail/tail.html @@ -18,17 +18,6 @@ </script> {% endif %} -<script type="text/javascript"> - $(document).ready(function () { - $('#git-wiki-toc').toc({ - title: '<i>Contents</i>', - showSpeed: 0, - headers: '#git-wiki-content h1, #git-wiki-content h2, #git-wiki-content h3, #git-wiki-content h4, #git-wiki-content h5, #git-wiki-content h6' - }); - $(document.body).redLinks(); - }); -</script> - {% if site.inc_after_tail %} {% include {{ site.inc_after_tail }} %} {% endif %} diff --git a/_layouts/git-wiki-bs-github.html b/_layouts/git-wiki-bs-github.html index 8237c4b..b2b0788 100644 --- a/_layouts/git-wiki-bs-github.html +++ b/_layouts/git-wiki-bs-github.html @@ -33,15 +33,7 @@ <div class="git-wiki-page"> {% include git-wiki/sections/tools/tools.html %} - {% if site.inc_before_toc %} - {% include {{ site.inc_before_toc }} %} - {% endif %} - - <div id="git-wiki-toc"></div> - - {% if site.inc_after_toc %} - {% include {{ site.inc_after_toc }} %} - {% endif %} + {% include git-wiki/components/toc/toc.html %} {% if site.inc_before_content %} {% include {{ site.inc_before_content }} %} diff --git a/_layouts/git-wiki-bs-lux.html b/_layouts/git-wiki-bs-lux.html index 04085e4..5cc4a27 100644 --- a/_layouts/git-wiki-bs-lux.html +++ b/_layouts/git-wiki-bs-lux.html @@ -46,15 +46,7 @@ <div class="git-wiki-page"> {% include git-wiki/sections/tools/tools.html %} - {% if site.inc_before_toc %} - {% include {{ site.inc_before_toc }} %} - {% endif %} - - <div id="git-wiki-toc"></div> - - {% if site.inc_after_toc %} - {% include {{ site.inc_after_toc }} %} - {% endif %} + {% include git-wiki/components/toc/toc.html %} {% if site.inc_before_content %} {% include {{ site.inc_before_content }} %} diff --git a/_layouts/git-wiki-bs-united.html b/_layouts/git-wiki-bs-united.html index 2130a9a..556bcd3 100644 --- a/_layouts/git-wiki-bs-united.html +++ b/_layouts/git-wiki-bs-united.html @@ -32,15 +32,7 @@ <div class="git-wiki-page"> {% include git-wiki/sections/tools/tools.html %} - {% if site.inc_before_toc %} - {% include {{ site.inc_before_toc }} %} - {% endif %} - - <div id="git-wiki-toc"></div> - - {% if site.inc_after_toc %} - {% include {{ site.inc_after_toc }} %} - {% endif %} + {% include git-wiki/components/toc/toc.html %} {% if site.inc_before_content %} {% include {{ site.inc_before_content }} %} diff --git a/assets/js/toc.js b/assets/js/toc.js deleted file mode 100644 index bf8fbec..0000000 --- a/assets/js/toc.js +++ /dev/null @@ -1,120 +0,0 @@ -// https://github.com/ghiculescu/jekyll-table-of-contents -(function ($) { - $.fn.toc = function (options) { - var element = $(this); - - setTimeout(function () { - var defaults = { - noBackToTopLinks: false, - title: '<i>Jump to...</i>', - minimumHeaders: 3, - headers: 'h1, h2, h3, h4, h5, h6', - listType: 'ol', // values: [ol|ul] - showEffect: 'show', // values: [show|slideDown|fadeIn|none] - showSpeed: 'slow', // set to 0 to deactivate effect - classes: { - list: '', - item: '' - } - }, - settings = $.extend(defaults, options); - - function fixedEncodeURIComponent(str) { - return encodeURIComponent(str).replace(/[!'()*]/g, function (c) { - return '%' + c.charCodeAt(0).toString(16); - }); - } - - function createLink(header) { - var innerText = (header.textContent === undefined) ? header.innerText : header.textContent; - return "<a href='#" + fixedEncodeURIComponent(header.id) + "'>" + innerText + "</a>"; - } - - var headers = $(settings.headers).filter(function () { - // get all headers with an ID - var previousSiblingName = element.prev().attr("name"); - if (!this.id && previousSiblingName) { - this.id = element.attr("id", previousSiblingName.replace(/\./g, "-")); - } - - // Yehonal - if (!this.id) { - this.id = element.text().replace(/\W/g, '_'); - } - - return this.id; - }); - if (!headers.length || headers.length < settings.minimumHeaders || !element.length) { - element.hide(); - return; - } - - if (0 === settings.showSpeed) { - settings.showEffect = 'none'; - } - - var get_level = function (ele) { - return parseInt(ele.nodeName.replace("H", ""), 10); - }; - var highest_level = headers.map(function (_, ele) { - return get_level(ele); - }).get().sort()[0]; - var return_to_top = '<i class="icon-arrow-up back-to-top"> </i>'; - - var level = get_level(headers[0]), - this_level, - html = settings.title + " <" + settings.listType + " class=\"" + settings.classes.list + "\">"; - headers.on('click', function () { - if (!settings.noBackToTopLinks) { - window.location.hash = this.id; - } - }) - .addClass('clickable-header') - .each(function (_, header) { - this_level = get_level(header); - if (!settings.noBackToTopLinks && this_level === highest_level) { - $(header).addClass('top-level-header').after(return_to_top); - } - if (this_level === level) // same level as before; same indenting - html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); - else if (this_level <= level) { // higher level than before; end parent ol - for (var i = this_level; i < level; i++) { - html += "</li></" + settings.listType + ">" - } - html += "<li class=\"" + settings.classes.item + "\">" + createLink(header); - } else if (this_level > level) { // lower level than before; expand the previous to contain a ol - for (i = this_level; i > level; i--) { - html += "<" + settings.listType + " class=\"" + settings.classes.list + "\">" + - "<li class=\"" + settings.classes.item + "\">" - } - html += createLink(header); - } - level = this_level; // update for the next one - }); - html += "</" + settings.listType + ">"; - if (!settings.noBackToTopLinks) { - $(document).on('click', '.back-to-top', function () { - $(window).scrollTop(0); - window.location.hash = ''; - }); - } - - var render = { - show: function () { - element.hide().html(html).show(settings.showSpeed); - }, - slideDown: function () { - element.hide().html(html).slideDown(settings.showSpeed); - }, - fadeIn: function () { - element.hide().html(html).fadeIn(settings.showSpeed); - }, - none: function () { - element.html(html).show(); - } - }; - - render[settings.showEffect](); - }, 0); - } -})(jQuery); |
