Create a Table of Contents automatically
2016년 8월 18일에 게시됨
Much like the author of this article, our company needs a way to automatically generate a table of contents for large articles. After finding out we can use jQuery, I put together a script that will automagically create a tiered table of contents based on the Header tags used in the article.
All you need to do is paste the Javascript in this page into the Javascript section of your Articles:
Click on the JS tab, then paste in the code at the end:
That should do it.. as long as you've used proper heading tags (H1, H2, etc), a Table of Contents will be created any time you load the article.
You can see a live example here: http://codepen.io/ngalluzzo/pen/kXqyVQ?editors=1010
3
55
댓글 55개
Sara Anthony
Do you have any updated code for this? I only found one from this article and some things seem to be outdated. Thank you!https://support.zendesk.com/hc/en-us/community/posts/4408867942298-Tip-How-to-make-a-Table-Of-Contents-TOC-For-Article-Template
0
Michael Clarke
This thread has been super helpful, so thanks everyone who has contributed.
Like 405905032713, I'm also wanting to add a title to the TOC - Is someone able to point us in the right direction?
0
Anum Awan
Wow thanks 414774449873 that works really well.
I have my TOC working with nested bullet points for each heading level. Two things I'm curious if anyone knows:
Thank you.
0
Jordan Dayton
454757487 I wish I could share our community link but it's a private comm 😞
I do have some good news though... I found some script that adjusts the top of the viewport for anchor links, and it's working marvelously. I did adjust the "100" to "80" and it's perfect for us. Here's the js snippet:
// The function actually applying the offset function offsetAnchor() { if (location.hash.length !== 0) { window.scrollTo(window.scrollX, window.scrollY - 100); } } // Captures click events of all <a> elements with href starting with # $(document).on('click', 'a[href^="#"]', function(event) { // Click events are captured before hashchanges. Timeout // causes offsetAnchor to be called after the page jump. window.setTimeout(function() { offsetAnchor(); }, 0); }); // Set the offset when entering page with hash present in the url window.setTimeout(offsetAnchor, 0);
Source: https://stackoverflow.com/a/17535094/6084758
btw 405905032713 ⬆︎ that should help you! Also, I'm not seeing 1.1 or 1.11, etc. we see an "unordered list" in our community. I wonder if you're seeing numbers as a result of custom styling in your community?
0
Anum Awan
Experiencing the same issue as Jordan with the viewport being below the linked heading.
I'm also wondering what part of the code creates the numbering (1, 1.1, 1.1.1). I'd like to remove the numbering but keep everything else as is.
0
Vlad
Hey Jordan, regarding this issue "but the viewport of the screen is about 30 pixels below the actual header." - it could be fixed with pure CSS (padding-top and negative margin-top).
It's really hard to tell from this point why the TOC doesn't work from the 1st visit of the page; maybe it would help if you can share your HC URL so I can take a closer look. Thanks!
0
Jordan Dayton
In addition to my last question (previous post) I am just now noticing that the table of contents does not render/display when I visit a page. However, when I press the "refresh" button on that page, the TOC then appears. Any idea what I could change, in the code, to make the TOC render immediately, the first time the page loads? 454757487?
0
Jordan Dayton
in response to 454757487's post here: https://support.zendesk.com/hc/en-us/community/posts/213641488/comments/360001878568
This was the only code snippet I could get to work for our instance of ZenDesk. Thank you for that!
Now I've noticed that when we click one of the links in the TOC it takes us to the respective heading, but the viewport of the screen is about 30 pixels below the actual header. (see the GIF below).
20980975327 posted a link to her Guides here and as I looked at the Enalyzer TOC it works perfectly... taking you right to the respective header... placing it perfectly at the top of the screen viewport.
What modifications could I add to the code below, to adjust the script to make the anchor have the header appear at the top of the viewport?
0
Paul DISABLED LaBarbera
Hey Ami, yes that is the case. I suggest not wrapping the H2s in divs. If you need to add divs within content, just add it around the <p>s inside the H2/H3s
This solution works in most ZD environments, but if you have a very customized theme it may not.
0
Ami Dewar
I've noticed that this code breaks if I wrap the H2 with a div or section. The toc only shows the first H2 but none of the subsequent H2s. Has anyone else experienced this?
0
로그인하세요.