From b127ac951f7e88105e1ec1c13e253116dcf34d5f Mon Sep 17 00:00:00 2001 From: Lex Li Date: Tue, 26 Mar 2024 02:03:18 -0400 Subject: [PATCH 1/3] NEW: Added sitemap_suffix_included --- docs/source/advanced-configuration.rst | 14 ++++++++++++++ docs/source/configuration-values.rst | 8 ++++++++ sphinx_sitemap/__init__.py | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/source/advanced-configuration.rst b/docs/source/advanced-configuration.rst index 0aa0f8d..51d4346 100644 --- a/docs/source/advanced-configuration.rst +++ b/docs/source/advanced-configuration.rst @@ -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 ^^^^^^^^^^^^^^^ diff --git a/docs/source/configuration-values.rst b/docs/source/configuration-values.rst index 12cc37b..97c52ee 100644 --- a/docs/source/configuration-values.rst +++ b/docs/source/configuration-values.rst @@ -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 diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 0668fbb..9bb766e 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -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: @@ -141,7 +143,7 @@ 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 From 672f8aab718e2f185660d8a3b5047a4d220b5f0f Mon Sep 17 00:00:00 2001 From: Lex Li Date: Tue, 26 Mar 2024 02:13:26 -0400 Subject: [PATCH 2/3] Added test case for sitemap_suffix_included --- tests/test_simple.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_simple.py b/tests/test_simple.py index eb83a56..a5ae1fd 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -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", + ] + } From 2e3411a2f61f8aab8ddf79fc0a6900cd6883c281 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 06:25:13 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sphinx_sitemap/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sphinx_sitemap/__init__.py b/sphinx_sitemap/__init__.py index 9bb766e..78965bc 100644 --- a/sphinx_sitemap/__init__.py +++ b/sphinx_sitemap/__init__.py @@ -143,7 +143,9 @@ def add_html_link(app: Sphinx, pagename: str, templatename, context, doctree): else: sitemap_link = pagename + "/" else: - sitemap_link = (pagename + file_suffix) if app.config.sitemap_suffix_included else pagename + sitemap_link = ( + (pagename + file_suffix) if app.config.sitemap_suffix_included else pagename + ) env.app.sitemap_links.put(sitemap_link) # type: ignore