Skip to content

Commit

Permalink
Fix validation of heading 1 in a page, now only validate pages that a…
Browse files Browse the repository at this point in the history
…re actually in the navigation
  • Loading branch information
timvink committed Sep 15, 2021
1 parent 9b83cc7 commit 25bc443
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 9 additions & 7 deletions mkdocs_print_site_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mkdocs.utils import write_file, copy_file, get_relative_url, warning_filter

from mkdocs_print_site_plugin.renderer import Renderer
from mkdocs_print_site_plugin.utils import get_theme_name
from mkdocs_print_site_plugin.utils import flatten_nav, get_theme_name

logger = logging.getLogger("mkdocs.plugins")
logger.addFilter(warning_filter)
Expand Down Expand Up @@ -147,6 +147,7 @@ def on_nav(self, nav, config, files, **kwargs):

# Save the (order of) pages and sections in the navigation before adding the print page
self.renderer.items = nav.items
self.all_pages_in_nav = flatten_nav(nav.items)

# Optionally add the print page to the site navigation
if self.config.get("add_to_navigation"):
Expand All @@ -172,12 +173,13 @@ def on_page_content(self, html, page, config, files, **kwargs):
# We need to validate that the first heading on each page is a h1
# This is required for the print page table of contents and enumeration logic
if self.config.get("add_table_of_contents") or self.config.get("enumerate_headings"):
match = re.search(r"\<h[0-6]", html)
if match:
if not match.group() == "<h1":
msg = f"The page {page.title} ({page.file.src_path}) does not start with a level 1 heading."
msg += "This is required for print page Table of Contents and/or enumeration of headings."
raise AssertionError(msg)
if page in self.all_pages_in_nav:
match = re.search(r"\<h[0-6]", html)
if match:
if not match.group() == "<h1":
msg = f"The page {page.title} ({page.file.src_path}) does not start with a level 1 heading."
msg += "This is required for print page Table of Contents and/or enumeration of headings."
raise AssertionError(msg)

# Link to the PDF version of the entire site on a page.
if self.config.get("path_to_pdf") != "":
Expand Down
14 changes: 14 additions & 0 deletions mkdocs_print_site_plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ def get_theme_name(config) -> str:
return "mkdocs"
else:
return name


def flatten_nav(items):
"""
Create a flat list of pages from a nested navigation.
"""
pages = []

for item in items:
if item.is_page:
pages.append(item)
if item.is_section:
pages.append(flatten_nav(item.children))
return pages

0 comments on commit 25bc443

Please sign in to comment.