Skip to content

Commit

Permalink
Fix MkDocs sitemap to not skip section index pages
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
oprypin committed Apr 25, 2021
1 parent ca3af56 commit 7ecc308
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mkdocs_section_index/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def get_source(self, environment: Environment, template: str) -> Tuple[str, str,
old_src = src
path = pathlib.Path(filename).as_posix()

if path.endswith("/material/partials/nav-item.html"):
if path.endswith("/mkdocs/templates/sitemap.xml"):
src = _transform_mkdocs_sitemap_template(src)
elif path.endswith("/material/partials/nav-item.html"):
src = _transform_material_nav_item_template(src)
elif path.endswith("/material/partials/tabs-item.html"):
src = _transform_material_tabs_item_template(src)
Expand All @@ -41,6 +43,13 @@ def get_source(self, environment: Environment, template: str) -> Tuple[str, str,
return src, filename, uptodate


def _transform_mkdocs_sitemap_template(src: str) -> str:
return src.replace(
"{%- else %}",
"{%- endif %}{% if item.url %}",
)


def _transform_material_nav_item_template(src: str) -> str:
src = src.replace(
"{% if nav_item.children %}",
Expand Down
47 changes: 47 additions & 0 deletions tests/rewrites/mkdocs-sitemap-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# https://github.com/mkdocs/mkdocs/blob/cb85d48851133e6d482deb3405de67c3dbea82be/mkdocs/templates/sitemap.xml
input: |
{%- macro nav_item(item) -%}
{%- if item.children -%}
{%- for child in item.children -%}
{{ nav_item(child) }}
{%- endfor -%}
{%- else %}
{%- if not item.is_link -%}
<url>
<loc>{% if item.canonical_url %}{{ item.canonical_url|e }}{% else %}{{ item.abs_url|e }}{% endif %}</loc>
{% if item.update_date %}<lastmod>{{item.update_date}}</lastmod>{% endif %}
<changefreq>daily</changefreq>
</url>
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for item in nav -%}
{{ nav_item(item) }}
{%- endfor %}
</urlset>
output: |
{%- macro nav_item(item) -%}
{%- if item.children -%}
{%- for child in item.children -%}
{{ nav_item(child) }}
{%- endfor -%}
{%- endif %}{% if item.url %}
{%- if not item.is_link -%}
<url>
<loc>{% if item.canonical_url %}{{ item.canonical_url|e }}{% else %}{{ item.abs_url|e }}{% endif %}</loc>
{% if item.update_date %}<lastmod>{{item.update_date}}</lastmod>{% endif %}
<changefreq>daily</changefreq>
</url>
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for item in nav -%}
{{ nav_item(item) }}
{%- endfor %}
</urlset>
5 changes: 5 additions & 0 deletions tests/test_rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from mkdocs_section_index import rewrites


@pytest.mark.golden_test("rewrites/mkdocs-sitemap-*.yml")
def test_rewrite_mkdocs_sitemap(golden):
assert rewrites._transform_mkdocs_sitemap_template(golden["input"]) == golden.out["output"]


@pytest.mark.golden_test("rewrites/readthedocs-base-*.yml")
def test_rewrite_readthedocs_base(golden):
assert rewrites._transform_readthedocs_base_template(golden["input"]) == golden.out["output"]
Expand Down

0 comments on commit 7ecc308

Please sign in to comment.