From f43aff6826f2627f54ac3a2f155636163b2eb2cb Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Thu, 24 Sep 2020 08:46:25 -0700 Subject: [PATCH 01/15] Introduce `new_line` variable to fix weird indents --- _includes/toc.html | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index e6c1cb4..32420de 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -53,6 +53,10 @@ generate the table of contents and will NOT output the markdown given to it {% endcomment %} + +{% capture new_line %} +{% endcapture %} + {% capture my_toc %}{% endcapture %} {% assign orderedList = include.ordered | default: false %} {% assign skipNoIDs = include.skipNoIDs | default: false %} @@ -129,24 +133,20 @@ --> {% assign submenuIndentation = space | prepend: ' ' %} - {% capture my_toc %}{{ my_toc }} -{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{{ new_line }}{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %} {% endif %} - {% capture my_toc %}{{ my_toc }} -{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{{ new_line }}{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} {% assign previousLevel = indentAmount %} {% endfor %} {% if include.class and include.class != blank %} - {% capture my_toc %}{:.{{ include.class }}} -{{ my_toc | lstrip }}{% endcapture %} + {% capture my_toc %}{:.{{ include.class }}}{{ new_line }}{{ my_toc | lstrip }}{% endcapture %} {% endif %} {% if include.id %} - {% capture my_toc %}{: #{{ include.id }}} -{{ my_toc | lstrip }}{% endcapture %} + {% capture my_toc %}{: #{{ include.id }}}{{ new_line }}{{ my_toc | lstrip }}{% endcapture %} {% endif %} - {% if include.submenu_class != blank %} + {% if include.submenu_class and include.submenu_class != blank %} {% for i in (1..previousLevel) %} {% assign lvl = previousLevel | plus: 1 | minus: i %} From 2ecc4c7e87fc171fb5f596658563ba8f49d88d49 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 20:57:11 -0800 Subject: [PATCH 03/15] Generate ToC via HTML instead of markdown --- _includes/toc.html | 101 ++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index 3def812..03cb521 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -24,7 +24,7 @@ OTHER DEALINGS IN THE SOFTWARE. {% endcomment %} {% comment %} - Version 1.0.14 + Version 1.1.0-dev 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 @@ -53,7 +53,6 @@ generate the table of contents and will NOT output the markdown given to it {% endcomment %} - {% capture new_line %} {% endcapture %} @@ -62,20 +61,22 @@ {% assign skipNoIDs = include.skipNoIDs | default: false %} {% assign minHeader = include.h_min | default: 1 %} {% assign maxHeader = include.h_max | default: 6 %} - {% assign nodes = include.html | split: ' maxHeader %} + {% if currLevel < minHeader or currLevel > maxHeader %} {% continue %} {% endif %} @@ -83,87 +84,75 @@ {% assign _idWorkspace = _workspace[0] | split: 'id="' %} {% assign _idWorkspace = _idWorkspace[1] | split: '"' %} - {% assign html_id = _idWorkspace[0] %} + {% assign htmlID = _idWorkspace[0] %} {% assign _classWorkspace = _workspace[0] | split: 'class="' %} {% assign _classWorkspace = _classWorkspace[1] | split: '"' %} - {% assign html_class = _classWorkspace[0] %} + {% assign htmlClass = _classWorkspace[0] %} - {% if html_class contains "no_toc" %} + {% if htmlClass contains "no_toc" %} {% continue %} {% endif %} {% if firstHeader %} - {% assign firstHeader = false %} - {% assign minHeader = headerLevel %} + {% assign minHeader = currLevel %} {% endif %} {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %} {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} - {% assign indentAmount = headerLevel | minus: minHeader %} - {% 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 %} + {% capture listItemClass %}class="{{ include.item_class | replace: '%level%', currLevel }}"{% endcapture %} {% endif %} {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %} - {% if html_id %} - {% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %} + {% if htmlID %} + {% capture listItem %}{{ anchor_body }}{% endcapture %} {% elsif skipNoIDs == true %} {% continue %} {% else %} - {% capture list_item %}{{ anchor_body }}{% endcapture %} + {% capture listItem %}{{ anchor_body }}{% endcapture %} {% endif %} - - {% if include.submenu_class and previousLevel > indentAmount %} - - {% assign submenuIndentation = space | prepend: ' ' %} - - {% capture my_toc %}{{ my_toc }}{{ new_line }}{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %} + {% if currLevel > lastLevel %} + {% capture my_toc %}{{ my_toc }}
    {{ new_line }}{% endcapture %} + {% elsif currLevel < lastLevel %} + {% assign repeatCount = lastLevel | minus: currLevel %} + + {% for i in (1..repeatCount) %} + {% capture my_toc %}{{ my_toc }}
{% endcapture %} + {% endfor %} + + {% capture my_toc %}{{ my_toc }}{% endcapture %} + {% else %} + {% capture my_toc %}{{ my_toc }}{{ new_line }}{% endcapture %} {% endif %} - {% capture my_toc %}{{ my_toc }}{{ new_line }}{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} + {% capture my_toc %}{{ my_toc }}
  • {{ listItem }}{% endcapture %} + + {% assign lastLevel = currLevel %} + {% assign firstHeader = false %} + {% endfor %} - {% assign previousLevel = indentAmount %} + {% assign repeatCount = minHeader | minus: 1 %} + {% assign repeatCount = lastLevel | minus: repeatCount %} + {% for i in (1..repeatCount) %} + {% capture my_toc %}{{ my_toc }}
  • {{ new_line }}{% endcapture %} {% endfor %} + {% assign root_attributes = '' %} {% if include.class and include.class != blank %} - {% capture my_toc %}{:.{{ include.class }}}{{ new_line }}{{ my_toc | lstrip }}{% endcapture %} + {% capture root_attributes %}class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} - {% if include.id %} - {% capture my_toc %}{: #{{ include.id }}}{{ new_line }}{{ my_toc | lstrip }}{% endcapture %} + {% if include.id and include.id != blank %} + {% capture root_attributes %}{{ root_attributes }} id="{{ include.id }}"{% endcapture %} {% endif %} - - {% if include.submenu_class and include.submenu_class != blank %} - - {% for i in (1..previousLevel) %} - {% assign lvl = previousLevel | plus: 1 | minus: i %} - {% assign closingSpace = '' %} - - {% for i in (1..lvl) %} - {% assign closingSpace = closingSpace | prepend: ' ' %} - {% endfor %} - - {% capture my_toc %}{{ my_toc }}{{ new_line }}{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} - {% endfor %} + {% if root_attributes %} + {% assign toc_length = my_toc | size %} + {% capture my_toc %}
      {{ my_toc | slice: 4, toc_length }}{% endcapture %} {% endif %} -{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} +{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} From 39089b25028de5afd93527a0d6dd12d41d83c2c0 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 20:59:51 -0800 Subject: [PATCH 04/15] Fix OL vs UL generation --- _includes/toc.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index 03cb521..b552b04 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -67,7 +67,7 @@ {% assign currLevel = 0 %} {% assign lastLevel = 0 %} - {% capture listModifier %}{% if orderedList %}ol{% else %}li{% endif %}{% endcapture %} + {% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %} {% for node in nodes %} {% if node == "" %} @@ -117,12 +117,12 @@ {% endif %} {% if currLevel > lastLevel %} - {% capture my_toc %}{{ my_toc }}
        {{ new_line }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}<{{ listModifier }}>{{ new_line }}{% endcapture %} {% elsif currLevel < lastLevel %} {% assign repeatCount = lastLevel | minus: currLevel %} {% for i in (1..repeatCount) %} - {% capture my_toc %}{{ my_toc }}
      {% endcapture %} + {% capture my_toc %}{{ my_toc }}{% endcapture %} {% endfor %} {% capture my_toc %}{{ my_toc }}{% endcapture %} @@ -139,7 +139,7 @@ {% assign repeatCount = minHeader | minus: 1 %} {% assign repeatCount = lastLevel | minus: repeatCount %} {% for i in (1..repeatCount) %} - {% capture my_toc %}{{ my_toc }}
    {{ new_line }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{{ new_line }}{% endcapture %} {% endfor %} {% assign root_attributes = '' %} @@ -153,6 +153,6 @@ {% if root_attributes %} {% assign toc_length = my_toc | size %} - {% capture my_toc %}
      {{ my_toc | slice: 4, toc_length }}{% endcapture %} + {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ my_toc | slice: 4, toc_length }}{% endcapture %} {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} From 63da8cbcc5729b60d71b5235af7ee05ee4c2d6bd Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:09:11 -0800 Subject: [PATCH 05/15] Fix render when no headings --- _includes/toc.html | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index b552b04..c38b4cf 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -142,17 +142,19 @@ {% capture my_toc %}{{ my_toc }}{{ new_line }}{% endcapture %} {% endfor %} - {% assign root_attributes = '' %} - {% if include.class and include.class != blank %} - {% capture root_attributes %}class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} - {% endif %} + {% if my_toc != '' %} + {% assign root_attributes = '' %} + {% if include.class and include.class != blank %} + {% capture root_attributes %}class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} - {% if include.id and include.id != blank %} - {% capture root_attributes %}{{ root_attributes }} id="{{ include.id }}"{% endcapture %} - {% endif %} + {% if include.id and include.id != blank %} + {% capture root_attributes %}{{ root_attributes }} id="{{ include.id }}"{% endcapture %} + {% endif %} - {% if root_attributes %} - {% assign toc_length = my_toc | size %} - {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ my_toc | slice: 4, toc_length }}{% endcapture %} + {% if root_attributes %} + {% assign toc_length = my_toc | size %} + {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ my_toc | slice: 4, toc_length }}{% endcapture %} + {% endif %} {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} From a6727991832eb33cff133458fe8621f767cddbbc Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:10:19 -0800 Subject: [PATCH 06/15] Minor improvements to unit tests file --- tests.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests.py b/tests.py index edbadbe..eb057e8 100644 --- a/tests.py +++ b/tests.py @@ -3,7 +3,7 @@ import unittest import xml.etree.ElementTree as ET -class TestSequense(unittest.TestCase): +class TestSequence(unittest.TestCase): pass def test_generator(a, b): @@ -11,9 +11,14 @@ def test(self): self.assertEqual(a, b) return test -def normalize_xml(xml): - tree = ET.fromstring(xml) - return re.sub('\\n\s+', '', ET.tostring(tree)) +def normalize_xml(xml, test_file): + try: + tree = ET.fromstring(xml) + except: + print(f">> Invalid XML in {test_file}") + raise + + return re.sub('\\n\s+', '', str(ET.tostring(tree))) if __name__ == '__main__': test_path = os.path.join(os.getcwd(), '_site', 'tests') @@ -22,12 +27,12 @@ def normalize_xml(xml): path = os.path.join(test_path, test_file) with open(path, 'r') as file: actual, expected = file.read().split('') - actual = normalize_xml(actual) - expected = normalize_xml(expected) + actual = normalize_xml(actual, test_file) + expected = normalize_xml(expected, test_file) - # Add the unit test to our TestSequense + # Add the unit test to our TestSequence test_name = 'test_{}'.format(test_file) test = test_generator(actual, expected) - setattr(TestSequense, test_name, test) + setattr(TestSequence, test_name, test) unittest.main() From c6570e62263afff15316b1a95ab14e7731622fbf Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:25:34 -0800 Subject: [PATCH 07/15] Fix anchor_class and item_class support --- _includes/toc.html | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index c38b4cf..5672c9d 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -53,9 +53,6 @@ generate the table of contents and will NOT output the markdown given to it {% endcomment %} -{% capture new_line %} -{% endcapture %} - {% capture my_toc %}{% endcapture %} {% assign orderedList = include.ordered | default: false %} {% assign skipNoIDs = include.skipNoIDs | default: false %} @@ -102,22 +99,27 @@ {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} {% if include.item_class and include.item_class != blank %} - {% capture listItemClass %}class="{{ include.item_class | replace: '%level%', currLevel }}"{% endcapture %} + {% capture listItemClass %}class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} - {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} - {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %} + {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} {% if htmlID %} - {% capture listItem %}{{ anchor_body }}{% endcapture %} + {% capture anchorAttributes %}href="{% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ htmlID }}"{% endcapture %} + + {% if include.anchor_class %} + {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + + {% capture listItem %}{{ anchorBody }}{% endcapture %} {% elsif skipNoIDs == true %} {% continue %} {% else %} - {% capture listItem %}{{ anchor_body }}{% endcapture %} + {% capture listItem %}{{ anchorBody }}{% endcapture %} {% endif %} {% if currLevel > lastLevel %} - {% capture my_toc %}{{ my_toc }}<{{ listModifier }}>{{ new_line }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}<{{ listModifier }}>{% endcapture %} {% elsif currLevel < lastLevel %} {% assign repeatCount = lastLevel | minus: currLevel %} @@ -127,10 +129,10 @@ {% capture my_toc %}{{ my_toc }}{% endcapture %} {% else %} - {% capture my_toc %}{{ my_toc }}{{ new_line }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{% endcapture %} {% endif %} - {% capture my_toc %}{{ my_toc }}
    • {{ listItem }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}
    • {{ listItem }}{% endcapture %} {% assign lastLevel = currLevel %} {% assign firstHeader = false %} @@ -139,7 +141,7 @@ {% assign repeatCount = minHeader | minus: 1 %} {% assign repeatCount = lastLevel | minus: repeatCount %} {% for i in (1..repeatCount) %} - {% capture my_toc %}{{ my_toc }}
    • {{ new_line }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{% endcapture %} {% endfor %} {% if my_toc != '' %} From 4ae888bc1376ff48e0f0501eb4ff08ade6a15121 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:31:18 -0800 Subject: [PATCH 08/15] Fix newline + space sensitivity in unit tests --- tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.py b/tests.py index eb057e8..24787f2 100644 --- a/tests.py +++ b/tests.py @@ -18,7 +18,7 @@ def normalize_xml(xml, test_file): print(f">> Invalid XML in {test_file}") raise - return re.sub('\\n\s+', '', str(ET.tostring(tree))) + return re.sub(r'(\\n|\n)\s*', '', str(ET.tostring(tree)), 0, re.MULTILINE) if __name__ == '__main__': test_path = os.path.join(os.getcwd(), '_site', 'tests') From 6fbbe5117458d7d523785f588350964cf7653e05 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:44:40 -0800 Subject: [PATCH 09/15] Fix support for submenu_class --- _includes/toc.html | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index 5672c9d..63db37a 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -102,6 +102,11 @@ {% capture listItemClass %}class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} + {% if include.submenu_class and include.submenu_class != blank %} + {% assign subMenuLevel = currLevel | minus: 1 %} + {% capture subMenuClass %}class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} + {% endif %} + {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} {% if htmlID %} @@ -119,7 +124,7 @@ {% endif %} {% if currLevel > lastLevel %} - {% capture my_toc %}{{ my_toc }}<{{ listModifier }}>{% endcapture %} + {% capture my_toc %}{{ my_toc }}<{{ listModifier }} {{ subMenuClass }}>{% endcapture %} {% elsif currLevel < lastLevel %} {% assign repeatCount = lastLevel | minus: currLevel %} @@ -155,8 +160,8 @@ {% endif %} {% if root_attributes %} - {% assign toc_length = my_toc | size %} - {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ my_toc | slice: 4, toc_length }}{% endcapture %} + {% assign nodes = my_toc | split: '>' %} + {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} {% endif %} {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} From 7160118ce7d4d2a848a0e93801f3f7ce29fec42b Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Fri, 27 Nov 2020 21:46:57 -0800 Subject: [PATCH 10/15] Setup Python 3.8 --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b44c909..3fb9eb0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,10 @@ jobs: with: ruby-version: '2.5' + - uses: actions/setup-python@v2 + with: + python-version: '3.8' + - name: Configure Jekyll if: matrix.jekyll-versions != '(GitHub Pages)' run: | From e6757a4884780a24a7ec0c20f8387c9149cc276f Mon Sep 17 00:00:00 2001 From: Christopher Yeh Date: Sat, 28 Nov 2020 01:24:28 -0800 Subject: [PATCH 11/15] Remove extraneous whitespace Removes extra whitespace in HTML opening tags (li, ul, ol, a) when there are no attributes and classes. For example, "
    • " is fixed to be "
    • ", and "" is fixed to be "". --- _includes/toc.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index 63db37a..d79bd60 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -99,24 +99,24 @@ {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %} {% if include.item_class and include.item_class != blank %} - {% capture listItemClass %}class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} + {% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} {% if include.submenu_class and include.submenu_class != blank %} {% assign subMenuLevel = currLevel | minus: 1 %} - {% capture subMenuClass %}class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} + {% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} {% if htmlID %} - {% capture anchorAttributes %}href="{% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ htmlID }}"{% endcapture %} + {% capture anchorAttributes %} href="{% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ htmlID }}"{% endcapture %} {% if include.anchor_class %} {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} - {% capture listItem %}{{ anchorBody }}{% endcapture %} + {% capture listItem %}{{ anchorBody }}{% endcapture %} {% elsif skipNoIDs == true %} {% continue %} {% else %} @@ -124,7 +124,7 @@ {% endif %} {% if currLevel > lastLevel %} - {% capture my_toc %}{{ my_toc }}<{{ listModifier }} {{ subMenuClass }}>{% endcapture %} + {% capture my_toc %}{{ my_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} {% elsif currLevel < lastLevel %} {% assign repeatCount = lastLevel | minus: currLevel %} @@ -137,7 +137,7 @@ {% capture my_toc %}{{ my_toc }}
    • {% endcapture %} {% endif %} - {% capture my_toc %}{{ my_toc }}
    • {{ listItem }}{% endcapture %} + {% capture my_toc %}{{ my_toc }}{{ listItem }}{% endcapture %} {% assign lastLevel = currLevel %} {% assign firstHeader = false %} @@ -152,7 +152,7 @@ {% if my_toc != '' %} {% assign root_attributes = '' %} {% if include.class and include.class != blank %} - {% capture root_attributes %}class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} + {% capture root_attributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} {% if include.id and include.id != blank %} @@ -161,7 +161,7 @@ {% if root_attributes %} {% assign nodes = my_toc | split: '>' %} - {% capture my_toc %}<{{ listModifier }} {{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} + {% capture my_toc %}<{{ listModifier }}{{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} {% endif %} {% endif %} {% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} From 78d6850208f3910b8af1a3ffea9b168fe426d7a6 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 28 Nov 2020 14:52:26 -0800 Subject: [PATCH 12/15] Rename 'my_toc' internal variable to 'jekyll_toc' --- _includes/toc.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index d79bd60..4e644fd 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -53,7 +53,7 @@ generate the table of contents and will NOT output the markdown given to it {% endcomment %} - {% capture my_toc %}{% endcapture %} + {% capture jekyll_toc %}{% endcapture %} {% assign orderedList = include.ordered | default: false %} {% assign skipNoIDs = include.skipNoIDs | default: false %} {% assign minHeader = include.h_min | default: 1 %} @@ -124,20 +124,20 @@ {% endif %} {% if currLevel > lastLevel %} - {% capture my_toc %}{{ my_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} {% elsif currLevel < lastLevel %} {% assign repeatCount = lastLevel | minus: currLevel %} {% for i in (1..repeatCount) %} - {% capture my_toc %}{{ my_toc }}
    • {% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} {% endfor %} - {% capture my_toc %}{{ my_toc }}{% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} {% else %} - {% capture my_toc %}{{ my_toc }}{% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} {% endif %} - {% capture my_toc %}{{ my_toc }}{{ listItem }}{% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}{{ listItem }}{% endcapture %} {% assign lastLevel = currLevel %} {% assign firstHeader = false %} @@ -146,10 +146,10 @@ {% assign repeatCount = minHeader | minus: 1 %} {% assign repeatCount = lastLevel | minus: repeatCount %} {% for i in (1..repeatCount) %} - {% capture my_toc %}{{ my_toc }}{% endcapture %} + {% capture jekyll_toc %}{{ jekyll_toc }}{% endcapture %} {% endfor %} - {% if my_toc != '' %} + {% if jekyll_toc != '' %} {% assign root_attributes = '' %} {% if include.class and include.class != blank %} {% capture root_attributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} @@ -160,8 +160,8 @@ {% endif %} {% if root_attributes %} - {% assign nodes = my_toc | split: '>' %} - {% capture my_toc %}<{{ listModifier }}{{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} + {% assign nodes = jekyll_toc | split: '>' %} + {% capture jekyll_toc %}<{{ listModifier }}{{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} {% endif %} {% endif %} -{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc }} +{% endcapture %}{% assign tocWorkspace = '' %}{{ jekyll_toc }}{% assign jekyll_toc = '' %} From effb2010e4d9b6d2863fe9d7e8d2e020588d0c74 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 28 Nov 2020 15:34:45 -0800 Subject: [PATCH 13/15] Use camelCase for `rootAttributes` variable --- _includes/toc.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/_includes/toc.html b/_includes/toc.html index 4e644fd..f656ff9 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -150,18 +150,18 @@ {% endfor %} {% if jekyll_toc != '' %} - {% assign root_attributes = '' %} + {% assign rootAttributes = '' %} {% if include.class and include.class != blank %} - {% capture root_attributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} + {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} {% endif %} {% if include.id and include.id != blank %} - {% capture root_attributes %}{{ root_attributes }} id="{{ include.id }}"{% endcapture %} + {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %} {% endif %} - {% if root_attributes %} + {% if rootAttributes %} {% assign nodes = jekyll_toc | split: '>' %} - {% capture jekyll_toc %}<{{ listModifier }}{{ root_attributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} + {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} {% endif %} {% endif %} -{% endcapture %}{% assign tocWorkspace = '' %}{{ jekyll_toc }}{% assign jekyll_toc = '' %} +{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }} From 621f4937f287a86caa91e1dbf439274371617153 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 28 Nov 2020 15:42:27 -0800 Subject: [PATCH 14/15] Deprecate `baseurl` and `skipNoIDs` --- README.md | 9 +++- _includes/toc.html | 23 ++++++++-- _tests/base_url-deprecated.md | 15 +++++++ _tests/base_url.md | 2 +- _tests/skipHeadingsWithNoIDs-Deprecated.md | 50 ++++++++++++++++++++++ _tests/skipHeadingsWithNoIDs.md | 2 +- 6 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 _tests/base_url-deprecated.md create mode 100644 _tests/skipHeadingsWithNoIDs-Deprecated.md diff --git a/README.md b/README.md index e76c4bb..bf4677b 100644 --- a/README.md +++ b/README.md @@ -58,12 +58,17 @@ This snippet is highly customizable. Here are the available parameters to change | `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 | | `submenu_class` | string | '' | add custom class(es) for each child group of headings; has support for `%level%` placeholder which is the current "submenu" heading level | -| `baseurl` | string | '' | add a base url to the TOC links for when your TOC is on another page than the actual content | +| `base_url` | 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 | -| `skipNoIDs` | bool | false | skip headers that do not have an `id` attribute | +| `skip_no_ids` | bool | false | skip headers that do not have an `id` attribute | * This is a required parameter +### Deprecated Variables + +- `baseurl` has been deprecated since 1.1.0, use `base_url` instead +- `skipNoIDs` has been deprecated since 1.1.0, use `skip_no_ids` instead + ## Performance The performance impact of this snippet on your site is pretty negligible. The stats below were gotten from Jekyll's `--profile` option. diff --git a/_includes/toc.html b/_includes/toc.html index f656ff9..e77633b 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -44,18 +44,33 @@ * 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 * submenu_class (string) : '' - add custom class(es) for each child group of headings; has support for '%level%' placeholder which is the current "submenu" heading level - * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content + * base_url (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 - * skipNoIDs (bool) : false - skip headers that do not have an `id` attribute + * skip_no_ids (bool) : false - skip headers that do not have an `id` attribute 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 newline %} + {% endcapture %} + {% assign newline = newline | rstrip %} + + {% capture deprecation_warnings %}{% endcapture %} + + {% if include.baseurl %} + {% capture deprecation_warnings %}{{ deprecation_warnings }}{{ newline }}{% endcapture %} + {% endif %} + + {% if include.skipNoIDs %} + {% capture deprecation_warnings %}{{ deprecation_warnings }}{{ newline }}{% endcapture %} + {% endif %} + {% capture jekyll_toc %}{% endcapture %} {% assign orderedList = include.ordered | default: false %} - {% assign skipNoIDs = include.skipNoIDs | default: false %} + {% assign baseURL = include.base_url | default: include.baseurl | default: '' %} + {% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %} {% assign minHeader = include.h_min | default: 1 %} {% assign maxHeader = include.h_max | default: 6 %} {% assign nodes = include.html | strip | split: ' + + diff --git a/_tests/base_url.md b/_tests/base_url.md index 40ec785..282f376 100644 --- a/_tests/base_url.md +++ b/_tests/base_url.md @@ -6,7 +6,7 @@ {% endcapture %} {% assign text = markdown | markdownify %} -{% include toc.html html=text baseurl="example.org" %} +{% include toc.html html=text base_url="example.org" %} diff --git a/_tests/skipHeadingsWithNoIDs-Deprecated.md b/_tests/skipHeadingsWithNoIDs-Deprecated.md new file mode 100644 index 0000000..e90cd7f --- /dev/null +++ b/_tests/skipHeadingsWithNoIDs-Deprecated.md @@ -0,0 +1,50 @@ +--- +# https://github.com/allejo/jekyll-toc/issues/32 +--- + +{% capture markdown %} +## Sample Usage + +
      +

      My Awesome Example Page

      +

      With an awesome subtitle

      + Dummy Link +
      + +### Known Problems + +Lots! + +### Resources + +#### Paid + +#### Free +{% endcapture %} +{% assign text = markdown | markdownify %} + +{% include toc.html html=text skipNoIDs=true %} + + + + diff --git a/_tests/skipHeadingsWithNoIDs.md b/_tests/skipHeadingsWithNoIDs.md index e90cd7f..52aea43 100644 --- a/_tests/skipHeadingsWithNoIDs.md +++ b/_tests/skipHeadingsWithNoIDs.md @@ -23,7 +23,7 @@ Lots! {% endcapture %} {% assign text = markdown | markdownify %} -{% include toc.html html=text skipNoIDs=true %} +{% include toc.html html=text skip_no_ids=true %} From fc252c029cfefb54a7ff3783a5191ef03c99f0f2 Mon Sep 17 00:00:00 2001 From: Vladimir Jimenez Date: Sat, 28 Nov 2020 15:46:24 -0800 Subject: [PATCH 15/15] Version bump to 1.1.0 --- _includes/toc.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_includes/toc.html b/_includes/toc.html index e77633b..8c71007 100644 --- a/_includes/toc.html +++ b/_includes/toc.html @@ -24,7 +24,7 @@ OTHER DEALINGS IN THE SOFTWARE. {% endcomment %} {% comment %} - Version 1.1.0-dev + Version 1.1.0 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