Skip to content

Commit 6bc0aa2

Browse files
committed
Add TOC and styling
1 parent 030a70c commit 6bc0aa2

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

_includes/toc.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{% comment %}
2+
This creates a table of contents from headings in your content
3+
GitHub Pages compatible - no plugins required
4+
Preserves formatting like italics and bold in TOC links
5+
{% endcomment %}
6+
7+
{% assign toc_items = '' | split: '' %}
8+
{% assign html_content = content | markdownify %}
9+
10+
{% comment %} Use regex-like approach to find headings {% endcomment %}
11+
{% assign heading_sections = html_content | split: '<h' %}
12+
13+
{% for section in heading_sections %}
14+
{% if section contains '</h' %}
15+
{% comment %} Get the heading level {% endcomment %}
16+
{% assign level_char = section | slice: 0, 1 %}
17+
{% assign level = level_char | plus: 0 %}
18+
19+
{% if level >= 2 and level <= 6 %}
20+
{% comment %} Find everything after the first > and before the closing tag {% endcomment %}
21+
{% assign parts = section | split: '>' %}
22+
{% if parts.size > 1 %}
23+
{% assign remaining = parts | shift | join: '>' %}
24+
{% assign heading_content = remaining | split: '</h' | first %}
25+
26+
{% comment %} Create clean ID from text-only version {% endcomment %}
27+
{% assign clean_text = heading_content | strip_html | strip %}
28+
{% assign heading_id = clean_text | downcase | replace: ' ', '-' | replace: ':', '' | replace: '?', '' | replace: '!', '' | replace: '.', '' | replace: ',', '' | replace: ';', '' | replace: '"', '' | replace: "'", '' | replace: '(', '' | replace: ')', '' | replace: '[', '' | replace: ']', '' | replace: '{', '' | replace: '}', '' | replace: '/', '' | replace: '\', '' | replace: '+', '' | replace: '=', '' | replace: '*', '' | replace: '&', '' | replace: '%', '' | replace: '$', '' | replace: '#', '' | replace: '@', '' | replace: '<', '' | replace: '>', '' %}
29+
{% assign heading_id = heading_id | replace: '--', '-' | replace: '--', '-' %}
30+
31+
{% assign toc_item = level | append: '|||' | append: heading_id | append: '|||' | append: heading_content %}
32+
{% assign toc_items = toc_items | push: toc_item %}
33+
{% endif %}
34+
{% endif %}
35+
{% endif %}
36+
{% endfor %}
37+
38+
{% if toc_items.size > 0 %}
39+
<div class="table-of-contents">
40+
<h3>Page contents</h3>
41+
<ul>
42+
{% for item in toc_items %}
43+
{% assign parts = item | split: '|||' %}
44+
{% assign level = parts[0] %}
45+
{% assign id = parts[1] %}
46+
{% assign text = parts[2] %}
47+
<li class="toc-level-{{ level }}">
48+
<a href="#{{ id }}">{{ text }}</a>
49+
</li>
50+
{% endfor %}
51+
</ul>
52+
</div>
53+
{% endif %}

assets/main.scss

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,23 @@ img {
4040
/* Spacing after posts */
4141
.post-list article {
4242
margin-bottom: 2em;
43-
}
43+
}
44+
45+
.table-of-contents {
46+
background: #f8f9fa;
47+
border: 1px solid #e9ecef;
48+
border-radius: 6px;
49+
padding: 1.5rem;
50+
margin-bottom: 2rem;
51+
}
52+
53+
.table-of-contents h3 {
54+
margin-top: 0;
55+
margin-bottom: 1rem;
56+
font-size: 1.1rem;
57+
color: #6c757d; /* Gray color */
58+
}
59+
60+
.table-of-contents ul {
61+
margin-bottom: 0;
62+
}

0 commit comments

Comments
 (0)