Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2dca0e9

Browse files
author
Liam Bigelow
committedJun 19, 2018
Adding table of contents to docs
1 parent 7cd93e5 commit 2dca0e9

File tree

4 files changed

+4307
-1
lines changed

4 files changed

+4307
-1
lines changed
 

‎src/_includes/toc.html

+85
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 }}

‎src/_layouts/doc.html

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<p class="docs-category">{% if page.category %}{{ page.category }}{% else %}{{ site.display_title }}{% endif %}</p>
3232
<h2>{{ page.title }}</h2>
3333

34+
<div class="toc">{% include toc.html html=content h_max=3 %}</div>
35+
3436
{% assign requirements = "" %}
3537

3638
{% if page.requirements.build == 'Jekyll' %}

‎src/_sass/_docs.scss

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
html, body{
2+
scroll-behavior: smooth;
3+
}
24

35
h3 + .warning {
46
margin-top: 1.75em;
@@ -30,3 +32,36 @@ h4 svg {
3032
article.content h3 {
3133
margin-top: 40px;
3234
}
35+
.docs-container h2 {
36+
margin: 0;
37+
}
38+
.toc ul {
39+
padding: 0;
40+
margin: 1rem -1rem 1.5rem -1rem;
41+
max-width: 500px;
42+
li {
43+
margin: 0 1rem;
44+
list-style-type: none;
45+
font-size: 14px;
46+
line-height: 1.8em;
47+
a {
48+
font-weight: bold;
49+
&:before {
50+
content: "";
51+
display: inline-block;
52+
margin-right: 5px;
53+
position: relative;
54+
transform: scaleY(1.8);
55+
font-size: 14px;
56+
}
57+
}
58+
59+
&:last-of-type {
60+
a {
61+
&:before {
62+
content: "";
63+
}
64+
}
65+
}
66+
}
67+
}

‎yarn.lock

+4,184
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
This repository has been archived.