# Why TOC is not working?

**URL:** <https://discourse.gohugo.io/t/why-toc-is-not-working/11388>\
**Category:** support\
**Created:** [April 5, 2018, 12:48pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388 "2018-04-05T12:48:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [April 5, 2018, 12:48pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/1 "2018-04-05T12:48:24Z")

</div>

I want to have a table of contnents on the right sidebar. I put a  
shortcode in toc.html

What I do made wrong?

```
{{ if .Params.toc }}
  {{ .TableOfContents }}
{{ end }}

/content/_index.md
    ---
    title: "Handbook"
    date: 2018-01-03T18:48:05+01:00
    toc: true
    draft: false
    menu:
      main:
         name: Handbook
         url: /handbook/
         weight: 2
    ---

    {{% section class="item-1 outline" %}}
    {{% divmd class="item-1 card-nb" %}}
    # Handbook

    ---
    {{% /divmd %}}

    {{% divmd class="item-8A card-nb" %}}
    ## General

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula 
    eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient 

    ## Why handbook first

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula 
    eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient 

    ### Advantages

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula 
    eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient 

    {{% /divmd %}}

    {{% divmd class="item-4D card-toc" %}}

    ## Table of contents

    {{% .TableOfContents %}}
    {{% /divmd %}}
    {{% /section %}}
```

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 5, 2018, 2:12pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/2 "2018-04-05T14:12:31Z")

</div>

> [@Joerg](#):
>
> {{ if .Params.toc }}  
> {{ .TableOfContents }}  
> {{ end }}

If this is the code from your shortcode, it should read:

```auto
{{ if $.Page.Params.toc }}
  {{ $.Page.TableOfContents }}
{{ end }}

```

> [@Joerg](#):
>
> {{% .TableOfContents %}}

You call your shortcode using its basename from `layouts/shortcode/`  
It should look more like `{{% tableofcontents %}}`

Cheers.

---

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [April 5, 2018, 2:45pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/3 "2018-04-05T14:45:19Z")

</div>

Thank you for your help.

I wrote this lines in shortcode/tableofcontents.html  
But nothing happend.

```
{{ if $.Page.Params.toc }}
  {{ $.Page.TableOfContents }}
{{ end }}

```

this is the code in handbook/\_index.md

```
{{% divmd class="item-4D card-toc" %}} 

## Table of contents

{{% tableofcontents %}} 
{{% /divmd %}} 
{{% /section %}}
```

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 5, 2018, 3:05pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/4 "2018-04-05T15:05:32Z")

</div>

Do you have a repo somewhere so we can play this this?

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 5, 2018, 6:25pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/6 "2018-04-05T18:25:04Z")

</div>

Hey! I took a look. Turns out any every titles inside your `divmd` or your `section` shortcodes are ignored by the TOC generator…

You could start researching on that, not sure there is a work around… would be curious to know.

Cheers.

---

<div class="post-metadata">

**Author:** ![Joerg](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Joerg](https://discourse.gohugo.io/u/Joerg)\
**Post date:** [April 5, 2018, 6:50pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/7 "2018-04-05T18:50:02Z")

</div>

I tried to find a solution with divmd and without divmd. Nothing works. It seems like I have to create the table of contents by hand. I still have styleguide.md and manuals.md in the directory handbook next to \_index.md. How can I display the content as if this content is a single md file?

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 5, 2018, 7:02pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/8 "2018-04-05T19:02:29Z")

</div>

I assure you it works fine if you add a `# Title` outside of your shortcodes. I tried it myself. Every titles outside of the shortcodes were included in the TOC.

---

<div class="post-metadata">

**Author:** ![DirkC](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dirkc/32/8628_2.png) [@DirkC](https://discourse.gohugo.io/u/DirkC)\
**Post date:** [June 19, 2018, 3:34pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/9 "2018-06-19T15:34:06Z")

</div>

I have the same problem as mentioned here: [Why TOC is not working?](https://discourse.gohugo.io/t/why-toc-is-not-working/11388)

The table of contents is working fine - as long as the headers are outside a shortcode. Once they are in a short code enclosing the headers the {{ .TableOfContents }} remains empty.

My shortcode basically assigns a class to the paragraph:  
htmlclass:

```
{{ $contentclass := .Get 0 }}
<div class= {{$contentclass}} >
{{ .Inner }}
</div >

```

In markdown:

```
{{% htmlclass "content-body-section" %}}

## First H2
Loret Ipsum
{{% / htmlclass %}}

{{% htmlclass "content-body-section" %}}

## Second H2
Loret Ipsum
{{% / htmlclass %}}

## Third H2

```

In the TOC - only the “Third H2” is listed - the other ones are ignored.  
Am I doing something wrong or is this the default behaviour for TOC?

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 19, 2018, 8:39pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/10 "2018-06-19T20:39:15Z")

</div>

Listing headings from shortcodes in the TOC will be fixed as part of [a larger effort to improve how shortcode content is processed by our markdown parser](https://github.com/gohugoio/hugo/issues/4695#issuecomment-398529579).

---

<div class="post-metadata">

**Author:** ![DirkC](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dirkc/32/8628_2.png) [@DirkC](https://discourse.gohugo.io/u/DirkC)\
**Post date:** [June 19, 2018, 9:53pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/11 "2018-06-19T21:53:34Z")

</div>

Ok - Thanks for the update

---

<div class="post-metadata">

**Author:** ![matteocontrini](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/matteocontrini/32/8471_2.png) [@matteocontrini](https://discourse.gohugo.io/u/matteocontrini)\
**Post date:** [April 21, 2019, 9:54am UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/12 "2019-04-21T09:54:32Z")

</div>

> [@maiki](#):
>
> Listing headings from shortcodes in the TOC will be fixed as part of [a larger effort to improve how shortcode content is processed by our markdown parser](https://github.com/gohugoio/hugo/issues/4695#issuecomment-398529579)

Is that still the case? I’m getting the same behaviour in Hugo 0.55.x.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 30, 2019, 2:16pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/13 "2019-06-30T14:16:07Z")

</div>

Split this topic on June 30, 2019, 2:16pm UTC

A post was split to a new topic: [Shortcode headings not showing in TOC](https://discourse.gohugo.io/t/shortcode-headings-not-showing-in-toc/19428)

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 30, 2019, 5:35pm UTC](https://discourse.gohugo.io/t/why-toc-is-not-working/11388/14 "2019-06-30T17:35:54Z")

</div>

Closed on June 30, 2019, 5:35pm UTC


