Tip: How to make a Table Of Contents (TOC) For Article Template
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 learnhow to customize the theme.
You can use the free plugin byNikhil 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 yourdocument_head.hbsfile, 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
-
Hello,
Thanks for sharing. I did a similar thing using the following code:
- in script.js:
!function(a){"use strict";var b=function(b){return this.each(function(){var c,d,e=a(this),f=e.data(),g=[e],h=this.tagName,i=0;c=a.extend({content:"body",headings:"h1,h2,h3"},{content:f.toc||void 0,headings:f.tocHeadings||void 0},b),d=c.headings.split(","),a(c.content).find(c.headings).attr("id",function(b,c){var d=function(a){0===a.length&&(a="?");for(var b=a.replace(/\s+/g,"_"),c="",d=1;null!==document.getElementById(b+c);)c="_"+d++;return b+c};return c||d(a(this).text())}).each(function(){var b=a(this),c=a.map(d,function(a,c){return b.is(a)?c:void 0})[0];if(c>i){var e=g[0].children("li:last")[0];e&&g.unshift(a("<"+h+"/>").appendTo(e))}else g.splice(0,Math.min(i-c,Math.max(g.length-1,0)));a("
").appendTo(g[0]).append(a("").text(b.text()).attr("href","#"+b.attr("id"))),i=c})})},c=a.fn.toc;a.fn.toc=b,a.fn.toc.noConflict=function(){return a.fn.toc=c,this},a(function(){b.call(a("[data-toc]"))})}(window.jQuery);
if ($("#toc").length>0){
$("#toc").toc({content:"div.article-body"});
}- in style.css:
.toc-list ul, .toc-list ol {
list-style-type: revert;
}
.toc-list ul li, .toc-list ol li {
margin: 0px 0 0px 10px;
}
.toc-list ul li a {
border-radius: 4px;
color: $text_color;
display: block;
font-weight: 300;
padding-left: 10px;
/*margin-bottom: 10px;*/
}
.toc-list ul li a:hover {
background-color: $brand_color;
color: $brand_text_color;
text-decoration: none;
}- in article_page.hbs:
{{#each article.labels}}
{{#is identifier "show_toc"}}
{{dc 'in_this_article'}}
{{/is}}
{{/each}}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.
-
Thanks I think you read this one also to add the title above TOC list items:
-
Ifra Saqlainthe screen shots for steps 2 and 3 are identical. Is that intentional?
-
Hello Mark, thanks to ping, that's not intentionally, there should have been the document_head template screenshot.
-
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
headings. Is there a way to hide the "In this article" heading if there are no
headings in the article?
< div class = "article-content">
< div class = "article-body">In this article:
{{#if attachments}}
< div class = "article-attachments">
38 Comments