Search code examples
pythonpython-sphinxtoctree

Programmatically adding Nested TOC entries to Sidebar in Sphinx


I am trying to automatically add entries into the TOC sidebar based on the contents of a directory through a extension, and for some reason the TOC Node correctly applies an entry to the current section but it does not apply any additional sections if it is nested, such as a new index file that contains a TOC entry. Here's an image of what's happening:

Outcome

And the structure is like this. For clarity, each section is it's own file, not a sub-header within the same file.

Core Section
-section1
-section2
-section3
-section4
---nested1
---nested2
---nested3

If I navigate to the section4, I see the TOC node inside of it, but the sidebar does not reflect those nested ones. The theme I am using is the sphinx Book theme if that makes any difference.

Outside of organization looping through the files, this is the only thing to do with the actual TOC processing. It simply loops through a list of pre-processed files (giving them a formatted title) and then includes as expected.

entries = []
includefiles = []
for subfile in folder:
    entries.append([file.title, file.docname])
    includefiles.append(file.docname)
tocnode = addnodes.toctree(parent=self.env.docname, glob=False, entries=entries, includefiles=includefiles)

Solution

  • Turns out it WAS the Book theme. The sidebar HTML has a maximum depth of 4, so I had to overwrite that by making a copy of the file in _templates and modifying that depth. The code worked after that.