Skip to content

Commit 83eb35d

Browse files
authored
Merge pull request #104 from timvink/support_missing_h1
Support pages with missing h1 tags
2 parents 5dd5c90 + 11a7fe7 commit 83eb35d

6 files changed

Lines changed: 57 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: 25 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,31 @@ 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+
105+
# Support mkdocs-material tags
106+
# See https://squidfunk.github.io/mkdocs-material/plugins/tags
107+
if "tags" in item.meta:
108+
tags = item.meta["tags"]
109+
tags_html = "<nav class='md-tags'>"
110+
for tag in tags:
111+
tags_html += f"<span class='md-tag'>{tag}</span>"
112+
tags_html += "</nav>"
113+
item_html = tags_html + item_html
114+
97115
# Update internal anchor links, image urls, etc
98-
item_html += fix_internal_links(
99-
item.html, item.url, directory_urls=dir_urls
116+
items_html += fix_internal_links(
117+
item_html, item.url, directory_urls=dir_urls
100118
)
101119

102120
if item.is_section:
103-
item_html += """
121+
items_html += """
104122
<h%s class='nav-section-title' id='section-%s'>
105123
%s <a class='headerlink' href='#section-%s' title='Permanent link'>↵</a>
106124
</h%s>
@@ -111,16 +129,16 @@ def get_html_from_items(
111129
to_snake_case(item.title),
112130
min(6, section_depth + 1),
113131
)
114-
item_html += get_html_from_items(
132+
items_html += get_html_from_items(
115133
item.children, dir_urls, excluded_pages, section_depth + 1
116134
)
117135
# We also need to indicate the end of section page
118136
# We do that using a h1 with a specific class
119137
# In CSS we display:none, in JS we can use it for formatting the table of contents.
120-
item_html += (
138+
items_html += (
121139
"<h1 class='nav-section-title-end'>Ended: %s</h1>" % item.title
122140
)
123-
return item_html
141+
return items_html
124142

125143
html += get_html_from_items(
126144
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",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
tags:
3+
- HTML5
4+
- JavaScript
5+
- CSS
6+
---
7+
8+
# Hello there
9+
10+
Testing [tags](https://squidfunk.github.io/mkdocs-material/plugins/tags/?h=tags#usage) plugin.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
site_name: Test
2+
3+
theme:
4+
name: material
5+
6+
plugins:
7+
- tags
8+
- print-site:
9+
add_to_navigation: true
10+
11+
markdown_extensions:
12+
- attr_list

tests/test_building.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,17 @@ 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

278+
def test_build_with_material_tags(tmp_path):
279+
"""
280+
Test support with tags.
281+
"""
282+
check_build(tmp_path, "mkdocs_material_tags/mkdocs.yml", exit_code=0)
276283

277284
def test_basic_disable_plugin(tmp_path):
278285
"""

0 commit comments

Comments
 (0)