docsearch

Algolia DocSearch 集成到 VuePress 中,为你的文档网站提供搜索功能。

::: tip 当你正确配置该插件后,默认主题会把 DocSearch 按钮添加到导航栏。

该插件不一定能在其他主题中直接使用,因此你应参考主题本身的文档来获取更多信息。 :::

使用方法

  1. npm i -D @vuepress/plugin-docsearch@next
  1. const { docsearchPlugin } = require('@vuepress/plugin-docsearch')
  2. module.exports = {
  3. plugins: [
  4. docsearchPlugin({
  5. // 配置项
  6. }),
  7. ],
  8. }

获取搜索索引

你需要 提交你的网站 URL 来加入 DocSearch 项目。当你的索引成功创建后, DocSearch 团队会将 apiKeyindexName 发送到你的邮箱。接下来,你就可以配置该插件,在 VuePress 中启用 DocSearch 了。

或者,你也可以 运行你自己的爬虫 来创建索引,然后使用你自己的 appId, apiKeyindexName 来配置该插件。

::: details 官方爬虫配置示例

  1. new Crawler({
  2. appId: 'YOUR_APP_ID',
  3. apiKey: 'YOUR_API_KEY',
  4. rateLimit: 8,
  5. startUrls: [
  6. // 这是 Algolia 开始抓取网站的初始地址
  7. // 如果你的网站被分为数个独立部分,你可能需要在此设置多个入口链接
  8. 'https://YOUR_WEBSITE_URL/',
  9. ],
  10. sitemaps: [
  11. // 如果你在使用 Sitemap 插件 (如: vuepress-plugin-sitemap2),你可以提供 Sitemap 链接
  12. 'https://YOUR_WEBSITE_URL/sitemap.xml',
  13. ],
  14. ignoreCanonicalTo: false,
  15. exclusionPatterns: [
  16. // 你可以通过它阻止 Algolia 抓取某些 URL
  17. ],
  18. discoveryPatterns: [
  19. // 这是 Algolia 抓取 URL 的范围
  20. 'https://YOUR_WEBSITE_URL/**',
  21. ],
  22. // 爬虫执行的计划时间,可根据文档更新频率设置
  23. schedule: 'at 02:00 every 1 day',
  24. actions: [
  25. // 你可以拥有多个 action,特别是你在一个域名下部署多个文档时
  26. {
  27. // 使用适当的名称为索引命名
  28. indexName: 'YOUR_INDEX_NAME',
  29. // 索引生效的路径
  30. pathsToMatch: ['https://YOUR_WEBSITE_URL/**'],
  31. // 控制 Algolia 如何抓取你的站点
  32. recordExtractor: ({ $, helpers }) => {
  33. // @vuepress/theme-default 的选项
  34. return helpers.docsearch({
  35. recordProps: {
  36. lvl0: {
  37. selectors: '.sidebar-heading.active',
  38. defaultValue: 'Documentation',
  39. },
  40. lvl1: '.theme-default-content h1',
  41. lvl2: '.theme-default-content h2',
  42. lvl3: '.theme-default-content h3',
  43. lvl4: '.theme-default-content h4',
  44. lvl5: '.theme-default-content h5',
  45. lvl6: '.theme-default-content h6',
  46. content: '.theme-default-content p, .theme-default-content li',
  47. },
  48. indexHeadings: true,
  49. })
  50. },
  51. },
  52. ],
  53. initialIndexSettings: {
  54. // 控制索引如何被初始化,这仅当索引尚未生成时有效
  55. // 你可能需要在修改后手动删除并重新生成新的索引
  56. YOUR_INDEX_NAME: {
  57. attributesForFaceting: ['type', 'lang'],
  58. attributesToRetrieve: ['hierarchy', 'content', 'anchor', 'url'],
  59. attributesToHighlight: ['hierarchy', 'hierarchy_camel', 'content'],
  60. attributesToSnippet: ['content:10'],
  61. camelCaseAttributes: ['hierarchy', 'hierarchy_radio', 'content'],
  62. searchableAttributes: [
  63. 'unordered(hierarchy_radio_camel.lvl0)',
  64. 'unordered(hierarchy_radio.lvl0)',
  65. 'unordered(hierarchy_radio_camel.lvl1)',
  66. 'unordered(hierarchy_radio.lvl1)',
  67. 'unordered(hierarchy_radio_camel.lvl2)',
  68. 'unordered(hierarchy_radio.lvl2)',
  69. 'unordered(hierarchy_radio_camel.lvl3)',
  70. 'unordered(hierarchy_radio.lvl3)',
  71. 'unordered(hierarchy_radio_camel.lvl4)',
  72. 'unordered(hierarchy_radio.lvl4)',
  73. 'unordered(hierarchy_radio_camel.lvl5)',
  74. 'unordered(hierarchy_radio.lvl5)',
  75. 'unordered(hierarchy_radio_camel.lvl6)',
  76. 'unordered(hierarchy_radio.lvl6)',
  77. 'unordered(hierarchy_camel.lvl0)',
  78. 'unordered(hierarchy.lvl0)',
  79. 'unordered(hierarchy_camel.lvl1)',
  80. 'unordered(hierarchy.lvl1)',
  81. 'unordered(hierarchy_camel.lvl2)',
  82. 'unordered(hierarchy.lvl2)',
  83. 'unordered(hierarchy_camel.lvl3)',
  84. 'unordered(hierarchy.lvl3)',
  85. 'unordered(hierarchy_camel.lvl4)',
  86. 'unordered(hierarchy.lvl4)',
  87. 'unordered(hierarchy_camel.lvl5)',
  88. 'unordered(hierarchy.lvl5)',
  89. 'unordered(hierarchy_camel.lvl6)',
  90. 'unordered(hierarchy.lvl6)',
  91. 'content',
  92. ],
  93. distinct: true,
  94. attributeForDistinct: 'url',
  95. customRanking: [
  96. 'desc(weight.pageRank)',
  97. 'desc(weight.level)',
  98. 'asc(weight.position)',
  99. ],
  100. ranking: [
  101. 'words',
  102. 'filters',
  103. 'typo',
  104. 'attribute',
  105. 'proximity',
  106. 'exact',
  107. 'custom',
  108. ],
  109. highlightPreTag: '<span class="algolia-docsearch-suggestion--highlight">',
  110. highlightPostTag: '</span>',
  111. minWordSizefor1Typo: 3,
  112. minWordSizefor2Typos: 7,
  113. allowTyposOnNumericTokens: false,
  114. minProximity: 1,
  115. ignorePlurals: true,
  116. advancedSyntax: true,
  117. attributeCriteriaComputedByMinProximity: true,
  118. removeWordsIfNoResults: 'allOptional',
  119. },
  120. },
  121. })

上述 recordProps 是用于默认主题的配置,你可以根据你使用的主题来修改它们。

注意 initialIndexSettings.YOUR_INDEX_NAME.attributesForFaceting 字段必须包含 'lang',否则该插件将无法正常工作。

:::

::: tip 如果你使用的不是默认主题,或者在使用 Docsearch 的时候遇到了任何问题,你也可以检查上述的爬虫配置示例,然后前往 Algolia Crawler 仓库,在你项目侧边栏中的 Editor 页面中修改你的配置。 :::

配置项

apiKey

  • 类型: string

  • 是否必需: true

  • 详情:

    从 DocSearch 团队收到的 apiKey ,或者由你自己生成。

  • 参考:

indexName

  • 类型: string

  • 是否必需: true

  • 详情:

    从 DocSearch 团队收到的 indexName ,或者由你自己生成。

  • 参考:

appId

searchParameters

placeholder

disableUserPersonalization

initialQuery

translations

locales

  • 类型: Record<string, DocsearchPluginOptions>

  • 详情:

    在不同 locales 下对该插件进行不同的配置。

    该插件的所有其他选项都可以在 locale 中进行配置。

  • 示例:

  1. module.exports = {
  2. plugins: [
  3. docsearchPlugin({
  4. apiKey: '<API_KEY>',
  5. indexName: '<INDEX_NAME>',
  6. locales: {
  7. '/': {
  8. placeholder: 'Search Documentation',
  9. translations: {
  10. button: {
  11. buttonText: 'Search Documentation',
  12. },
  13. },
  14. },
  15. '/zh/': {
  16. placeholder: '搜索文档',
  17. translations: {
  18. button: {
  19. buttonText: '搜索文档',
  20. },
  21. },
  22. },
  23. },
  24. }),
  25. ],
  26. }

样式

你可以通过 @docsearch/css 提供的 CSS 变量来自定义样式:

  1. :root {
  2. --docsearch-primary-color: rgb(84, 104, 255);
  3. --docsearch-text-color: rgb(28, 30, 33);
  4. --docsearch-spacing: 12px;
  5. --docsearch-icon-stroke-width: 1.4;
  6. --docsearch-highlight-color: var(--docsearch-primary-color);
  7. --docsearch-muted-color: rgb(150, 159, 175);
  8. --docsearch-container-background: rgba(101, 108, 133, 0.8);
  9. --docsearch-logo-color: rgba(84, 104, 255);
  10. /* modal */
  11. --docsearch-modal-width: 560px;
  12. --docsearch-modal-height: 600px;
  13. --docsearch-modal-background: rgb(245, 246, 247);
  14. --docsearch-modal-shadow: inset 1px 1px 0 0 rgba(255, 255, 255, 0.5), 0 3px
  15. 8px 0 rgba(85, 90, 100, 1);
  16. /* searchbox */
  17. --docsearch-searchbox-height: 56px;
  18. --docsearch-searchbox-background: rgb(235, 237, 240);
  19. --docsearch-searchbox-focus-background: #fff;
  20. --docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color);
  21. /* hit */
  22. --docsearch-hit-height: 56px;
  23. --docsearch-hit-color: rgb(68, 73, 80);
  24. --docsearch-hit-active-color: #fff;
  25. --docsearch-hit-background: #fff;
  26. --docsearch-hit-shadow: 0 1px 3px 0 rgb(212, 217, 225);
  27. /* key */
  28. --docsearch-key-gradient: linear-gradient(
  29. -225deg,
  30. rgb(213, 219, 228) 0%,
  31. rgb(248, 248, 248) 100%
  32. );
  33. --docsearch-key-shadow: inset 0 -2px 0 0 rgb(205, 205, 230), inset 0 0 1px 1px
  34. #fff, 0 1px 2px 1px rgba(30, 35, 90, 0.4);
  35. /* footer */
  36. --docsearch-footer-height: 44px;
  37. --docsearch-footer-background: #fff;
  38. --docsearch-footer-shadow: 0 -1px 0 0 rgb(224, 227, 232), 0 -3px 6px 0 rgba(69, 98, 155, 0.12);
  39. }

组件

Docsearch

  • 详情:

    该插件会全局注册一个 <Docsearch /> 组件,你可以不传入任何 Props 来使用它。

    将该组件放置在你想要显示 docsearch 按钮的地方。例如,默认主题将这个组件放在了导航栏的末尾。

::: tip 该组件主要用于主题开发。在大多数情况下你不需要直接使用该组件。 :::