Determine, if ToC is longer than x listitems

I want to add some different styles to my ToC depending on its length. It doesn’t seem that the length is available anywhere in the document, so my idea™ is, that I take .TableOfContents and findRE through it to get the number of opening li tags. If that number is bigger than my x then I am fairly sure that there are more than x items in the list.

Something like this…

{{ (findRE "<li[(<\/li)]*" .Content).len }}

Is that the way to go? I assume there might be a better way based on counting the headlines in the document, but that too is not something that has some form of function in the templates available.

One last thing: Is whatever .TableOfContents returns cached in some form, so that it’s not calculated twice or should I appoint it to a variable and reuse?

It’s cached.

Currently you do need to do some string counting, e.g. strings.Count "<li>".

4 Likes

Much easier than a regexp.

By the way, the strings.count docs is broken, probably because of a pipe. I’ll send a PR in a bit.

3 Likes