Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 598f0d7

Browse files
committed
Add an optional Table of Contents
Fixes #8 Usus the awesome `jekyll-toc` from https://github.com/allejo/jekyll-toc
1 parent 161d59d commit 598f0d7

File tree

4 files changed

+117
-5
lines changed

4 files changed

+117
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
toc: true
3+
---
4+
15
# City of Amsterdam Jekyll theme
26

37
A jekyll theme for the set up of quick generic static sites and for use with GitHub pages and/or Jekyll in the [webdesign of the City of Amsterdam](https://patternlab-amsterdam.infoprojects.nl/).

_includes/toc.liquid

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{% capture tocWorkspace %}
2+
{% comment %}
3+
Version 1.0.4
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 for each list item; has support for '%level%' placeholder, which is the current heading level
22+
23+
Output:
24+
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
25+
{% endcomment %}
26+
27+
{% capture my_toc %}{% endcapture %}
28+
{% assign orderedList = include.ordered | default: false %}
29+
{% assign minHeader = include.h_min | default: 1 %}
30+
{% assign maxHeader = include.h_max | default: 6 %}
31+
{% assign nodes = include.html | split: '<h' %}
32+
{% assign firstHeader = true %}
33+
34+
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
35+
36+
{% for node in nodes %}
37+
{% if node == "" %}
38+
{% continue %}
39+
{% endif %}
40+
41+
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
42+
43+
{% if headerLevel < minHeader or headerLevel > maxHeader %}
44+
{% continue %}
45+
{% endif %}
46+
47+
{% if firstHeader %}
48+
{% assign firstHeader = false %}
49+
{% assign minHeader = headerLevel %}
50+
{% endif %}
51+
52+
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
53+
{% assign _workspace = node | split: '</h' %}
54+
55+
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
56+
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
57+
{% assign html_id = _idWorkspace[0] %}
58+
59+
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
60+
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
61+
62+
{% assign space = '' %}
63+
{% for i in (1..indentAmount) %}
64+
{% assign space = space | prepend: ' ' %}
65+
{% endfor %}
66+
67+
{% unless include.item_class == blank %}
68+
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
69+
{% endunless %}
70+
71+
{% capture my_toc %}{{ my_toc }}
72+
{{ space }}{{ listModifier }} {{ listItemClass }} [{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}](#{{ html_id }}){% endcapture %}
73+
74+
{% endfor %}
75+
76+
{% if include.class %}
77+
{% capture my_toc %}{:.{{ include.class }}}
78+
{{ my_toc | lstrip }}{% endcapture %}
79+
{% endif %}
80+
81+
{% if include.id %}
82+
{% capture my_toc %}{: #{{ include.id }}}
83+
{{ my_toc | lstrip }}{% endcapture %}
84+
{% endif %}
85+
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}

_layouts/default.liquid

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
layout: wrapper
33
---
44

5+
{% if page.toc != false and site.toc == true or page.toc == true %}
6+
{% include toc.liquid html=content h_min=2 sanitize=true class="table-of-contents" %}
7+
{% endif %}
8+
59
<article class="markdown-body">
610
{{content}}
711
</article>
12+
13+

assets/style.css

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ main>article.markdown-body {
101101
padding: 2em 0 4em;
102102
}
103103

104-
main>aside {
104+
main>aside, main>ul.table-of-contents {
105105
padding: 0 0 4em 0;
106106
}
107107
@media screen and (min-width: 1050px) {
108-
main>aside {
108+
main>aside, main>ul.table-of-contents {
109109
display: inline-block;
110-
padding: 4.6em 0 4em 6em;
110+
padding: 4.5em 2em 4em 0;
111111
max-width: 25%;
112112
vertical-align: top;
113113
}
@@ -121,9 +121,26 @@ main>aside span.score {
121121
height: 1.5em;
122122
text-align: center;
123123
}
124-
main>aside ul {
124+
125+
main>aside ul, main>ul.table-of-contents ul {
125126
list-style: none;
126-
padding: 0.75rem;
127+
padding: 0.5rem;
128+
}
129+
130+
ul.table-of-contents {
131+
font-size: 1.125em;
132+
list-style-type: none;
133+
}
134+
ul.table-of-contents li {
135+
padding: 0.25rem 0;
136+
}
137+
138+
ul.table-of-contents li li li {
139+
font-size: 1rem;
140+
}
141+
142+
a:link {
143+
font-weight: normal;
127144
}
128145

129146
/* The cards section in Projects and Guides */

0 commit comments

Comments
 (0)