Skip to content

Commit a1ca60d

Browse files
authored
Support missing h1 tags, closes #94
1 parent 0a7300b commit a1ca60d

4 files changed

Lines changed: 20 additions & 23 deletions

File tree

mkdocs_print_site_plugin/plugin.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,6 @@ def on_page_content(self, html, page, config, files, **kwargs):
216216
if page != self.print_page:
217217
page.html = html
218218

219-
# We need to validate that the first heading on each page is a h1
220-
# This is required for the print page table of contents and enumeration logic
221-
if self.config.get("add_table_of_contents") or self.config.get(
222-
"enumerate_headings"
223-
):
224-
if page in self.all_pages_in_nav:
225-
match = re.search(r"\<h[0-6]", html)
226-
if match:
227-
if not match.group() == "<h1":
228-
msg = f"The page {page.title} ({page.file.src_path}) does not start with a level 1 heading."
229-
msg += "This is required for print page Table of Contents and/or enumeration of headings."
230-
raise AssertionError(msg)
231-
232219
# Link to the PDF version of the entire site on a page.
233220
if self.config.get("path_to_pdf") != "":
234221
pdf_url = self.config.get("path_to_pdf")

mkdocs_print_site_plugin/renderer.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def get_html_from_items(
7676
"""
7777
Get all the HTML from the pages.
7878
"""
79-
item_html = ""
79+
items_html = ""
8080

8181
for item in items:
8282
if item.is_page:
@@ -94,13 +94,21 @@ def get_html_from_items(
9494
% item.file.src_path
9595
)
9696
continue
97+
98+
item_html = item.html
99+
100+
# Add missing h1 tag if it doesn't exist
101+
if not item_html.startswith("<h1"):
102+
item_html = f"<h1 id=\"{to_snake_case(item.title)}\">{item.title}</h1>{item_html}"
103+
logger.warning(f"[mkdocs-print-site] '{item.file.src_path}' file is missing a leading h1 tag. Added to the print-page with title '{item.title}'")
104+
97105
# Update internal anchor links, image urls, etc
98-
item_html += fix_internal_links(
99-
item.html, item.url, directory_urls=dir_urls
106+
items_html += fix_internal_links(
107+
item_html, item.url, directory_urls=dir_urls
100108
)
101109

102110
if item.is_section:
103-
item_html += """
111+
items_html += """
104112
<h%s class='nav-section-title' id='section-%s'>
105113
%s <a class='headerlink' href='#section-%s' title='Permanent link'>↵</a>
106114
</h%s>
@@ -111,16 +119,16 @@ def get_html_from_items(
111119
to_snake_case(item.title),
112120
min(6, section_depth + 1),
113121
)
114-
item_html += get_html_from_items(
122+
items_html += get_html_from_items(
115123
item.children, dir_urls, excluded_pages, section_depth + 1
116124
)
117125
# We also need to indicate the end of section page
118126
# We do that using a h1 with a specific class
119127
# In CSS we display:none, in JS we can use it for formatting the table of contents.
120-
item_html += (
128+
items_html += (
121129
"<h1 class='nav-section-title-end'>Ended: %s</h1>" % item.title
122130
)
123-
return item_html
131+
return items_html
124132

125133
html += get_html_from_items(
126134
self._get_items(),

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name="mkdocs-print-site-plugin",
12-
version="2.4.1",
12+
version="2.5.0",
1313
description="MkDocs plugin that combines all pages into one, allowing for easy export to PDF and standalone HTML.",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",

tests/test_building.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ def test_basic_build6(tmp_path):
269269

270270
def test_basic_build7(tmp_path):
271271
"""
272-
Test error when page does not start with h1 heading.
272+
Test when page does not start with h1 heading.
273+
274+
As of v2.5.0, this is allowed (the plugin will add a heading to the print page)
273275
"""
274-
check_build(tmp_path, "bad_headings/mkdocs.yml", exit_code=1)
276+
check_build(tmp_path, "bad_headings/mkdocs.yml", exit_code=0)
275277

276278

277279
def test_basic_disable_plugin(tmp_path):

0 commit comments

Comments
 (0)