Customise table of content

Hi everyone, I want to customise my table of contents, I’m using {{ .TableOfContents }} for generating toc. For most of the pages, I want to use the <h2> to <h3> tag as the heading for toc, But for some pages, I want to expand the depth to <h2> to <h4>, can we add some frontmatter in a page like expandToc: true (or similar) and use that to extend toc.

Maybe this can help

1 Like

This can be done with the .Fragments variable (since 0.111.0).

{{ $tocStartLevel := default 2 .Params.toc_start_level  }}
{{ $tocEndLevel := default 3 .Params.toc_end_level  }}
{{ $tocOrdered := false  }}
{{ .Fragments.ToHTML $tocStartLevel $tocEndLevel $tocOrdered | safeHTML }}

Override the levels in front matter.

toc_start_level: 2
toc_end_level: 4
1 Like

Where should we put this code? Should we put in partials toc.html?

Replace the {{ .TableOfContents }} with the code snippet above should be OK.

1 Like

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