Create a Table of Contents automatically

53 Kommentare

  • Ariana Metzler

    Hi Vladan,

    Thanks for the response. A good example of an article in our Help Center with multiple levels of headers can be found here:

    [Link removed after the issue was resolved]

    0
  • Vladan Jovic
    Community Moderator

    Thanks for the feedback! Let us know if any further help needed ;)

    0
  • Angela Frey

    This script hyperlinks to the header name, which means that if I cross-link from article A to a subsection in article B, if that header name is ever updated the cross-link breaks.

    Is it possible to hyperlink to the header ID instead? For example:

    Header Name hyperlink:

    ...hc/en-us/articles/360000297868#ConfigureTrustedSender

    Header ID hyperlink:

    ...hc/en-us/articles/360000297868#h_97516510121537301054444

    FWIW, you can find the header ID while editing an article - in Article A, select text, insert link, choose a header in the dropdown, then look at the URL. I was using this method to manually create Tables of Content, which I could then refer to when creating a cross-link from another article to the subsection (just copy the URL). 

    0
  • Vladan Jovic
    Community Moderator

    Hey Iris, glad to hear it works!

    Yes, it is doable to have it on the right or left side but that requires some design/CSS changes in the layout.

    If you have someone in your team familiar with CSS he/she can do that for sure.

    0
  • Duane Knudsen

    Vladan, thank you very much for sharing. I've incorporated the code into the article_page.hbs template of our HC and it works great. I had played around a bit with the JS version but couldn't get it to run properly (could be due to a highly customized HC with category accordians, etc). 

    Thanks again for a fantastic ToC solution!

    0
  • Michael H

    Am I missing something with implementing this code?

    Reading the thread, and taking the last post from @Tim, I understand the totality of the implementation path being this:

    1. In Guide, go to /theming/workbench
    2. Select your theme, and press view theme.
    3. press Edit code.
    4. Edit script.js, and add the code to the end of the file. Save changes.

    And that's it - it should display right?

    Having followed these steps on my Guide instance, running the Copenhagen theme, I don't get anything let alone the change i'm expected.

    Here's a page from my Guide instance which I would hope gets this auto TOC:
    https://help.wafresh.com.au/hc/en-au/articles/360007488692-What-happens-if-an-item-is-out-of-stock-or-unavailable-

    Appreciate any help or feedback to understand where I'm going wrong.

    0
  • Tim Capone

    @Michael H

     

    You want to put it in the article_page.hbs file, not script.js

    0
  • Paul LaBarbera

    @Tim Capone

    Does it matter where in article-Page.hbs you put this script? I've tried adding this to a default Copenhagen theme, and I'm not seeing anything.

     

    I used Vladan's script and it worked fine. 

    0
  • Tim Capone

    @Paul LaBarbera

    I put my code on the bottom of the script.

     

    It's working on Firefox, Chrome, Internet Explorer, and Edge for me.

    0
  • Stephen A Kairys

    Hi,

    Per Tim's comment from last week:

    >> It's working on Firefox, Chrome, Internet Explorer, and Edge for me.

    Does anyone know if this code is browser-dependent? A former colleague who contributed to this thread (Nick Smolnehy) implemented this feature here. I believe he made some additions to the original code.

    Using Chrome, it's been working great, but I started using Firefos yesterday, and:

    1. The TOC itself does not display.
    2. The headings throughout the document are not auto-numbered  e.g

    2.1.3 Modify Vendor

    appears as

    Modify Vendor

    Thanks.

     

     

     

    0
  • Brett Bowser
    Zendesk Community Team

    Thanks for taking the time to share this with everyone Matthew!

    0
  • Tyler Abate

    Can a community team member assist me with adding a table of contents to my knowledge base? The above does not seem to work in 2020. Also, I am using H2 for my main headers.

    0
  • Brett Bowser
    Zendesk Community Team

    Hey Tyler,

    This would require some custom code in your Guide theme which is not something we can help with from our end. I would recommend taking a look at the following documentation we have available:

    If you don't have the necessary resources available to assist with customizing your Help Center, we do have a Professional Services team that can assist at an additional cost. This is something you'll want to discuss with your account manager so if you're interested, I'm happy to get you in touch if needed.

    Cheers!

    0
  • Andrea Coloma

    I added Lily Svetnik code and it worked but is there any way I can make this into side navigation and not the first thing on the article - does that make sense?

     

    Here is a link to an article: https://helpcenter.enalyzer.com/hc/en-us/articles/360012608599#HowdoIcreateateam?

    0
  • Ami Dewar

    Vladan - This works great in Chrome and Safari, but in Firefox the li elements are not created - have you noticed this? 

    0
  • Vladan Jovic
    Community Moderator

    Hey Ami, I didn't notice, to be honest. Do you have it on live and can share the URL with us so I can check? 

     

    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
  • Paul 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
  • Jordan Dayton

    in response to Vladan Jovic'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). 

    Andrea Coloma 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
  • 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? Vladan Jovic?

    0
  • Vladan Jovic
    Community Moderator

    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
  • 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
  • Jordan Dayton

    Vladan Jovic 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 Anum Awan ⬆︎  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

Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.

Powered by Zendesk