Skip to content

Commit fd7ab3f

Browse files
committed
add tocbot tables of contents to posts
1 parent 89e4537 commit fd7ab3f

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

post.hbs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
<div class="{{post_class}}">
66
{{#if feature_image}}
7-
<img src="{{img_url feature_image size="m"}}" class="profile-image">
7+
<img src="{{img_url feature_image size="m"}}" class="profile-image">
88
{{/if}}
99
<h1>{{title}}</h1>
1010
<br>
11-
{{content}}
11+
12+
<aside class="gh-sidebar"><div class="gh-toc"></div></aside>
13+
<div class="post-content">
14+
{{content}}
15+
</div>
1216
<p><em>{{date published_at format="MMMM DD, YYYY"}}</em></p>
1317
<br>
1418
</div>
@@ -21,4 +25,4 @@
2125

2226
{{#contentFor 'scripts'}}
2327
<script src="{{asset 'dist/post.js'}}" defer></script>
24-
{{/contentFor}}
28+
{{/contentFor}}

src/css/app.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222

2323
/* Vendor */
2424
@import './vendors/ghost.css';
25+
@import '../../node_modules/tocbot/dist/tocbot.css';

src/css/pages/post.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@
3838
line-height: 1.25rem;
3939
margin-bottom: 0.5em;
4040
}
41+
42+
/* tocbot stuff */
43+
.gh-toc > .toc-list {
44+
position: relative;
45+
margin-bottom: 1rem;
46+
}
47+
48+
.toc-list {
49+
overflow: hidden;
50+
list-style: none;
51+
}
52+
53+
.toc-list-item {
54+
margin-bottom: 5px;
55+
}
56+
4157
@media (min-width: 400px) {
4258
.post {
4359
width: 95%;
@@ -64,4 +80,20 @@
6480
.post {
6581
width: 800px;
6682
}
83+
84+
.gh-sidebar {
85+
position: absolute;
86+
left: 0;
87+
}
88+
89+
.gh-toc {
90+
position: sticky;
91+
top: 4vmin;
92+
max-width: calc((100vw - 800px) / 2);
93+
padding-right: 2rem;
94+
}
95+
}
96+
97+
.gh-toc .is-active-link::before {
98+
background-color: var(--ghost-accent-color);
6799
}

src/js/post/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
// Ship JS only active on post pages for better performance
2+
import tocbot from 'tocbot';
3+
4+
tocbot.init({
5+
// Where to render the table of contents.
6+
tocSelector: '.gh-toc',
7+
// Where to grab the headings to build the table of contents.
8+
contentSelector: '.post',
9+
// Which headings to grab inside of the contentSelector element.
10+
headingSelector: 'h2, h3, h4',
11+
// Ensure correct positioning
12+
hasInnerContainers: true,
13+
});

0 commit comments

Comments
 (0)