Recent searches
No recent searches
Tip: How to make a Table Of Contents (TOC) For Article Template
Posted Sep 01, 2021
Prerequisite:
File Name: article_page.hbs, style.css, script.js file
Time To Read: Max 2 min
Time To Embed: Max 4 min
Here's a way to create a table of contents in your Article template, that ensures subheadings will be properly indented instead of aligned with the main headings
2 min read to learn how to customize the theme.
You can use the free plugin by Nikhil Dabas. You can embed easily in your theme, see the below steps:
1). Download the plugin and unzip.
2). Upload the JS files into your assets folder of your Help Centre. See the below screenshot.
3). Now, link the jquery.toc.min.js file and the jQuery library onto your document_head.hbs file, the plugin is jQuery based.
4). Call the TOC function on your script.js file at the bottom area under the Document function.
5). At the end, add the div to show the TOC on the article template.
The output is:
Thanks
3
50 comments
Jaïs Pingouroux
Hello,
Thanks for sharing. I did a similar thing using the following code:
- in script.js:
- in style.css:
- in article_page.hbs:
I found this last part great because it actually triggers the TOC section on articles ONLY if I had the show_toc label in the article, which is a customization I wanted.
0
Ifra Saqlain
Thanks I think you read this one also to add the title above TOC list items:
https://support.zendesk.com/hc/en-us/community/posts/1500000064241-Using-Section-Headings-to-create-a-Table-of-Contents
0
Mark Glinski
Ifra Saqlain the screen shots for steps 2 and 3 are identical. Is that intentional?

0
Ifra Saqlain
Hello Mark, thanks to ping, that's not intentionally, there should have been the document_head template screenshot.
0
Mark Glinski
Thanks, Ifra Saqlain! Got the dynamic TOC working with the jQuery plugins.
One question: I added an "In this article" heading. I modified the Javascript snippet to only pull <h2> headings. Is there a way to hide the "In this article" heading if there are no <h2> headings in the article?
0
Ifra Saqlain
Hello Mark, you can add a class-name in your h3 heading tag: toc-title, and check the length of li in th ul, then hide the toc-title.
Solution:
1). Add class-name in h3.
2). Check the li length in the ul.
3). Now test your TOC and if any issue please do let me know :)
Thanks
0
Administrator
Thank you for the solution. Works fine.
I'm trying set TOC as transparent callout.
Add this code:
and
If there is no heading in article TOC is hide, but borders of this callout are still there. What should I do?
0
Ifra Saqlain
hii, because you forgot to add a dot for the class in your script code:
Your code:
Fixed: Apply this.
1
Administrator
Indeed. Thank you. And the last question:
how can I change:
to hide TOC if article has less than 2 headings?
0
Ifra Saqlain
hey Jakub :)
Use this script code for your query:
1
Administrator
Many thanks!
Really appreciate your help :)
0
Administrator
Hello,
TOC works nice except for one thing - I am wondering is this happens only in my HC?
Clicking on the link takes you to the specific header, but a few lines below not to head of section.
For example - click on 3rd link from TOC: http://gmsystem.zendesk.com/hc/pl/articles/4417171125905-Jak-wypożyczyć-licencję-Solid-Edge-
As you can see it takes you to proper section but first lines are hidden behind header.
How can I fix that?
0
Ifra Saqlain
Hello Jakub Kręcisz, it is cause of your sticky header, the header is hiding those few lines.
See the below screenshot:
Your sticky header:
When I removed position: fixed from the CSS:
Solution:
You can fix this issue by adding the given code at the end of your script file inside the DOM function
Thanks
Team
Also, click on the given link :)
http://ifrasaqlain.com/introduction/
1
Administrator
Thank you! I did as you wrote.
Is this possible to limit this only for article page?
I would like to minimalize jump effect on other pages, i.e. main page where row with blocks is shifted with specific pixels amount:
position"0":
position"scrolled down":
0
Ifra Saqlain
Add template name on article page.
Update the previous script code:
1
Administrator
Ifra,
really, really appreciate your help - thank you so much!
0
Ifra Saqlain
:)
0
Mickey Farmer
This is nothing short of amazing. Thank you so much Ifra Saqlain for this guide!
0
Ifra Saqlain
:)
0
Anh Le
I did these but it doesn't work: https://support.heroesempires.com/hc/en-us/articles/5260022672665
No TC shows.
And please post the code, don't post the screenshot :)
0
Ifra Saqlain
Anh Le, I think you didn't add jquery.toc.min file to your head file.
Error:
Solution:
First you need to download the plugin.
and then unzip that,
you will get jquery.toc.min
upload it to your HC assets folder.
import it to your document_head.hbs file.
Your issue would be solved :)
Thanks
0
Anh Le
Of course, I did.
0
Anh Le
Ifra Saqlain
0
Ifra Saqlain
Anh Le :), do the following:
i). Go to your document_head.hbs file.
ii). Update the sequence of CDNs.
iii). Currently, your TOC CDN is above jQuery-v3.5.1, it should be below this version of jQuery or you can move your jQuery-v3.5.1 CDN ust below the jQuery-v3.6.0.
Currently:
After updating:
iv). Second thing is, you don't have the class name article-body.
You'll need to add class name with the markdown.
OR,
you can change the class name in the script code.
Now, test and let me know.
1
Anh Le
Thank you Ifra Saqlain the theme developer loaded the js file end of that file. I removed it and it works.
However, the format is ugly; any suggestions? https://support.heroesempires.com/hc/en-us/articles/5260022672665-Tutorials
Btw, do you plan to have the native TOC for Zendesk article?
0
Ifra Saqlain
Hi Anh Le :),
move the script files to the footer.hbs file and css files keep into document-head file.
script (JS) files to the footer file.
CSS files to the document file.
and then test, are all the functions working fine as previous or not?
And,
You can post this question in the Guide Feedback. I'm only a moderator so can't tell about the roadmaps :)
Thanks
0
Anh Le
Ifra Saqlain I don't get it. What are the new code for? Can't you share the code instead of taking the screenshot and make me retype those codes?
0
Ifra Saqlain
Anh Le,
I answered your query
Move script files document_head.hbs file to footer.hbs file.
Move your script files to the footer.hbs file.
script file: <script src="..."></script>
Template name: footer.hbs
And, keep your CSS files to document_head.hbs file as it is.
CSS files: <link href="..." rel="stylesheet" type="css/text" />
Template Name: document_head.hbs
0
Anh Le
I moved these codes from document_head.hbs to footer.hbs:
But nothing changed.
0
Ifra Saqlain
Anh Le, I think you don't need to do anything else, forget about formatting, move our jquery CDN and TOC to the document_head.hbs file as it is.
See, you didn't move all the script files to the footer file.
You only moved jquery CDN and TOC.
Now the error is:
Okay, move you jQuery and TOC to the document_head.hbs file as it was. Then the final results would be:
Your TOC is showing:
0