【问题标题】:How do I generate a sitemap.xml file with sphinx?如何使用 sphinx 生成 sitemap.xml 文件?
【发布时间】:2026-01-02 07:50:01
【问题描述】:

Here 它说:“特殊条目名称 self 代表包含 toctree 指令的文档。如果您想从 toctree 生成“站点地图”,这很有用。”我一直在关注这个话题 - Using self to create a sitemap with toctree in sphinx seem broken?。我似乎无法让它工作。

是否有任何地方有我可以查看的 sphinx 生成站点地图的详细示例?

【问题讨论】:

    标签: python python-sphinx sitemap toctree


    【解决方案1】:

    我最终在这个主题中使用了我自己的站点地图生成器。 https://github.com/guzzle/guzzle_sphinx_theme

    # Import guzzle theme https://github.com/guzzle/guzzle_sphinx_theme
    # Not actually using the theme but intead using the
    # sitemap functionality and overriding the theme/templates in source/
    html_translator_class = 'guzzle_sphinx_theme.HTMLTranslator'
    html_theme_path = guzzle_sphinx_theme.html_theme_path()
    html_theme = 'guzzle_sphinx_theme'
    extensions.append("guzzle_sphinx_theme")
    html_theme_options = {
        "base_url": "YOURSITEURL"
    }
    

    我正在覆盖我的源目录中的所有内容,除了站点地图生成器之外,我没有使用主题的任何其他部分。相当草率的解决方案,但它对我有用。

    【讨论】:

      【解决方案2】:

      我知道这个问题已经有四年了,所以当时这个插件可能不可用。我刚刚尝试了sphinx-sitemap 插件,它对我有用。

      集成很简单:

      pip install sphinx-sitemap
      

      然后在你的config.py 集合中:

      extensions = ['sphinx_sitemap']
      html_baseurl = 'https://my-site.com/docs/'
      

      然后正常生成网站。 sitemap.xml 将出现在项目的基础上。它们还具有一些高级功能,例如版本控制和多语言(我还没有尝试过)。

      【讨论】: