From a1ca60dcba1cc10fc6de0ff2eff6556cc0d4a2c9 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Thu, 23 May 2024 20:44:59 +0000 Subject: [PATCH 1/2] Support missing h1 tags, closes #94 --- mkdocs_print_site_plugin/plugin.py | 13 ------------- mkdocs_print_site_plugin/renderer.py | 22 +++++++++++++++------- setup.py | 2 +- tests/test_building.py | 6 ++++-- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index 1820828..51f7f1a 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -216,19 +216,6 @@ def on_page_content(self, html, page, config, files, **kwargs): if page != self.print_page: page.html = html - # 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" - ): - if page in self.all_pages_in_nav: - match = re.search(r"\{item.title}{item_html}" + 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}'") + # Update internal anchor links, image urls, etc - item_html += fix_internal_links( - item.html, item.url, directory_urls=dir_urls + items_html += fix_internal_links( + item_html, item.url, directory_urls=dir_urls ) if item.is_section: - item_html += """ + items_html += """ %s @@ -111,16 +119,16 @@ def get_html_from_items( to_snake_case(item.title), min(6, section_depth + 1), ) - item_html += get_html_from_items( + items_html += get_html_from_items( item.children, dir_urls, excluded_pages, section_depth + 1 ) # We also need to indicate the end of section page # We do that using a h1 with a specific class # In CSS we display:none, in JS we can use it for formatting the table of contents. - item_html += ( + items_html += ( "

Ended: %s

" % item.title ) - return item_html + return items_html html += get_html_from_items( self._get_items(), diff --git a/setup.py b/setup.py index e562e3a..e64a683 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name="mkdocs-print-site-plugin", - version="2.4.1", + version="2.5.0", description="MkDocs plugin that combines all pages into one, allowing for easy export to PDF and standalone HTML.", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_building.py b/tests/test_building.py index 10f6c94..d2c4247 100644 --- a/tests/test_building.py +++ b/tests/test_building.py @@ -269,9 +269,11 @@ def test_basic_build6(tmp_path): def test_basic_build7(tmp_path): """ - Test error when page does not start with h1 heading. + Test when page does not start with h1 heading. + + As of v2.5.0, this is allowed (the plugin will add a heading to the print page) """ - check_build(tmp_path, "bad_headings/mkdocs.yml", exit_code=1) + check_build(tmp_path, "bad_headings/mkdocs.yml", exit_code=0) def test_basic_disable_plugin(tmp_path): From 11a7fe749eb0b8aa7d2210e17e54da37bb962be7 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Thu, 23 May 2024 21:32:19 +0000 Subject: [PATCH 2/2] Support mkdocs material tags, closes #92 --- mkdocs_print_site_plugin/renderer.py | 10 ++++++++++ .../projects/mkdocs_material_tags/docs/index.md | 10 ++++++++++ .../projects/mkdocs_material_tags/mkdocs.yml | 12 ++++++++++++ tests/test_building.py | 5 +++++ 4 files changed, 37 insertions(+) create mode 100644 tests/fixtures/projects/mkdocs_material_tags/docs/index.md create mode 100644 tests/fixtures/projects/mkdocs_material_tags/mkdocs.yml diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index fa8eff9..75d5940 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -102,6 +102,16 @@ def get_html_from_items( item_html = f"

{item.title}

{item_html}" 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}'") + # Support mkdocs-material tags + # See https://squidfunk.github.io/mkdocs-material/plugins/tags + if "tags" in item.meta: + tags = item.meta["tags"] + tags_html = "" + item_html = tags_html + item_html + # Update internal anchor links, image urls, etc items_html += fix_internal_links( item_html, item.url, directory_urls=dir_urls diff --git a/tests/fixtures/projects/mkdocs_material_tags/docs/index.md b/tests/fixtures/projects/mkdocs_material_tags/docs/index.md new file mode 100644 index 0000000..8903870 --- /dev/null +++ b/tests/fixtures/projects/mkdocs_material_tags/docs/index.md @@ -0,0 +1,10 @@ +--- +tags: + - HTML5 + - JavaScript + - CSS +--- + +# Hello there + +Testing [tags](https://squidfunk.github.io/mkdocs-material/plugins/tags/?h=tags#usage) plugin. \ No newline at end of file diff --git a/tests/fixtures/projects/mkdocs_material_tags/mkdocs.yml b/tests/fixtures/projects/mkdocs_material_tags/mkdocs.yml new file mode 100644 index 0000000..939d653 --- /dev/null +++ b/tests/fixtures/projects/mkdocs_material_tags/mkdocs.yml @@ -0,0 +1,12 @@ +site_name: Test + +theme: + name: material + +plugins: + - tags + - print-site: + add_to_navigation: true + +markdown_extensions: + - attr_list \ No newline at end of file diff --git a/tests/test_building.py b/tests/test_building.py index d2c4247..4c1b7bf 100644 --- a/tests/test_building.py +++ b/tests/test_building.py @@ -275,6 +275,11 @@ def test_basic_build7(tmp_path): """ check_build(tmp_path, "bad_headings/mkdocs.yml", exit_code=0) +def test_build_with_material_tags(tmp_path): + """ + Test support with tags. + """ + check_build(tmp_path, "mkdocs_material_tags/mkdocs.yml", exit_code=0) def test_basic_disable_plugin(tmp_path): """