Skip to content

Commit e52b94d

Browse files
authored
Only add h1 tag if the first heading is not a h1
1 parent 15005e2 commit e52b94d

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

mkdocs_print_site_plugin/renderer.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import jinja2
23
import logging
34

@@ -21,7 +22,7 @@ class Renderer(object):
2122
def __init__(
2223
self,
2324
plugin_config,
24-
mkdocs_config={},
25+
mkdocs_config=None,
2526
cover_page_template_path="",
2627
banner_template_path="",
2728
print_page=None,
@@ -30,7 +31,7 @@ def __init__(
3031
Inits the class.
3132
"""
3233
self.plugin_config = plugin_config
33-
self.mkdocs_config = mkdocs_config
34+
self.mkdocs_config = mkdocs_config or {}
3435
self.cover_page_template_path = cover_page_template_path
3536
self.banner_template_path = banner_template_path
3637
self.print_page = print_page
@@ -82,26 +83,25 @@ def get_html_from_items(
8283
if item.is_page:
8384
# Do not include page in print page if excluded
8485
if exclude(item.file.src_path, excluded_pages):
85-
logging.debug("Excluding page " + item.file.src_path)
86+
logging.debug(f"Excluding page '{item.file.src_path}'")
8687
continue
8788

8889
# If you specify the same page twice in your navigation, it is only rendered once
8990
# so we need to check if the html attribute exists
9091
if hasattr(item, "html"):
9192
if item.html == "":
92-
logger.warning(
93-
"[mkdocs-print-site] %s is empty and will be ignored"
94-
% item.file.src_path
95-
)
93+
logger.warning(f"[mkdocs-print-site] {item.file.src_path} is empty and will be ignored")
9694
continue
9795

9896
item_html = item.html
9997

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-
98+
# Add missing h1 tag if the first heading is not a h1
99+
match = re.search(r"\<h[0-6]", item_html)
100+
if match:
101+
if not match.group() == "<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+
105105
# Support mkdocs-material tags
106106
# See https://squidfunk.github.io/mkdocs-material/plugins/tags
107107
if "tags" in item.meta:

0 commit comments

Comments
 (0)