To answer your original question…
layouts/_default/_markup/render-heading.html
{{ if eq .Level 2 }}
{{ printf "<!-- end-chunk -->" | safeHTML }}
{{ printf "<!-- begin-chunk data-anchor=%q -->" .Anchor | safeHTML }}
{{ end }}
<h{{ .Level }} id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }}</h{{ .Level }}>
template
{{ range (findRE `(?s)<!-- begin-chunk.*?(?:<!-- end-chunk -->|$)` .Content) }}
{{ $anchor := replaceRE `(?s).+data-anchor="(.+?)".+` "$1" . }}
{{ $section := replaceRE `(?s)<!-- begin-chunk.+?-->(.+?)(?:<!-- end-chunk -->|$)` "$1" . }}
{{/* Remove leading and trailing newlines. */}}
{{ $section = trim $section "\n" }}
<div id="{{ $anchor }}">
{{ $section | safeHTML}}
</div>
{{ end }}
This produces something like...
<div id="section-1">
<h2 id="section-1">Section 1</h2>
<p>This is <strong>section</strong> 1.</p>
<p>This is section 1.</p>
<h3 id="section-11">Section 1.1</h3>
<p>This is <strong>section</strong> 1.1.</p>
<p>This is section 1.1.</p>
<h3 id="section-111">Section 1.1.1</h3>
<p>This is <strong>section</strong> 1.1.1.</p>
<p>This is section 1.1.1.</p>
</div>
<div id="section-2">
<h2 id="section-2">Section 2</h2>
<p>This is <strong>section</strong> 2.</p>
<p>This is section 2.</p>
</div>
<div id="section-3">
<h2 id="section-3">Section 3</h2>
<p>This is <strong>section</strong> 3.</p>
<p>This is section 3.</p>
</div>