【问题标题】:Sphinx Docs | Can it support Algolia's Doc Search狮身人面像文档 |能支持Algolia的Doc Search吗
【发布时间】:2019-02-25 18:47:42
【问题描述】:

我想知道是否有人知道 Algolia 的 DocSearch 免费文档服务是否可以集成到 Sphinx 文档网站中。谢谢。。

【问题讨论】:

  • 请尝试询问具体的实际问题。你真的尝试过使用 Algolia 来制作 Sphinx 文档吗?发生了什么?见stackoverflow.com/help/on-topic
  • 好的,抱歉,先试一下,然后再回来。

标签: python-sphinx algolia


【解决方案1】:

它绝对可以很容易地集成到 Sphinx 文档中。

我申请了一个帐户并获得了 API 密钥。

阅读:https://community.algolia.com/docsearch/run-your-own.html

我在 Python 2.7 和 MacOS 10.13 下从 github 安装了免费的 docsearch-scraper,并使用 pipenv 和缺少的 dotenv 模块的附加后安装使其工作。

经过一番摆弄后,我使用了基于./docsearch bootstrap 命令输出的自定义config.json,将所有以“lvln”开头的行中出现的“FIXME”替换为“.section”,并将“FIXME”替换为以“text”开头的行和“.document”(参见下面的示例)。

我们成功地索引了 Sphinx 文档并运行 .docsearch playground 命令打开了一个测试网络服务器,该服务器立即提供了出色的结果。

我们使用 readthedocs sphinx_rtd_theme,您可以轻松地将 algolia 文档中的 CSS-Links 和 Javascript sn-ps 添加到创建到 _source/_templates/ 文件夹中的 page.html 模板扩展文件中(见下文)。此文件夹可能需要在您的设置的conf.py 中注册!

将此添加到您现有位置的conf.py

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

当我完成集成后,我可能会带着更详细的分步指南回到这里。

示例 config.json:

{
  "index_name": "yourindexname",
  "start_urls": [
    "https://replaceme.com"
  ],
  "stop_urls": [
    "https://replaceme.com/omit"
  ],
  "selectors": {
    "lvl0": ".section h1",
    "lvl1": ".section h2",
    "lvl2": ".section h3",
    "lvl3": ".section h4",
    "lvl4": ".section h5",
    "lvl5": ".section h6",
    "text": ".document p, .document li"
  },
}

更多内容:https://community.algolia.com/docsearch/config-file.html

这会在 _source/_static/ 文件夹中添加 algolia CSS 和 custom.css 文件以覆盖样式。 sn-ps的来源见https://community.algolia.com/docsearch/dropdown.html

示例page.html 模板:

{% extends "!page.html" %}

{% set css_files = css_files + ["https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css", "_static/custom.css"] %}

{% block footer %}
<script
src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
<script>
docsearch({
  // Your apiKey and indexName will be given to you once
  // we create your config. The appId value is missing in the first 
  // version of this example and in the algolia community doc 
  // until today (5th march of 2019).
  appId: '<your-app-id>',
  apiKey: '<yourkey>',
  indexName: '<yourindex>',
  // Replace inputSelector with a CSS selector
  // matching your search input
  inputSelector: '#rtd-search-form input[type=text]',
  // Set debug to true if you want to inspect the dropdown
  debug: true
});
</script>

{% endblock %}

ToDo:测试 algolia 社区文档的链接

提示:您可以通过将此样式添加到您的 custom.css 文件中来测试输入选择器是否有效:

#rtd-search-form input[type=text]{
    background-color: #c51919; /* red */
}

【讨论】:

  • 感谢您的详细解答。我的方法错过了几个步骤。一旦我找到一些时间会回复它,与您的 cmets 一起并将其标记为已批准。谢谢!!
  • 我更新了底部的page.html sn-p,将appId添加到JSON数据中。没有它是行不通的。我现在可以确认这对我们来说完全是在一个实时网站上工作”。
  • 快速提问。我已经完成了你所说的一切。但我看不到 Algolia DocSearch 下拉菜单。是因为我在我的本地主机上吗?
  • 只要您可以访问互联网,它也可以在从网络服务器运行时在本地静态构建中工作。结果由 Algolia CDN 提供。对我们来说,即使没有网络服务器它也能工作。如果有疑问,请使用 HTML 所在目录 _build/html/index.html 中的python -m SimpleHTTPServer 8080。我们遇到了上面 page.html sn-p 中缺少 appID 值的问题。我在更新评论中提到了它。与我最初的帖子相比,检查您是否添加了行 `appId: '', `。这是在做伎俩,并没有在 Algolia 文档的所有地方都提到。
  • 您需要进一步改进config.json以使定义列表的内容被索引并省略目录的双重索引:追加到"text": ".document p, .document li, .document dt, .document dd"并添加"selectors_exclude": ["[class^='toctree']"],
猜你喜欢
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多