主题配置:Algolia Search {#theme-config-algolia-search}

themeConfig.algolia 选项允许你使用 Algolia DocSearch。要启用它,你至少需要提供 apiKey 和 indexName:

  1. module.exports = {
  2. themeConfig: {
  3. algolia: {
  4. apiKey: 'your_api_key',
  5. indexName: 'index_name'
  6. }
  7. }
  8. }

有关更多选项,请查看 Algolia DocSearch‘s documentation。你可以将任何额外的选项与其他选项一起传递,比如 searchParameters

  1. module.exports = {
  2. themeConfig: {
  3. algolia: {
  4. apiKey: 'your_api_key',
  5. indexName: 'index_name',
  6. searchParameters: {
  7. facetFilters: ['tags:guide,api']
  8. }
  9. }
  10. }
  11. }

国际化(i18n) {#internationalization-i18n}

如果你的文档中有多个语言环境,并且你在 themeconfig 中定义了一个 locales 对象:

  1. module.exports = {
  2. themeConfig: {
  3. locales: {
  4. // ...
  5. },
  6. algolia: {
  7. apiKey: 'your_api_key',
  8. indexName: 'index_name'
  9. }
  10. }
  11. }

VitePress 会自动在 searchParams.facetFilter 数组中添加一个语言值正确的 language facetFilter。通过添加 language 作为 faceting_ 的 _custom 属性 请确保你的 DocSearch 配置也配置正确 ,并根据 <html> 元素的 lang 属性进行设置。 以下是 DocSearch 配置的一个简短示例:

  1. {
  2. "index_name": "<the name of your library>",
  3. "start_urls": [
  4. {
  5. "url": "<your deployed url>"
  6. }
  7. ],
  8. "stop_urls": ["(?:(?<!\\.html)(?<!/))$"],
  9. "selectors": {
  10. "lvl0": {
  11. "selector": ".sidebar > .sidebar-links > .sidebar-link .sidebar-link-item.active",
  12. "global": true,
  13. "default_value": "Documentation"
  14. },
  15. "lvl1": ".content h1",
  16. "lvl2": ".content h2",
  17. "lvl3": ".content h3",
  18. "lvl4": ".content h4",
  19. "lvl5": ".content h5",
  20. "lvl6": ".content p, .content li",
  21. "text": ".content [class^=language-]",
  22. "language": {
  23. "selector": "/html/@lang",
  24. "type": "xpath",
  25. "global": true,
  26. "default_value": "en-US"
  27. }
  28. },
  29. "custom_settings": {
  30. "attributesForFaceting": ["language"]
  31. }
  32. }

你可以查看 Vue Router 使用的 DocSearch 配置 以获得完整的示例。