|
| 1 | +# search |
| 2 | + |
| 3 | +<NpmBadge package="@vuepress/plugin-search" /> |
| 4 | + |
| 5 | +Provide local search to your documentation site. |
| 6 | + |
| 7 | +::: tip |
| 8 | +Default theme will add search box to the navbar once you configure this plugin correctly. |
| 9 | + |
| 10 | +This plugin may not be used directly in other themes, so you'd better refer to the documentation of your theme for more details. |
| 11 | +::: |
| 12 | + |
| 13 | +## Local Search Index |
| 14 | + |
| 15 | +This plugin will generate search index from your pages locally, and load the search index file when users enter your site. In other words, this is a lightweight built-in search which does not require any external requests. |
| 16 | + |
| 17 | +However, when your site has a large number of pages, the size of search index file would be very large, which could slow down the page loading speed. In this case, we recommend you to use a more professional solution - [docsearch](./docsearch.md). |
| 18 | + |
| 19 | +## Options |
| 20 | + |
| 21 | +### locales |
| 22 | + |
| 23 | +- Type: `Record<string, { placeholder: string }>` |
| 24 | + |
| 25 | +- Details: |
| 26 | + |
| 27 | + The text of the search box in different locales. |
| 28 | + |
| 29 | + If this option is not specified, it will fallback to default text. |
| 30 | + |
| 31 | +- Example: |
| 32 | + |
| 33 | +```js |
| 34 | +module.exports = { |
| 35 | + plugins: [ |
| 36 | + [ |
| 37 | + '@vuepress/plugin-search', |
| 38 | + { |
| 39 | + locales: { |
| 40 | + '/': { |
| 41 | + placeholder: 'Search', |
| 42 | + }, |
| 43 | + '/zh/': { |
| 44 | + placeholder: '搜索', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + ], |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +- Also see: |
| 54 | + - [Guide > I18n](../../guide/i18n.md) |
| 55 | + |
| 56 | +### hotKeys |
| 57 | + |
| 58 | +- Type: `string[]` |
| 59 | + |
| 60 | +- Default: `['s', '/']` |
| 61 | + |
| 62 | +- Details: |
| 63 | + |
| 64 | + Specify the [event.key](http://keycode.info/) of the hotkeys. |
| 65 | + |
| 66 | + When hotkeys are pressed, the search box input will be focused. |
| 67 | + |
| 68 | + Set to an empty array to disable hotkeys. |
| 69 | + |
| 70 | +### maxSuggestions |
| 71 | + |
| 72 | +- Type: `number` |
| 73 | + |
| 74 | +- Default: `5` |
| 75 | + |
| 76 | +- Details: |
| 77 | + |
| 78 | + Specify the maximum number of search results. |
| 79 | + |
| 80 | +### isSearchable |
| 81 | + |
| 82 | +- Type: `(page: Page) => boolean` |
| 83 | + |
| 84 | +- Default: `() => true` |
| 85 | + |
| 86 | +- Details: |
| 87 | + |
| 88 | + A function to determine whether a page should be included in the search index. |
| 89 | + |
| 90 | + - Return `true` to include the page. |
| 91 | + - Return `false` to exclude the page. |
| 92 | + |
| 93 | +- Example: |
| 94 | + |
| 95 | +```js |
| 96 | + |
| 97 | +module.exports = { |
| 98 | + plugins: [ |
| 99 | + [ |
| 100 | + '@vuepress/plugin-search', |
| 101 | + { |
| 102 | + // exclude the homepage |
| 103 | + isSearchable: (page) => page.path !== '/', |
| 104 | + }, |
| 105 | + ], |
| 106 | + ], |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### getExtraFields |
| 111 | + |
| 112 | +- Type: `(page: Page) => string[]` |
| 113 | + |
| 114 | +- Default: `() => []` |
| 115 | + |
| 116 | +- Details: |
| 117 | + |
| 118 | + A function to add extra fields to the search index of a page. |
| 119 | + |
| 120 | + By default, this plugin will use page title and headers as the search index. This option could help you to add more searchable fields. |
| 121 | + |
| 122 | +- Example: |
| 123 | + |
| 124 | +```js |
| 125 | + |
| 126 | +module.exports = { |
| 127 | + plugins: [ |
| 128 | + [ |
| 129 | + '@vuepress/plugin-search', |
| 130 | + { |
| 131 | + // allow searching the `tags` frontmatter |
| 132 | + getExtraFields: (page) => page.frontmatter.tags ?? [], |
| 133 | + }, |
| 134 | + ], |
| 135 | + ], |
| 136 | +} |
| 137 | +``` |
| 138 | +
|
| 139 | +## Styles |
| 140 | +
|
| 141 | +You can customize the style of the search box via CSS variables: |
| 142 | +
|
| 143 | +```css |
| 144 | +:root { |
| 145 | + --search-accent-color: #3eaf7c; |
| 146 | + --search-text-color: #2c3e50; |
| 147 | + --search-border-color: #eaecef; |
| 148 | + |
| 149 | + --search-item-text-color: #5d81a5; |
| 150 | + --search-item-focus-bg-color: #f3f4f5; |
| 151 | + |
| 152 | + --search-input-width: 8rem; |
| 153 | + --search-result-width: 20rem; |
| 154 | +} |
| 155 | +``` |
| 156 | +
|
| 157 | +## Components |
| 158 | +
|
| 159 | +### SearchBox |
| 160 | +
|
| 161 | +- Details: |
| 162 | +
|
| 163 | + This plugin will register a `<SearchBox />` component globally, and you can use it without any props. |
| 164 | +
|
| 165 | + Put this component to where you want to place the search box. For example, default theme puts this component to the end of the navbar. |
| 166 | +
|
| 167 | +::: tip |
| 168 | +This component is mainly used for theme development. You don't need to use it directly in most cases. |
| 169 | +::: |
0 commit comments