diff --git a/docs/options.md b/docs/options.md index a83af81..4868c58 100644 --- a/docs/options.md +++ b/docs/options.md @@ -9,6 +9,7 @@ plugins: print_page_title: 'Print Site' add_table_of_contents: true toc_title: 'Table of Contents' + toc_depth: 6 add_full_urls: false enumerate_headings: true enumerate_figures: true @@ -29,6 +30,9 @@ plugins: `toc_title` : Default is `'Table of Contents'`. When `add_table_of_contents` is set to `true` this setting controls the name of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. +`toc_depth` +: Default is `6`. When `add_table_of_contents` is set to `true` this setting controls the depth of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. + `add_full_urls` : Default is `false`. When printing a page, you cannot see the target of a link. This option adds the target url in parenthesis behind a link. diff --git a/mkdocs.yml b/mkdocs.yml index 4ad6753..06a2080 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -13,6 +13,8 @@ plugins: add_full_urls: false add_table_of_contents: true toc_title: "Table of Contents" + toc_depth: 2 + add_full_urls: false enumerate_headings: true enumerate_figures: true add_cover_page: true @@ -72,7 +74,7 @@ markdown_extensions: - pymdownx.tilde - pymdownx.smartsymbols - toc: - permalink: true + permalink: ↵ extra_javascript: - javascripts/config.js diff --git a/mkdocs_print_site_plugin/js/print-site.js b/mkdocs_print_site_plugin/js/print-site.js index 80cdfb8..8e38069 100644 --- a/mkdocs_print_site_plugin/js/print-site.js +++ b/mkdocs_print_site_plugin/js/print-site.js @@ -26,6 +26,9 @@ function generate_toc() { var current_section_depth = 0; var inserted_padding_row = false; + // Extract table of contents depth + var toc_depth = document.getElementById("print-page-toc").getAttribute("data-toc-depth") + for (var i = 0; i < toc_elements.length; i++) { @@ -62,6 +65,11 @@ function generate_toc() { tag = el.tagName tag_level = tag.substring(1) + // print-site-plugin has a setting to control TOC depth + if ( tag_level > toc_depth ) { + continue; + } + while (tag_level > current_heading_depth) { current_heading_depth++; ToC += "" + + ToC += "" - document.querySelectorAll("#print-page-toc nav")[0].insertAdjacentHTML("beforeend", ToC); + document.querySelectorAll("#print-page-toc nav")[0].insertAdjacentHTML("beforeend", ToC); } \ No newline at end of file diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index b475ca7..5f1ff8d 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -23,6 +23,7 @@ class PrintSitePlugin(BasePlugin): ("print_page_title", config_options.Type(str, default="Print Site")), ("add_table_of_contents", config_options.Type(bool, default=True)), ("toc_title", config_options.Type(str, default="Table of Contents")), + ("toc_depth", config_options.Type(int, default=6)), ("add_full_urls", config_options.Type(bool, default=False)), ("enumerate_headings", config_options.Type(bool, default=False)), ("enumerate_figures", config_options.Type(bool, default=False)), @@ -34,6 +35,10 @@ class PrintSitePlugin(BasePlugin): def on_config(self, config, **kwargs): + # Check valid table of contents depth + assert self.config.get("toc_depth") >= 1 + assert self.config.get("toc_depth") <= 6 + # Because other plugins can alter the navigation # (and thus which pages should be in the print page) # it is important 'print-site' is defined last in the 'plugins' diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index dad0a5d..e65b5cd 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -135,7 +135,7 @@ def _explain_block(): def _toc(self): return f"""