Skip to content

Commit

Permalink
Make sure sidebar toc on print page HTML also respects TOC depth para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
timvink committed Sep 13, 2021
1 parent eb2fdc2 commit d4d4fe3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mkdocs_print_site_plugin/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,26 @@ def get_toc_sidebar(self) -> TableOfContents:
toc_link = update_toc_item(page_key, toc_link)
toc += [toc_link]

# Make sure the sidebar ToC also respects
# the plugin's toc_depth parameter
toc = clear_children_up_to(self.plugin_config.get("toc_depth"), toc)

return TableOfContents(toc)


def clear_children_up_to(depth, items):
"""
Filters items in sidebar table of contents to a given depth.
"""
for toc_item in items:
# note the -1 is there because of the hack on levels in update_toc_item()
if toc_item.level == depth - 1:
toc_item.children = []
else:
toc_item.children = clear_children_up_to(depth, toc_item.children)
return items


def update_toc_item(page_key: str, toc_link: AnchorLink) -> AnchorLink:
"""
Updates a AnchorItem to work on the print page.
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/projects/basic/mkdocs_toc_depth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
site_name: Test

plugins:
- print-site:
toc_depth: 1

0 comments on commit d4d4fe3

Please sign in to comment.