mirror of https://github.com/vuejs/vitepress
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
10 KiB
387 lines
10 KiB
1 year ago
|
---
|
||
|
outline: deep
|
||
|
---
|
||
|
|
||
2 years ago
|
# Search
|
||
|
|
||
2 years ago
|
## Local Search
|
||
2 years ago
|
|
||
2 years ago
|
VitePress supports fuzzy full-text search using a in-browser index thanks to [minisearch](https://github.com/lucaong/minisearch/). To enable this feature, simply set the `themeConfig.search.provider` option to `'local'` in your `.vitepress/config.ts` file:
|
||
2 years ago
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
2 years ago
|
search: {
|
||
|
provider: 'local'
|
||
|
}
|
||
2 years ago
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
Example result:
|
||
|
|
||
|
![screenshot of the search modal](/search.png)
|
||
|
|
||
|
Alternatively, you can use [Algolia DocSearch](#algolia-search) or some community plugins like <https://www.npmjs.com/package/vitepress-plugin-search> or <https://www.npmjs.com/package/vitepress-plugin-pagefind>.
|
||
2 years ago
|
|
||
2 years ago
|
### i18n {#local-search-i18n}
|
||
2 years ago
|
|
||
|
You can use a config like this to use multilingual search:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
2 years ago
|
search: {
|
||
|
provider: 'local',
|
||
|
options: {
|
||
|
locales: {
|
||
4 months ago
|
zh: { // make this `root` if you want to translate the default locale
|
||
2 years ago
|
translations: {
|
||
|
button: {
|
||
4 months ago
|
buttonText: '搜索',
|
||
|
buttonAriaLabel: '搜索'
|
||
2 years ago
|
},
|
||
|
modal: {
|
||
4 months ago
|
displayDetails: '显示详细列表',
|
||
|
resetButtonTitle: '重置搜索',
|
||
|
backButtonTitle: '关闭搜索',
|
||
|
noResultsText: '没有结果',
|
||
2 years ago
|
footer: {
|
||
|
selectText: '选择',
|
||
4 months ago
|
selectKeyAriaLabel: '输入',
|
||
|
navigateText: '导航',
|
||
|
navigateUpKeyAriaLabel: '上箭头',
|
||
|
navigateDownKeyAriaLabel: '下箭头',
|
||
|
closeText: '关闭',
|
||
|
closeKeyAriaLabel: 'esc'
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
1 year ago
|
### miniSearch options
|
||
|
|
||
|
You can configure MiniSearch like this:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
|
search: {
|
||
|
provider: 'local',
|
||
|
options: {
|
||
|
miniSearch: {
|
||
|
/**
|
||
|
* @type {Pick<import('minisearch').Options, 'extractField' | 'tokenize' | 'processTerm'>}
|
||
|
*/
|
||
1 year ago
|
options: {
|
||
|
/* ... */
|
||
|
},
|
||
1 year ago
|
/**
|
||
|
* @type {import('minisearch').SearchOptions}
|
||
|
* @default
|
||
|
* { fuzzy: 0.2, prefix: true, boost: { title: 4, text: 2, titles: 1 } }
|
||
|
*/
|
||
1 year ago
|
searchOptions: {
|
||
|
/* ... */
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
1 year ago
|
Learn more in [MiniSearch docs](https://lucaong.github.io/minisearch/classes/MiniSearch.MiniSearch.html).
|
||
1 year ago
|
|
||
1 year ago
|
### Custom content renderer
|
||
1 year ago
|
|
||
1 year ago
|
You can customize the function used to render the markdown content before indexing it:
|
||
1 year ago
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
|
search: {
|
||
1 year ago
|
provider: 'local',
|
||
|
options: {
|
||
|
/**
|
||
|
* @param {string} src
|
||
|
* @param {import('vitepress').MarkdownEnv} env
|
||
|
* @param {import('markdown-it')} md
|
||
|
*/
|
||
|
_render(src, env, md) {
|
||
|
// return html string
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
This function will be stripped from client-side site data, so you can use Node.js APIs in it.
|
||
|
|
||
|
#### Example: Excluding pages from search
|
||
|
|
||
|
You can exclude pages from search by adding `search: false` to the frontmatter of the page. Alternatively:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
|
search: {
|
||
|
provider: 'local',
|
||
1 year ago
|
options: {
|
||
1 year ago
|
_render(src, env, md) {
|
||
|
const html = md.render(src, env)
|
||
|
if (env.frontmatter?.search === false) return ''
|
||
|
if (env.relativePath.startsWith('some/path')) return ''
|
||
|
return html
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
::: warning Note
|
||
|
In case a custom `_render` function is provided, you need to handle the `search: false` frontmatter yourself. Also, the `env` object won't be completely populated before `md.render` is called, so any checks on optional `env` properties like `frontmatter` should be done after that.
|
||
|
:::
|
||
|
|
||
|
#### Example: Transforming content - adding anchors
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
|
search: {
|
||
|
provider: 'local',
|
||
|
options: {
|
||
|
_render(src, env, md) {
|
||
|
const html = md.render(src, env)
|
||
|
if (env.frontmatter?.title)
|
||
|
return md.render(`# ${env.frontmatter.title}`) + html
|
||
|
return html
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
2 years ago
|
## Algolia Search
|
||
|
|
||
2 years ago
|
VitePress supports searching your docs site using [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Refer their getting started guide. In your `.vitepress/config.ts` you'll need to provide at least the following to make it work:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
2 years ago
|
search: {
|
||
|
provider: 'algolia',
|
||
|
options: {
|
||
|
appId: '...',
|
||
|
apiKey: '...',
|
||
|
indexName: '...'
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
2 years ago
|
### i18n {#algolia-search-i18n}
|
||
2 years ago
|
|
||
|
You can use a config like this to use multilingual search:
|
||
|
|
||
|
```ts
|
||
|
import { defineConfig } from 'vitepress'
|
||
|
|
||
|
export default defineConfig({
|
||
|
themeConfig: {
|
||
2 years ago
|
search: {
|
||
|
provider: 'algolia',
|
||
|
options: {
|
||
|
appId: '...',
|
||
|
apiKey: '...',
|
||
|
indexName: '...',
|
||
|
locales: {
|
||
|
zh: {
|
||
|
placeholder: '搜索文档',
|
||
|
translations: {
|
||
|
button: {
|
||
|
buttonText: '搜索文档',
|
||
|
buttonAriaLabel: '搜索文档'
|
||
2 years ago
|
},
|
||
2 years ago
|
modal: {
|
||
|
searchBox: {
|
||
|
resetButtonTitle: '清除查询条件',
|
||
|
resetButtonAriaLabel: '清除查询条件',
|
||
|
cancelButtonText: '取消',
|
||
|
cancelButtonAriaLabel: '取消'
|
||
|
},
|
||
|
startScreen: {
|
||
|
recentSearchesTitle: '搜索历史',
|
||
|
noRecentSearchesText: '没有搜索历史',
|
||
|
saveRecentSearchButtonTitle: '保存至搜索历史',
|
||
|
removeRecentSearchButtonTitle: '从搜索历史中移除',
|
||
|
favoriteSearchesTitle: '收藏',
|
||
|
removeFavoriteSearchButtonTitle: '从收藏中移除'
|
||
|
},
|
||
|
errorScreen: {
|
||
|
titleText: '无法获取结果',
|
||
|
helpText: '你可能需要检查你的网络连接'
|
||
|
},
|
||
|
footer: {
|
||
|
selectText: '选择',
|
||
|
navigateText: '切换',
|
||
|
closeText: '关闭',
|
||
|
searchByText: '搜索提供者'
|
||
|
},
|
||
|
noResultsScreen: {
|
||
|
noResultsText: '无法找到相关结果',
|
||
|
suggestedQueryText: '你可以尝试查询',
|
||
|
reportMissingResultsText: '你认为该查询应该有结果?',
|
||
|
reportMissingResultsLinkText: '点击反馈'
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
[These options](https://github.com/vuejs/vitepress/blob/main/types/docsearch.d.ts) can be overridden. Refer official Algolia docs to learn more about them.
|
||
2 years ago
|
|
||
1 year ago
|
### Crawler Config
|
||
|
|
||
|
Here is an example config based on what this site uses:
|
||
|
|
||
|
```ts
|
||
|
new Crawler({
|
||
|
appId: '...',
|
||
|
apiKey: '...',
|
||
|
rateLimit: 8,
|
||
|
startUrls: ['https://vitepress.dev/'],
|
||
|
renderJavaScript: false,
|
||
|
sitemaps: [],
|
||
|
exclusionPatterns: [],
|
||
|
ignoreCanonicalTo: false,
|
||
|
discoveryPatterns: ['https://vitepress.dev/**'],
|
||
|
schedule: 'at 05:10 on Saturday',
|
||
|
actions: [
|
||
|
{
|
||
|
indexName: 'vitepress',
|
||
|
pathsToMatch: ['https://vitepress.dev/**'],
|
||
|
recordExtractor: ({ $, helpers }) => {
|
||
|
return helpers.docsearch({
|
||
|
recordProps: {
|
||
|
lvl1: '.content h1',
|
||
|
content: '.content p, .content li',
|
||
|
lvl0: {
|
||
|
selectors: '',
|
||
|
defaultValue: 'Documentation'
|
||
|
},
|
||
|
lvl2: '.content h2',
|
||
|
lvl3: '.content h3',
|
||
|
lvl4: '.content h4',
|
||
|
lvl5: '.content h5'
|
||
|
},
|
||
|
indexHeadings: true
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
],
|
||
|
initialIndexSettings: {
|
||
|
vitepress: {
|
||
|
attributesForFaceting: ['type', 'lang'],
|
||
|
attributesToRetrieve: ['hierarchy', 'content', 'anchor', 'url'],
|
||
|
attributesToHighlight: ['hierarchy', 'hierarchy_camel', 'content'],
|
||
|
attributesToSnippet: ['content:10'],
|
||
|
camelCaseAttributes: ['hierarchy', 'hierarchy_radio', 'content'],
|
||
|
searchableAttributes: [
|
||
|
'unordered(hierarchy_radio_camel.lvl0)',
|
||
|
'unordered(hierarchy_radio.lvl0)',
|
||
|
'unordered(hierarchy_radio_camel.lvl1)',
|
||
|
'unordered(hierarchy_radio.lvl1)',
|
||
|
'unordered(hierarchy_radio_camel.lvl2)',
|
||
|
'unordered(hierarchy_radio.lvl2)',
|
||
|
'unordered(hierarchy_radio_camel.lvl3)',
|
||
|
'unordered(hierarchy_radio.lvl3)',
|
||
|
'unordered(hierarchy_radio_camel.lvl4)',
|
||
|
'unordered(hierarchy_radio.lvl4)',
|
||
|
'unordered(hierarchy_radio_camel.lvl5)',
|
||
|
'unordered(hierarchy_radio.lvl5)',
|
||
|
'unordered(hierarchy_radio_camel.lvl6)',
|
||
|
'unordered(hierarchy_radio.lvl6)',
|
||
|
'unordered(hierarchy_camel.lvl0)',
|
||
|
'unordered(hierarchy.lvl0)',
|
||
|
'unordered(hierarchy_camel.lvl1)',
|
||
|
'unordered(hierarchy.lvl1)',
|
||
|
'unordered(hierarchy_camel.lvl2)',
|
||
|
'unordered(hierarchy.lvl2)',
|
||
|
'unordered(hierarchy_camel.lvl3)',
|
||
|
'unordered(hierarchy.lvl3)',
|
||
|
'unordered(hierarchy_camel.lvl4)',
|
||
|
'unordered(hierarchy.lvl4)',
|
||
|
'unordered(hierarchy_camel.lvl5)',
|
||
|
'unordered(hierarchy.lvl5)',
|
||
|
'unordered(hierarchy_camel.lvl6)',
|
||
|
'unordered(hierarchy.lvl6)',
|
||
|
'content'
|
||
|
],
|
||
|
distinct: true,
|
||
|
attributeForDistinct: 'url',
|
||
|
customRanking: [
|
||
|
'desc(weight.pageRank)',
|
||
|
'desc(weight.level)',
|
||
|
'asc(weight.position)'
|
||
|
],
|
||
|
ranking: [
|
||
|
'words',
|
||
|
'filters',
|
||
|
'typo',
|
||
|
'attribute',
|
||
|
'proximity',
|
||
|
'exact',
|
||
|
'custom'
|
||
|
],
|
||
|
highlightPreTag: '<span class="algolia-docsearch-suggestion--highlight">',
|
||
|
highlightPostTag: '</span>',
|
||
|
minWordSizefor1Typo: 3,
|
||
|
minWordSizefor2Typos: 7,
|
||
|
allowTyposOnNumericTokens: false,
|
||
|
minProximity: 1,
|
||
|
ignorePlurals: true,
|
||
|
advancedSyntax: true,
|
||
|
attributeCriteriaComputedByMinProximity: true,
|
||
|
removeWordsIfNoResults: 'allOptional'
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
2 years ago
|
<style>
|
||
|
img[src="/search.png"] {
|
||
|
width: 100%;
|
||
|
aspect-ratio: 1 / 1;
|
||
|
}
|
||
|
</style>
|