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.
- Complete.
The page will only be constructed with the section-container component. While the TOC component sites on top of the section-container component.
JCR View:
/content/my-site/page/jcr:content/par/toccomponent
/content/my-site/page/jcr:content/par/sectioncontainercomponent0
/content/my-site/page/jcr:content/par/sectioncontainercomponent1
/content/my-site/page/jcr:content/par/sectioncontainercomponent2
/content/my-site/page/jcr:content/par/sectioncontainercomponent3