There are many ways to implement your required, but here's what you can try.
- Create a section-container component (sling:superResourceType parsys or responsive-grid).
- The section-container will allow dialogue configurations to include a title & title header level.
- The section-container allows content components be added in its content body.
- Create a TOC component;
- The TOC component with one to one relationship with a Sling Model.
- The TOC component will have logic to search for all section-container components configured on the page and display the title & title header level properties.
The page will only be constructed with the section-container component. While the TOC component sites on top of the section-container component.