diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index 0f8467b..1ca3442 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -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. diff --git a/tests/fixtures/projects/basic/mkdocs_toc_depth.yml b/tests/fixtures/projects/basic/mkdocs_toc_depth.yml new file mode 100644 index 0000000..4ef8f8b --- /dev/null +++ b/tests/fixtures/projects/basic/mkdocs_toc_depth.yml @@ -0,0 +1,6 @@ +site_name: Test + +plugins: + - print-site: + toc_depth: 1 + \ No newline at end of file