A functional table of contents (TOC) is easy for users to navigate through the long article, but Ghost did not provide a nice out-of-the-box solution. This post is to show you how to build a nice table of contents like you see on the right with easy tweaks.
Besides, for those who are interested in building the blog, the prior 3 parts are the go-to reference:
1 Problem Statement
Ghost in fact introduces a simply table of contents in the official tutorial:
The TOC can be inserted into your theme, but in most cases they are not user friendly and pretty:
Everything is underlined
It is too close to the content
It is overlapped with meta data like author, tags
It is overlapped with wide or full images
2 Fix
Suppose that you have followed the example shared by Ghost official document, then the default.hbs and post.hbs have been updated and the Tocbot code has been added. Please do it if you have not so that at least there is a TOC no matter how bad it is :p
Now what we need to fix is the style of the TOC by editing the code in default.hbs.
2.1 Style
Firstly, go to the default.hbs file, below CSS code is inserted above {{ghost_head}} to set proper styles.
z-index is used to control the position of each element on the Z axis, in the above setting, images are placed on the front and the main content and TOC are behind it so that TOC does not float over wide or full images. Below snippet is added to remove the underline of text in the TOC:
Then, grid-column: main-end / wide-end is used to put the TOC on the right, please note that if you should use grid-column: wide-start / main-start if you still expect the TOC on the left.
Next, because the TOC is now on the right, and pretty close to the content, left: 30% is used to add margin so that it is a bit distant to the content. If your TOC is on the left, you should consider tweaking the parameter right with proper margin.
Also, below code is used to hide the TOC on small devices as the TOC is not able to hover side by side and so it is unnecessary.
2.2 Function
Now go to the end of the default.hbs and below code is inserted above {{ghost_foot}}.
orderedList: false is added to remove the order a, b, c, d in the TOC, as you can order the headers yourself if necessary.
3 Conclusion
With above simple tweak, the issues of the primitive TOC have been fixed perfectly, hope it helps you to prettify your Ghost website with a functional TOC.
💡
Above tweaks might not work for all themes, but please follow the ideas to tweak per your need.