Generate a Table of Contents of all posts

I would like to generate a Table of Contents for all posts on the site that I can attach to the Sidebar. Is this possible with Hugo?

YES, you can :wink:

roll you own

see

.Site.Pages

array of all content ordered by Date with the newest first. This array contains only the pages in the current language. See .Site.Pages .

.Site.RegularPages

a shortcut to the regular page collection. .Site.RegularPages is equivalent to where .Site.Pages "Kind" "page" . See .Site.Pages .

must range over one of this collections …

Thanks. I went for an alternative approach that uses the site section titles instead to make it shorter and cleaner. I am building an index with several chapters.

:warning: Not tested

Maybe something like that :

{{ define "subSections" }}
    <ul>
    {{ range .Sections }}
        <li>{{ .Title }}
        {{ template "subSections" . }}
    </li>
    {{ end }}
    </ul>
{{ end }}


{{ range .Site.Sections }}
            <a href="{{ .RelPermalink }}"><h4>{{ .Title }}</h4></a>
            {{ range .Sections }}
                <a href="{{ .RelPermalink }}"><h5 class="mb-0 title is-6">{{ .Title }}</h5></a>
                {{ template "subSections" . }}
            {{ end }}
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.