Why TOC is not working?

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 %}}

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

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

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

Cheers.

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 %}}

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

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.

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?

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.

I have the same problem as mentioned here: Why TOC is not working?

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?

1 Like

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.

Ok - Thanks for the update

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

A post was split to a new topic: Shortcode headings not showing in TOC