Skip to content

Commit 29d9d90

Browse files
committed
Added TOC
1 parent 5507517 commit 29d9d90

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

_includes/toc.html

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{% capture tocWorkspace %}
2+
{% comment %}
3+
Version 1.0.6
4+
https://github.com/allejo/jekyll-toc
5+
6+
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
7+
8+
Usage:
9+
{% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
10+
11+
Parameters:
12+
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
13+
14+
Optional Parameters:
15+
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
16+
* class (string) : '' - a CSS class assigned to the TOC
17+
* id (string) : '' - an ID to assigned to the TOC
18+
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
19+
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
20+
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
21+
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
22+
* baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
23+
* anchor_class (string) : '' - add custom class(es) for each anchor element
24+
25+
Output:
26+
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
27+
generate the table of contents and will NOT output the markdown given to it
28+
{% endcomment %}
29+
30+
{% capture my_toc %}{% endcapture %}
31+
{% assign orderedList = include.ordered | default: false %}
32+
{% assign minHeader = include.h_min | default: 1 %}
33+
{% assign maxHeader = include.h_max | default: 6 %}
34+
{% assign nodes = include.html | split: '<h' %}
35+
{% assign firstHeader = true %}
36+
37+
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
38+
39+
{% for node in nodes %}
40+
{% if node == "" %}
41+
{% continue %}
42+
{% endif %}
43+
44+
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
45+
46+
{% if headerLevel < minHeader or headerLevel > maxHeader %}
47+
{% continue %}
48+
{% endif %}
49+
50+
{% if firstHeader %}
51+
{% assign firstHeader = false %}
52+
{% assign minHeader = headerLevel %}
53+
{% endif %}
54+
55+
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
56+
{% assign _workspace = node | split: '</h' %}
57+
58+
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
59+
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
60+
{% assign html_id = _idWorkspace[0] %}
61+
62+
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
63+
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
64+
65+
{% assign space = '' %}
66+
{% for i in (1..indentAmount) %}
67+
{% assign space = space | prepend: ' ' %}
68+
{% endfor %}
69+
70+
{% unless include.item_class == blank %}
71+
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
72+
{% endunless %}
73+
74+
{% capture my_toc %}{{ my_toc }}
75+
{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
76+
{% endfor %}
77+
78+
{% if include.class %}
79+
{% capture my_toc %}{:.{{ include.class }}}
80+
{{ my_toc | lstrip }}{% endcapture %}
81+
{% endif %}
82+
83+
{% if include.id %}
84+
{% capture my_toc %}{: #{{ include.id }}}
85+
{{ my_toc | lstrip }}{% endcapture %}
86+
{% endif %}
87+
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}

_layouts/post.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ <h1 class="posttitle">{{ page.title }}</h1>
4848

4949
<!-- Post Featured Image -->
5050
{% if page.image %}<img class="featured-image img-fluid" src="{% if page.image contains "://" %}{{ page.image }}{% else %}{{ page.image | absolute_url }}{% endif %}" alt="{{ page.title }}">{% endif %}
51-
<!-- End Featured Image -->
51+
<!-- End Featured Image -->
52+
53+
<!-- Toc if any -->
54+
{% if page.toc %}
55+
<div class="toc mt-5 mb-5 lead">
56+
<h3 class="font-weight-bold">Summary</h3>
57+
{% include toc.html html=content %}
58+
</div>
59+
{% endif %}
60+
<!-- End Toc -->
5261

5362
<!-- Post Content -->
5463
<div class="article-post">

_posts/2018-06-12-education.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: "Education must also train one for quick, resolute and effective thinkin
44
author: john
55
categories: [ Jekyll, tutorial ]
66
image: assets/images/3.jpg
7+
toc: true
78
---
89
There are lots of powerful things you can do with the Markdown editor
910

assets/css/screen.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ License: https://www.wowthemes.net/freebies-license/
154154
margin-bottom:5px;
155155
margin-top:5px;
156156
font-size:0.95rem;
157+
}
158+
.toc ul {
159+
list-style: decimal;
160+
font-weight: 400;
157161
}
158162
.author-meta {
159163
flex:1 1 auto;

0 commit comments

Comments
 (0)