Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/mkdocs_to_pdf/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def to_pattern(s: str) -> Pattern:
self._options.logger.debug(
f'Exclude page patterns: {self._exclude_page_patterns}')

def _is_first_child_of_url_less_section(self, nav_items, page):
"""Recursively search for the page in the nav."""
for item in nav_items:
if item.is_section and not hasattr(item, 'url') and item.children:
if item.children[0] == page:
return True
# Recurse into children of the section
if self._is_first_child_of_url_less_section(item.children, page):
return True
return False

def on_nav(self, nav):
""" on_nav """
self._nav = nav
Expand All @@ -56,6 +67,14 @@ def on_nav(self, nav):
def on_post_page(self, output_content: str, page, pdf_path: str) -> str:
""" on_post_page """

if self._is_first_child_of_url_less_section(self._nav, page):
self._options.logger.info(f"Removing h1 from '{page.title}' as it is the first child of a URL-less section.")
soup = BeautifulSoup(output_content, 'html.parser')
h1 = soup.find('h1')
if h1:
h1.decompose()
output_content = str(soup)

def is_excluded(url: str) -> bool:
for p in self._exclude_page_patterns:
if p.match(url):
Expand Down