Skip to content

Commit

Permalink
Add toc_depth option, see #39
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Feb 22, 2021
1 parent 678001e commit 97d6734
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +74,7 @@ markdown_extensions:
- pymdownx.tilde
- pymdownx.smartsymbols
- toc:
permalink: true
permalink:

extra_javascript:
- javascripts/config.js
Expand Down
13 changes: 10 additions & 3 deletions mkdocs_print_site_plugin/js/print-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {


Expand Down Expand Up @@ -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 += "<ul class='print-site-toc-level-" + current_heading_depth + "'>";
Expand Down Expand Up @@ -96,9 +104,8 @@ function generate_toc() {

};

ToC +=
"</ul>" +
ToC += "</ul>"

document.querySelectorAll("#print-page-toc nav")[0].insertAdjacentHTML("beforeend", ToC);
document.querySelectorAll("#print-page-toc nav")[0].insertAdjacentHTML("beforeend", ToC);

}
5 changes: 5 additions & 0 deletions mkdocs_print_site_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_print_site_plugin/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _explain_block():
def _toc(self):
return f"""
<section class="print-page">
<div id="print-page-toc">
<div id="print-page-toc" data-toc-depth="{self.plugin_config.get("toc_depth")}">
<nav role='navigation' class='print-page-toc-nav'>
<h1 class='print-page-toc-title'>{self.plugin_config.get("toc_title")}</h1>
</nav>
Expand Down

0 comments on commit 97d6734

Please sign in to comment.