Modify Table of Contents to only take certain headings

Is it possible to tweak {{ .TableOfContents }} to only pull from <h3> and <h4> tags so that I don’t get such a deeply nested ul > li > ul > li etc? I ask because I am trying to move my new organization (and new web team) from an Office/Word–based workflow to a .md one. Because we have multiple web properties, we have standardized on body copy using ### and #### for headings and subheadings within an article body, with most of the output h1s, etc pulled from yaml. Is it possible to extend the {{ .TableOfContents }} this way so that it works with my current workflow?

As far as I know is it not possible to change the template of {{ .TableOfContents }} nor you can influence it’s generation. However, you can exclude some headings in the HTML with some clever use CSS rules.

this worked well for my needs

{{ .TableOfContents | replaceRE "<ul>[[:space:]]*<li>[[:space:]]*<ul>" "<ul>" | replaceRE "</ul>[[:space:]]*</li>[[:space:]]*</ul>" "</ul>" | safeHTML }}

along with this css

nav > ul > li {
  list-style: none;
}

To turn off specific children you can use css child selectors to target the level you want to turn off then just display: none; to hide them.

For example, I only wanted to show the top level links and hide the rest. This was achived with the following css:
nav > ul > li > ul { display: none; }

1 Like

So, I was following the Brent tip (using replaceRE) in a shortcode to show the table of contents in the middle of some posts. But I don’t know what happened after 0.55, replaceRE isn’t working anymore with .Page.TableOfContents.

Right now I’m using a dirty hack with CSS (visibility: hidden for the first ul li), but I really would like to have the previous behavior working again.