Empty li item in toc is generated

An empty li is generated in the TOC. I have set the start level to 2 and end level to 3. Once i put h4 above h3 it generates an empty li tag in toc.

hugo.toml

[markup]
  [markup.tableOfContents]
    endLevel = 3
    startLevel = 2

my md content -

# Heading one 
## Heading two 
#### Heading four 
#### Heading four 
### Heading three

Since h4 is above h3, any specific reason it generates an empty li on the TOC.

image

link to public repo

just as a hint: As per convention you should not skip heading levels in HTML

But nevertheless: According to common mark spec that construct is legal and also the Goldmark playground renders that properly

I simplified the problem to

[markup]
  [markup.tableOfContents]
    startLevel = 1
    endLevel = 2

and

## Heading two
# Heading one

when we look at the {{ .Fragments .}} we see that

  • an empty entry is added for the missing level 1 heading before level2.
  • To indicate that this is not a real heading it’s level is set to 0.
  • same for your out of sync level four heading
{
  "Headings": [
    {
      "ID": "",
      "Level": 0,
      "Title": "",
      "Headings": [
        {
          "ID": "heading-two",
          "Level": 2,
          "Title": "Heading two",
          "Headings": null
        }
      ]
    },
    {
      "ID": "heading-one",
      "Level": 1,
      "Title": "Heading one",
      "Headings": null
    }
  ],
  "Identifiers": [
    "heading-one",
    "heading-two"
  ],
  "HeadingsMap": {
    "heading-one": {
      "ID": "heading-one",
      "Level": 1,
      "Title": "Heading one",
      "Headings": null
    },
    "heading-two": {
      "ID": "heading-two",
      "Level": 2,
      "Title": "Heading two",
      "Headings": null
    }
  }
}

Conclusion:

  • Hugo adds some “Empty levels” without proper level in the Fragments
  • .TableOfContents method just walks the Fragments and prints the empty entry

Guess we can call that a bug.

I understand that we should not skip the heading levels in HTML. But do we still call it as bug?

@jmooring any thoughts ?

This is a known issue that will probably not be addressed in the near future:
https://github.com/gohugoio/hugo/issues/7128

You can walk the headings yourself to build a TOC. See this example.

1 Like