From c713144d1ba543cda14c2e6745be254594539e50 Mon Sep 17 00:00:00 2001 From: Samuel Wright Date: Wed, 27 Jun 2018 15:11:57 +0200 Subject: [PATCH 01/17] Switch to liquid toc --- _includes/head.html | 2 +- _includes/toc.html | 85 ++++++++++++++++++++++++++++++++++++++ _layouts/en.html | 7 ++++ assets/javascripts/site.js | 24 ----------- index.html | 1 + 5 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 _includes/toc.html delete mode 100644 assets/javascripts/site.js diff --git a/_includes/head.html b/_includes/head.html index b015d4dd33c..51fd30c83b4 100644 --- a/_includes/head.html +++ b/_includes/head.html @@ -4,7 +4,7 @@ - + diff --git a/_includes/toc.html b/_includes/toc.html new file mode 100644 index 00000000000..b9316a04b54 --- /dev/null +++ b/_includes/toc.html @@ -0,0 +1,85 @@ +{% capture tocWorkspace %} + {% comment %} + Version 1.0.4 + 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 for each list item; has support for '%level%' placeholder, which is the current heading level + + 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: ' maxHeader %} + {% continue %} + {% endif %} + + {% if firstHeader %} + {% assign firstHeader = false %} + {% assign minHeader = headerLevel %} + {% endif %} + + {% assign indentAmount = headerLevel | minus: minHeader | add: 1 %} + {% assign _workspace = node | split: '' | first }}>{% endcapture %} + {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} + + {% 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 }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% 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 | strip }} diff --git a/_layouts/en.html b/_layouts/en.html index 2f514a06ace..88ace145d2a 100644 --- a/_layouts/en.html +++ b/_layouts/en.html @@ -17,6 +17,13 @@

{{ page.title }}

{% endunless %} + + {% unless page.no_toc %} + + {% include toc.html class="list-languages" html=content %} + + {% endunless%} + {{ content }} diff --git a/assets/javascripts/site.js b/assets/javascripts/site.js deleted file mode 100644 index 210935336d0..00000000000 --- a/assets/javascripts/site.js +++ /dev/null @@ -1,24 +0,0 @@ -$(function(){ - var cname = function(name) { - return name.replace(/[ \/<>]/g, '-'); - } - - var list; - if ( $("#toc").length ) { - list = $("