Skip to main content

Sitemap generator for Sphinx

Project description

A Sphinx extension to generate multiversion and multilanguage sitemaps.org compliant sitemaps for the HTML version of your Sphinx documentation.

PyPI version Conda Forge Downloads Code style: Black Parallel Safe

Installing

Directly install via pip by using:

pip install sphinx-sitemap

Add sphinx_sitemap to the extensions array in your Sphinx conf.py. For example:

extensions = ['sphinx_sitemap']

Configuring

Set the value of html_baseurl in your Sphinx conf.py to the current base URL of your documentation. For example:

html_baseurl = 'https://my-site.com/docs/'

After the HTML build is done, sphinx-sitemap will output the location of the sitemap:

sitemap.xml was generated for URL https://my-site.com/docs/ in /path/to/_build/sitemap.xml

Tip: Make sure to confirm the accuracy of the sitemap after installs and upgrades.

Customizing the URL Scheme

The default URL format is {lang}{version}{link}. {lang} and {version} are controlled by the language and version config variables.

Note: As of Sphinx version 5, the language config value defaults to "en", if that makes the default scheme produce the incorrect url, then change the default behavior.

To change the default behavior, set the value of sitemap_url_scheme in conf.py to the desired format. For example:

sitemap_url_scheme = "{link}"

Or for nested deployments, something like:

sitemap_url_scheme = "{version}{lang}subdir/{link}"

Note: The extension is currently opinionated, in that it automatically appends trailing slashes to both the language and version values. You can also omit values from the scheme for desired behavior.

Changing the Filename

Set sitemap_filename in conf.py to the desired filename, for example:

sitemap_filename = "sitemap.xml"

Versioning Configuration

For multiversion sitemaps, it is required to generate a sitemap per version and then manually add their locations to a sitemapindex file.

The extension will look at the version config value for the current version being built, so make sure that is set.

Note: When using multiple versions, it is best practice to set the canonical URL in the theme layout of all versions to the latest version of that page:

<link rel="canonical" href="https://my-site.com/docs/latest/index.html"/>

Multilingual Configuration

For multilingual sitemaps, generate a sitemap per language/locale and then manually add their locations to a sitemapindex file.

The primary language is set by the language config value. Alternative languages are either manually set by sitemap_locales option or auto-detected by the extension from the locale_dirs config value, so make sure one of those is set.

sitemap_locales configuration is to specify a list of locales to include in the sitemap. For instance, if a third-party extension adds unsupported langauges to locale_dirs, or to allow locales to reach a certain translated percentage before making them public. For example, if the primary language is en, and a list with es and fr translations specified, the sitemap look like this:

<?xml version="1.0" encoding="utf-8"?>
  <urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
      <loc>https://my-site.com/docs/en/index.html</loc>
      <xhtml:link href="https://my-site.com/docs/es/index.html" hreflang="es" rel="alternate"/>
      <xhtml:link href="https://my-site.com/docs/fr/index.html" hreflang="fr" rel="alternate"/>
      <xhtml:link href="https://my-site.com/docs/en/index.html" hreflang="en" rel="alternate"/>
    </url>
    <url>
        <loc>https://my-site.com/docs/en/about.html</loc>
        <xhtml:link href="https://my-site.com/docs/es/about.html" hreflang="es" rel="alternate"/>
        <xhtml:link href="https://my-site.com/docs/fr/about.html" hreflang="fr" rel="alternate"/>
        <xhtml:link href="https://my-site.com/docs/en/about.html" hreflang="en" rel="alternate"/>
    </url>
  </urlset>

When the sitemap locales are limited:

sitemap_locales = ['en', 'es']

The end result is something like the following for each language/version build:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://my-site.com/docs/en/index.html</loc>
    <xhtml:link href="https://my-site.com/docs/es/index.html" hreflang="es" rel="alternate"/>
    <xhtml:link href="https://my-site.com/docs/en/index.html" hreflang="en" rel="alternate"/>
  </url>
  <url>
    <loc>https://my-site.com/docs/en/about.html</loc>
    <xhtml:link href="https://my-site.com/docs/es/about.html" hreflang="es" rel="alternate"/>
    <xhtml:link href="https://my-site.com/docs/en/about.html" hreflang="en" rel="alternate"/>
  </url>
</urlset>

When the special value of [None] is set:

sitemap_locales = [None]

only the primary language is generated:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://my-site.com/docs/en/index.html</loc>
  </url>
  <url>
    <loc>https://my-site.com/docs/en/about.html</loc>
  </url>
</urlset>

Getting the Most out of the Sitemap

  1. Add a robots.txt file in the source directory which contains a link to the sitemap or sitemapindex. For example:

    User-agent: *
    
    Sitemap: https://my-site.com/docs/sitemap.xml

    Then, add robots.txt to the html_extra_path config value:

    html_extra_path = ['robots.txt']
  2. Submit the sitemap or sitemapindex to the appropriate search engine tools.

Contributing

Pull Requests welcome! See CONTRIBUTING for instructions on how best to contribute.

License

sphinx-sitemap is made available under a MIT license; see LICENSE for details.

Originally based on the sitemap generator in the guzzle_sphinx_theme project, also licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx-sitemap-2.3.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_sitemap-2.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file sphinx-sitemap-2.3.0.tar.gz.

File metadata

  • Download URL: sphinx-sitemap-2.3.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for sphinx-sitemap-2.3.0.tar.gz
Algorithm Hash digest
SHA256 7b27b5d9f6ebf75597da7d48c64dcb9271fa269925673d900ee27d6b2c484915
MD5 db13961790041876229d1517062d4fb9
BLAKE2b-256 cec2c8ee76a0cd7435ddfaa55932fd73e2e1922a0603d4ea1ff9b0e3c0f2abba

See more details on using hashes here.

File details

Details for the file sphinx_sitemap-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: sphinx_sitemap-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for sphinx_sitemap-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8211ae28413ee529814bd56762873a5e1878fff0f254a7488da762984bdf658c
MD5 5588a8649548be951422c12d0456ef8d
BLAKE2b-256 c32792bd1deacd36907c88c02915ed3a89afbb0e5f2d99068e159d929a4a43cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page