【问题标题】:Read the docs search broken阅读文档搜索损坏
【发布时间】:2019-02-27 15:51:51
【问题描述】:

我正在尝试使用 Sphinx 结合阅读文档主题编写文档,但搜索功能似乎已损坏。

我在 Windows 10 上运行 Python 3.7 和最新版本的 Sphinx 和 Read the Docs 主题(均与 pip install 一起安装)。构建页面并在浏览器中打开 index.html 后(尝试 Edge 和 Chrome ) 我无法搜索我的离线文档。搜索页面打开但只显示动画Search -> Search . -> Search .. -> Search …,如果我将主题更改回标准主题,搜索将正常工作。

由于搜索适用于标准主题,它似乎不是与浏览器相关的问题,也似乎与我没有在 localhost 上托管页面这一事实有关。

如果我查看控制台中的错误,我会看到两个错误

jquery.js:4 Failed to load file:///C:/user/documentation/_build/html/searchindex.js: 
Cross origin requests are only supported for protocol 
schemes: http, data, chrome, chrome-extension, https.

searchtools.js:144 Uncaught ReferenceError: Stemmer is not defined
at Object.query (searchtools.js:144)
at Object.setIndex (searchtools.js:83)
at searchindex.js:1

我在网上搜索时找不到任何有用的东西来解决这个问题。同样刷新浏览器缓存 (Ctrl + F5),如 https://github.com/rtfd/readthedocs.org/issues/4026 中所述,也不起作用。

有人遇到过同样的问题吗?

【问题讨论】:

  • @mzjn:感谢您的评论。我已经尝试安装旧版本的 sphinx 1.7.9 但这也没有解决问题。
  • 如果我以 HTML 格式下载 ReadTheDocs 文档,搜索不起作用的事实是意料之中的,对吧?

标签: python python-sphinx read-the-docs


【解决方案1】:

我使用以下解决方案 (https://github.com/rtfd/sphinx_rtd_theme/pull/672/commits/4b9053cb9e805211f227399d66d82361b3e2bf56) 解决了这个问题。

感谢 GitHub 用户 @tk0miya。

首先,导航到 Python 安装文件夹并在 ./Lib/site-packages/sphinx_rtd_theme/layout.html 中找到 layout.html

然后将第 191 - 204 行替换为包含

<script type="text/javascript"> 
        var DOCUMENTATION_OPTIONS = { 
            URL_ROOT:'{{ url_root }}', 
            VERSION:'{{ release|e }}', 
            LANGUAGE:'{{ language }}', 
            COLLAPSE_INDEX:false, 
            FILE_SUFFIX:'{{ '' if no_search_suffix else file_suffix }}', 
            HAS_SOURCE:  {{ has_source|lower }}, 
            SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}' 
        }; 
    </script> 
    {%- for scriptfile in script_files %} 
      <script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script> 
    {%- endfor %} 

通过以下代码

{% if sphinx_version >= "1.8.0" %} 
      <script type="text/javascript" id="documentation_options" data-url_root="{{ pathto('', 1) }}" src="{{ pathto('_static/documentation_options.js', 1) }}"></script> 
      {%- for scriptfile in script_files %} 
        {{ js_tag(scriptfile) }} 
      {%- endfor %} 
    {% else %} 
      <script type="text/javascript"> 
          var DOCUMENTATION_OPTIONS = { 
              URL_ROOT:'{{ url_root }}', 
              VERSION:'{{ release|e }}', 
              LANGUAGE:'{{ language }}', 
              COLLAPSE_INDEX:false, 
              FILE_SUFFIX:'{{ '' if no_search_suffix else file_suffix }}', 
              HAS_SOURCE:  {{ has_source|lower }}, 
              SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}' 
          }; 
      </script> 
      {%- for scriptfile in script_files %} 
        <script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script> 
      {%- endfor %} 
    {% endif %} 

然后在控制台的文档文件夹中运行 make cleanmake html。在您的文档文件夹中执行控制台命令很重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多