```markdown
# Forest elephants
## Introduction
In this section, we discuss the lesser known forest elephants.
## Habitat
Forest elephants do not live in trees but among them.
```
```html
Forest elephants
Introduction
In this section, we discuss the lesser known forest elephants.
Habitat
Forest elephants do not live in trees but among them.
```
However, this approach had pretty complicated issues involving section nesting and the fact that we didn't have any control over its output other than by patching it. So, I decided to opt for a home-grown solution.
### The concept of "jurisdictions"
The naming is interesting but I felt like it was the most intuitive to me. Basically, I created a system that assigns each heading a "territory" that extends from its position to the start of the next heading (or the end of the document):
```javascript title="src/components/TOCHeader.astro" startLineNumber={123}
function buildHeadingJurisdictions() {
headingElements = Array.from(
document.querySelectorAll('.prose h2, .prose h3, .prose h4, .prose h5, .prose h6')
)
jurisdictions = headingElements.map((heading, index) => {
const nextHeading = headingElements[index + 1]
return {
id: heading.id,
start: heading.offsetTop,
end: nextHeading ? nextHeading.offsetTop : document.body.scrollHeight
}
})
}
```
1. First, we collect all heading elements (`