|
24 | 24 | OTHER DEALINGS IN THE SOFTWARE.
|
25 | 25 | {% endcomment %}
|
26 | 26 | {% comment %}
|
27 |
| - Version 1.0.14 |
| 27 | + Version 1.1.0 |
28 | 28 | https://github.com/allejo/jekyll-toc
|
29 | 29 |
|
30 | 30 | "...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
|
|
44 | 44 | * ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
|
45 | 45 | * item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
|
46 | 46 | * 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
|
47 |
| - * baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content |
| 47 | + * base_url (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content |
48 | 48 | * anchor_class (string) : '' - add custom class(es) for each anchor element
|
49 |
| - * skipNoIDs (bool) : false - skip headers that do not have an `id` attribute |
| 49 | + * skip_no_ids (bool) : false - skip headers that do not have an `id` attribute |
50 | 50 |
|
51 | 51 | Output:
|
52 | 52 | An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
|
53 | 53 | generate the table of contents and will NOT output the markdown given to it
|
54 | 54 | {% endcomment %}
|
55 | 55 |
|
56 |
| - {% capture my_toc %}{% endcapture %} |
| 56 | + {% capture newline %} |
| 57 | + {% endcapture %} |
| 58 | + {% assign newline = newline | rstrip %} <!-- Remove the extra spacing but preserve the newline --> |
| 59 | + |
| 60 | + {% capture deprecation_warnings %}{% endcapture %} |
| 61 | + |
| 62 | + {% if include.baseurl %} |
| 63 | + {% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "baseurl" has been deprecated, use "base_url" instead -->{{ newline }}{% endcapture %} |
| 64 | + {% endif %} |
| 65 | + |
| 66 | + {% if include.skipNoIDs %} |
| 67 | + {% capture deprecation_warnings %}{{ deprecation_warnings }}<!-- jekyll-toc :: "skipNoIDs" has been deprecated, use "skip_no_ids" instead -->{{ newline }}{% endcapture %} |
| 68 | + {% endif %} |
| 69 | + |
| 70 | + {% capture jekyll_toc %}{% endcapture %} |
57 | 71 | {% assign orderedList = include.ordered | default: false %}
|
58 |
| - {% assign skipNoIDs = include.skipNoIDs | default: false %} |
| 72 | + {% assign baseURL = include.base_url | default: include.baseurl | default: '' %} |
| 73 | + {% assign skipNoIDs = include.skip_no_ids | default: include.skipNoIDs | default: false %} |
59 | 74 | {% assign minHeader = include.h_min | default: 1 %}
|
60 | 75 | {% assign maxHeader = include.h_max | default: 6 %}
|
61 |
| - {% assign nodes = include.html | split: '<h' %} |
| 76 | + {% assign nodes = include.html | strip | split: '<h' %} |
| 77 | + |
62 | 78 | {% assign firstHeader = true %}
|
63 |
| - {% assign previousLevel = 0 %} |
| 79 | + {% assign currLevel = 0 %} |
| 80 | + {% assign lastLevel = 0 %} |
64 | 81 |
|
65 |
| - {% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %} |
| 82 | + {% capture listModifier %}{% if orderedList %}ol{% else %}ul{% endif %}{% endcapture %} |
66 | 83 |
|
67 | 84 | {% for node in nodes %}
|
68 | 85 | {% if node == "" %}
|
69 | 86 | {% continue %}
|
70 | 87 | {% endif %}
|
71 | 88 |
|
72 |
| - {% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %} |
| 89 | + {% assign currLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %} |
73 | 90 |
|
74 |
| - {% if headerLevel < minHeader or headerLevel > maxHeader %} |
| 91 | + {% if currLevel < minHeader or currLevel > maxHeader %} |
75 | 92 | {% continue %}
|
76 | 93 | {% endif %}
|
77 | 94 |
|
78 | 95 | {% assign _workspace = node | split: '</h' %}
|
79 | 96 |
|
80 | 97 | {% assign _idWorkspace = _workspace[0] | split: 'id="' %}
|
81 | 98 | {% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
|
82 |
| - {% assign html_id = _idWorkspace[0] %} |
| 99 | + {% assign htmlID = _idWorkspace[0] %} |
83 | 100 |
|
84 | 101 | {% assign _classWorkspace = _workspace[0] | split: 'class="' %}
|
85 | 102 | {% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
|
86 |
| - {% assign html_class = _classWorkspace[0] %} |
| 103 | + {% assign htmlClass = _classWorkspace[0] %} |
87 | 104 |
|
88 |
| - {% if html_class contains "no_toc" %} |
| 105 | + {% if htmlClass contains "no_toc" %} |
89 | 106 | {% continue %}
|
90 | 107 | {% endif %}
|
91 | 108 |
|
92 | 109 | {% if firstHeader %}
|
93 |
| - {% assign firstHeader = false %} |
94 |
| - {% assign minHeader = headerLevel %} |
| 110 | + {% assign minHeader = currLevel %} |
95 | 111 | {% endif %}
|
96 | 112 |
|
97 | 113 | {% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
|
98 | 114 | {% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
|
99 | 115 |
|
100 |
| - {% assign indentAmount = headerLevel | minus: minHeader %} |
101 |
| - {% assign space = '' %} |
102 |
| - {% for i in (1..indentAmount) %} |
103 |
| - {% assign space = space | prepend: ' ' %} |
104 |
| - {% endfor %} |
105 |
| - |
106 | 116 | {% if include.item_class and include.item_class != blank %}
|
107 |
| - {% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %} |
| 117 | + {% capture listItemClass %} class="{{ include.item_class | replace: '%level%', currLevel | split: '.' | join: ' ' }}"{% endcapture %} |
| 118 | + {% endif %} |
| 119 | + |
| 120 | + {% if include.submenu_class and include.submenu_class != blank %} |
| 121 | + {% assign subMenuLevel = currLevel | minus: 1 %} |
| 122 | + {% capture subMenuClass %} class="{{ include.submenu_class | replace: '%level%', subMenuLevel | split: '.' | join: ' ' }}"{% endcapture %} |
108 | 123 | {% endif %}
|
109 | 124 |
|
110 |
| - {% capture anchor_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} |
111 |
| - {% capture anchor_body %}{{ anchor_body | replace: "|", "\|" }}{% endcapture %} |
| 125 | + {% capture anchorBody %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %} |
| 126 | + |
| 127 | + {% if htmlID %} |
| 128 | + {% capture anchorAttributes %} href="{% if baseURL %}{{ baseURL }}{% endif %}#{{ htmlID }}"{% endcapture %} |
| 129 | + |
| 130 | + {% if include.anchor_class %} |
| 131 | + {% capture anchorAttributes %}{{ anchorAttributes }} class="{{ include.anchor_class | split: '.' | join: ' ' }}"{% endcapture %} |
| 132 | + {% endif %} |
112 | 133 |
|
113 |
| - {% if html_id %} |
114 |
| - {% capture list_item %}[{{ anchor_body }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% endcapture %} |
| 134 | + {% capture listItem %}<a{{ anchorAttributes }}>{{ anchorBody }}</a>{% endcapture %} |
115 | 135 | {% elsif skipNoIDs == true %}
|
116 | 136 | {% continue %}
|
117 | 137 | {% else %}
|
118 |
| - {% capture list_item %}{{ anchor_body }}{% endcapture %} |
| 138 | + {% capture listItem %}{{ anchorBody }}{% endcapture %} |
119 | 139 | {% endif %}
|
120 | 140 |
|
121 |
| - <!-- |
122 |
| - If we have a submenu class and we're unindenting, then we need to add a "closing" class to this group of bullet |
123 |
| - points |
124 |
| - --> |
125 |
| - {% if include.submenu_class and previousLevel > indentAmount %} |
126 |
| - <!-- |
127 |
| - `space` is the current indentation, so we if want to close off the previous grouping, we need to add one |
128 |
| - more level of indentation |
129 |
| - --> |
130 |
| - {% assign submenuIndentation = space | prepend: ' ' %} |
131 |
| - |
132 |
| - {% capture my_toc %}{{ my_toc }} |
133 |
| -{{ submenuIndentation }}{:.{{ include.submenu_class | replace: '%level%', previousLevel }}}{% endcapture %} |
| 141 | + {% if currLevel > lastLevel %} |
| 142 | + {% capture jekyll_toc %}{{ jekyll_toc }}<{{ listModifier }}{{ subMenuClass }}>{% endcapture %} |
| 143 | + {% elsif currLevel < lastLevel %} |
| 144 | + {% assign repeatCount = lastLevel | minus: currLevel %} |
| 145 | + |
| 146 | + {% for i in (1..repeatCount) %} |
| 147 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %} |
| 148 | + {% endfor %} |
| 149 | + |
| 150 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
| 151 | + {% else %} |
| 152 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li>{% endcapture %} |
134 | 153 | {% endif %}
|
135 | 154 |
|
136 |
| - {% capture my_toc %}{{ my_toc }} |
137 |
| -{{ space }}{{ listModifier }} {{ listItemClass }} {{ list_item }}{% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %} |
| 155 | + {% capture jekyll_toc %}{{ jekyll_toc }}<li{{ listItemClass }}>{{ listItem }}{% endcapture %} |
138 | 156 |
|
139 |
| - {% assign previousLevel = indentAmount %} |
| 157 | + {% assign lastLevel = currLevel %} |
| 158 | + {% assign firstHeader = false %} |
140 | 159 | {% endfor %}
|
141 | 160 |
|
142 |
| - {% if include.class and include.class != blank %} |
143 |
| - {% capture my_toc %}{:.{{ include.class }}} |
144 |
| -{{ my_toc | lstrip }}{% endcapture %} |
145 |
| - {% endif %} |
| 161 | + {% assign repeatCount = minHeader | minus: 1 %} |
| 162 | + {% assign repeatCount = lastLevel | minus: repeatCount %} |
| 163 | + {% for i in (1..repeatCount) %} |
| 164 | + {% capture jekyll_toc %}{{ jekyll_toc }}</li></{{ listModifier }}>{% endcapture %} |
| 165 | + {% endfor %} |
146 | 166 |
|
147 |
| - {% if include.id %} |
148 |
| - {% capture my_toc %}{: #{{ include.id }}} |
149 |
| -{{ my_toc | lstrip }}{% endcapture %} |
150 |
| - {% endif %} |
| 167 | + {% if jekyll_toc != '' %} |
| 168 | + {% assign rootAttributes = '' %} |
| 169 | + {% if include.class and include.class != blank %} |
| 170 | + {% capture rootAttributes %} class="{{ include.class | split: '.' | join: ' ' }}"{% endcapture %} |
| 171 | + {% endif %} |
151 | 172 |
|
152 |
| - <!-- |
153 |
| - If we have a submenu class, we need to close off all the remaining list item groups so that submenu classes are |
154 |
| - applied correctly to them |
155 |
| - --> |
156 |
| - {% if include.submenu_class != blank %} |
157 |
| - <!-- The last level of indentation that we were at, we need to work backwards from there closing each group --> |
158 |
| - {% for i in (1..previousLevel) %} |
159 |
| - {% assign lvl = previousLevel | plus: 1 | minus: i %} <!-- Invert the indent level based on `i` --> |
160 |
| - {% assign closingSpace = '' %} |
161 |
| - |
162 |
| - {% for i in (1..lvl) %} |
163 |
| - {% assign closingSpace = closingSpace | prepend: ' ' %} |
164 |
| - {% endfor %} |
| 173 | + {% if include.id and include.id != blank %} |
| 174 | + {% capture rootAttributes %}{{ rootAttributes }} id="{{ include.id }}"{% endcapture %} |
| 175 | + {% endif %} |
165 | 176 |
|
166 |
| - {% capture my_toc %}{{ my_toc }} |
167 |
| -{{ closingSpace }}{:.{{ include.submenu_class | replace: '%level%', lvl }}}{% endcapture %} |
168 |
| - {% endfor %} |
| 177 | + {% if rootAttributes %} |
| 178 | + {% assign nodes = jekyll_toc | split: '>' %} |
| 179 | + {% capture jekyll_toc %}<{{ listModifier }}{{ rootAttributes }}>{{ nodes | shift | join: '>' }}>{% endcapture %} |
| 180 | + {% endif %} |
169 | 181 | {% endif %}
|
170 |
| -{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }} |
| 182 | +{% endcapture %}{% assign tocWorkspace = '' %}{{ deprecation_warnings }}{{ jekyll_toc }} |
0 commit comments