Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Added sitemap_suffix_included to better work with Cloudflare Pages and search engines #89

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/source/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ Set :confval:`sitemap_filename` in **conf.py** to the desired filename, for exam

sitemap_filename = "sitemap.xml"

.. _configuration_including_suffix:

Including File Extensions
^^^^^^^^^^^^^^^^^^^^^^^^^

Set :confval:`sitemap_suffix_included` in **conf.py** to include/exclude file extensions in URLs, for example:

.. code-block:: python

sitemap_suffix_included = True

.. note:: The default value is ``True``. Set to ``False`` to suppress file extensions in URLs so that the sitemap
generated works better with service providers like Cloudflare Pages.

Version Support
^^^^^^^^^^^^^^^

Expand Down
8 changes: 8 additions & 0 deletions docs/source/configuration-values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ Another line to :math:`test two two`
See :ref:`configuration_supporting_multiple_languages` for more information.

.. versionadded:: 2.2.0

.. confval:: sitemap_suffix_included

Whether to include file extensions in URL. Default is ``True``.

See :ref:`configuration_including_suffix` for more information.

.. versionadded:: 2.5.2
6 changes: 5 additions & 1 deletion sphinx_sitemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def setup(app: Sphinx) -> Dict[str, Any]:

app.add_config_value("sitemap_filename", default="sitemap.xml", rebuild="")

app.add_config_value("sitemap_suffix_included", default=True, rebuild="")

try:
app.add_config_value("html_baseurl", default=None, rebuild="")
except BaseException:
Expand Down Expand Up @@ -141,7 +143,9 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree):
else:
sitemap_link = pagename + "/"
else:
sitemap_link = pagename + file_suffix
sitemap_link = (
(pagename + file_suffix) if app.config.sitemap_suffix_included else pagename
)

env.app.sitemap_links.put(sitemap_link) # type: ignore

Expand Down
35 changes: 35 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,38 @@ def test_simple_dirhtml(app, status, warning):
"search/",
]
}


@pytest.mark.sphinx(
"html",
freshenv=True,
confoverrides={
"html_baseurl": "https://example.org/docs/",
"language": "en",
"sitemap_suffix_included": False,
},
)
def test_sitemap_suffix_included(app, status, warning):
app.warningiserror = True
app.build()
assert "sitemap.xml" in os.listdir(app.outdir)
doc = etree.parse(app.outdir / "sitemap.xml")
urls = {
e.text
for e in doc.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}loc")
}

assert urls == {
f"https://example.org/docs/en/{d}"
for d in [
"index",
"foo",
"bar",
"lorem",
"ipsum",
"dolor",
"elitr",
"genindex",
"search",
]
}