Hook a shortcode/template before every first second level heading

With WordPress, I have this nifty code that hooks anything after a certain number of paragraphs. I want to replicate the same functionality with Hugo, but instead hook before the first second level heading. For this one site, I want to hook Hugo’s built in Table of Contents before every first <h2>abcd</h2>. I request if anyone more knowledgeable can help me with this?

You’ll need to use a regex replacement.

In your template, do this:

{{ replaceRE `(?s)(.*?)(<h2.*)` (print "$1" .TableOfContents "$2")  .Content | safeHTML }}

Instead of this:

{{ .Content }}
1 Like

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