https://discuss.asciidoctor.org/TOC-placement-with-Awestruct-tp546p565.html
Stéphane,
This is one of the features I'm looking forward to having in Awestruct. The feature is actually more generic than just solving the TOC problem. When an AsciiDoc document is parsed by Asciidoctor, it is loaded into an object model (call it an AST or DOM). I think that object model should be available to the layouts when the page is being constructed. This would allow the sidebar file in the main layout to render just the table of contents from the object model. It would also allow us to put other content extracted from the document into the sidebar.
...the fact that this isn't yet implemented doesn't mean it's not possible today. I believe there is a way for the sidebar to discover which file path is currently being rendered. In that case, you can simply invoke Asciidoctor directly inside the sidebar (or in an extension as Jason mentioned) to load the file again, then just step to the TOC block and render it.
However, you look at it, just consider that you are treating the two locations on the page as two separate rendering events. The alternative is to place the TOC using JavaScript after the page has loaded (as we do on
arquillian.org) but personally I find that to be pure hackery. It is possible to hit an in-between solution by rendering the TOC in a hidden div in the content, then pull it into the sidebar using JavaScript. That way, the integrity of the TOC is maintained.
Here's how that last solution might work:
:toc:
:toc-placement: manual
[#toc-container.hide]
--
toc::[]
--
<script>
$('#sidebar').append($('#toc-container'));
</scirpt>
That's a reasonable solution to work with that won't require a ton of effort. Long-term, though, the best solution is to get Asciidoctor to emit the TOC as a secondary document into the sidebar region.
I hope that gets you started!
-Dan