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.
419 lines
13 KiB
419 lines
13 KiB
---
|
|
outline: deep
|
|
---
|
|
|
|
# Buscar {#search}
|
|
|
|
## Busqueda local {#local-search}
|
|
|
|
VitePress admite la búsqueda de texto completo utilizando un índice en el navegador gracias a [minisearch](https://github.com/lucaong/minisearch/). Para habilitar esta función, simplemente configure la opción `themeConfig.search.provider` como `'local'` en el archivo `.vitepress/config.ts`:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local'
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
Resultado de ejemplo:
|
|
|
|

|
|
|
|
Alternativamente, puedes usar [Algolia DocSearch](#algolia-search) o algunos complementos comunitarios como <https://www.npmjs.com/package/vitepress-plugin-search> o <https://www.npmjs.com/package/vitepress-plugin-pagefind>.
|
|
|
|
### i18n {#local-search-i18n}
|
|
|
|
Puede utilizar una configuración como esta para utilizar la búsqueda multilingüe:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local',
|
|
options: {
|
|
locales: {
|
|
zh: {
|
|
translations: {
|
|
button: {
|
|
buttonText: '搜索文档',
|
|
buttonAriaLabel: '搜索文档'
|
|
},
|
|
modal: {
|
|
noResultsText: '无法找到相关结果',
|
|
resetButtonTitle: '清除查询条件',
|
|
footer: {
|
|
selectText: '选择',
|
|
navigateText: '切换'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
### Opciones MiniSearch {#mini-search-options}
|
|
|
|
Puedes configurar MiniSearch de esta manera:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local',
|
|
options: {
|
|
miniSearch: {
|
|
/**
|
|
* @type {Pick<import('minisearch').Options, 'extractField' | 'tokenize' | 'processTerm'>}
|
|
*/
|
|
options: {
|
|
/* ... */
|
|
},
|
|
/**
|
|
* @type {import('minisearch').SearchOptions}
|
|
* @default
|
|
* { fuzzy: 0.2, prefix: true, boost: { title: 4, text: 2, titles: 1 } }
|
|
*/
|
|
searchOptions: {
|
|
/* ... */
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
Obtenga más información en [documentación de MiniSearch](https://lucaong.github.io/minisearch/classes/MiniSearch.MiniSearch.html).
|
|
|
|
### Presentador de contenido personalizado {#custom-content-renderer}
|
|
|
|
Puedes personalizar la función utilizada para presentar el contenido de rebajas antes de indexarlo:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local',
|
|
options: {
|
|
/**
|
|
* @param {string} src
|
|
* @param {import('vitepress').MarkdownEnv} env
|
|
* @param {import('markdown-it-async')} md
|
|
*/
|
|
async _render(src, env, md) {
|
|
// retorne un string HTML
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
Esta función se eliminará de los datos del sitio web en el lado del cliente, por lo que podrá utilizar las API de Node.js en ella.
|
|
|
|
#### Ejemplo: Excluir páginas de la busqueda {#example-excluding-pages-from-search}
|
|
|
|
Puedes excluir páginas de la busqueda adicionando `search: false` al principio de la página. Alternativamente:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local',
|
|
options: {
|
|
async _render(src, env, md) {
|
|
const html = await md.renderAsync(src, env)
|
|
if (env.frontmatter?.search === false) return ''
|
|
if (env.relativePath.startsWith('algum/caminho')) return ''
|
|
return html
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
::: warning Nota
|
|
En este caso, una función `_render` se proporciona, es necesario manipular el `search: false` desde el frente por su cuenta. Además, el objeto `env` no estará completamente poblado antes que `md.renderAsync` se llama, luego verifica las propiedades opcionales `env`, como `frontmatter`, debe hacerse después de eso.
|
|
:::
|
|
|
|
#### Ejemplo: Transformar contenido - agregar anclajes {#example-transforming-content-adding-anchors}
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'local',
|
|
options: {
|
|
async _render(src, env, md) {
|
|
const html = await md.renderAsync(src, env)
|
|
if (env.frontmatter?.title)
|
|
return await md.renderAsync(`# ${env.frontmatter.title}`) + html
|
|
return html
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
## Busqueda de Algolia {#algolia-search}
|
|
|
|
VitePress admite la búsqueda en su sitio de documentación utilizando [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Consulte su guía de introducción. en tu archivo `.vitepress/config.ts`, Deberá proporcionar al menos lo siguiente para que funcione:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'algolia',
|
|
options: {
|
|
appId: '...',
|
|
apiKey: '...',
|
|
indexName: '...'
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
### i18n {#algolia-search-i18n} {#algolia-search-i18n}
|
|
|
|
Puedes utilizar una configuración como esta para utilizar la búsqueda multilingüe:
|
|
|
|
```ts
|
|
import { defineConfig } from 'vitepress'
|
|
|
|
export default defineConfig({
|
|
themeConfig: {
|
|
search: {
|
|
provider: 'algolia',
|
|
options: {
|
|
appId: '...',
|
|
apiKey: '...',
|
|
indexName: '...',
|
|
locales: {
|
|
zh: {
|
|
placeholder: '搜索文档',
|
|
translations: {
|
|
button: { buttonText: '搜索文档', buttonAriaLabel: '搜索文档' },
|
|
modal: {
|
|
searchBox: {
|
|
clearButtonTitle: '清除查询条件',
|
|
clearButtonAriaLabel: '清除查询条件',
|
|
closeButtonText: '关闭',
|
|
closeButtonAriaLabel: '关闭',
|
|
placeholderText: '搜索文档',
|
|
placeholderTextAskAi: '向 AI 提问:',
|
|
placeholderTextAskAiStreaming: '回答中...',
|
|
searchInputLabel: '搜索',
|
|
backToKeywordSearchButtonText: '返回关键字搜索',
|
|
backToKeywordSearchButtonAriaLabel: '返回关键字搜索'
|
|
},
|
|
startScreen: {
|
|
recentSearchesTitle: '搜索历史',
|
|
noRecentSearchesText: '没有搜索历史',
|
|
saveRecentSearchButtonTitle: '保存至搜索历史',
|
|
removeRecentSearchButtonTitle: '从搜索历史中移除',
|
|
favoriteSearchesTitle: '收藏',
|
|
removeFavoriteSearchButtonTitle: '从收藏中移除',
|
|
recentConversationsTitle: '最近的对话',
|
|
removeRecentConversationButtonTitle: '从历史记录中删除对话'
|
|
},
|
|
errorScreen: {
|
|
titleText: '无法获取结果',
|
|
helpText: '你可能需要检查你的网络连接'
|
|
},
|
|
noResultsScreen: {
|
|
noResultsText: '无法找到相关结果',
|
|
suggestedQueryText: '你可以尝试查询',
|
|
reportMissingResultsText: '你认为该查询应该有结果?',
|
|
reportMissingResultsLinkText: '点击反馈'
|
|
},
|
|
resultsScreen: { askAiPlaceholder: '向 AI 提问: ' },
|
|
askAiScreen: {
|
|
disclaimerText: '答案由 AI 生成,可能不准确,请自行验证。',
|
|
relatedSourcesText: '相关来源',
|
|
thinkingText: '思考中...',
|
|
copyButtonText: '复制',
|
|
copyButtonCopiedText: '已复制!',
|
|
copyButtonTitle: '复制',
|
|
likeButtonTitle: '赞',
|
|
dislikeButtonTitle: '踩',
|
|
thanksForFeedbackText: '感谢你的反馈!',
|
|
preToolCallText: '搜索中...',
|
|
duringToolCallText: '搜索 ',
|
|
afterToolCallText: '已搜索'
|
|
},
|
|
footer: {
|
|
selectText: '选择',
|
|
submitQuestionText: '提交问题',
|
|
selectKeyAriaLabel: 'Enter 键',
|
|
navigateText: '切换',
|
|
navigateUpKeyAriaLabel: '向上箭头',
|
|
navigateDownKeyAriaLabel: '向下箭头',
|
|
closeText: '关闭',
|
|
backToSearchText: '返回搜索',
|
|
closeKeyAriaLabel: 'Esc 键',
|
|
poweredByText: '搜索提供者'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
```
|
|
|
|
### Algolia Ask AI Support {#ask-ai}
|
|
|
|
Si deseas incluir **Ask AI**, pasa la opción `askAi` (o alguno de sus campos parciales) dentro de `options`:
|
|
|
|
```ts
|
|
options: {
|
|
appId: '...',
|
|
apiKey: '...',
|
|
indexName: '...',
|
|
// askAi: 'TU-ASSISTANT-ID'
|
|
askAi: {
|
|
assistantId: 'XXXYYY'
|
|
}
|
|
}
|
|
```
|
|
|
|
::: warning Nota
|
|
Si prefieres solo la búsqueda por palabra clave y no la Ask AI, simplemente omite `askAi`.
|
|
:::
|
|
|
|
[Estas opciones](https://github.com/vuejs/vitepress/blob/main/types/docsearch.d.ts) se pueden superponer. Consulte la documentación oficial de Algolia para obtener más información sobre ellos.
|
|
|
|
### Configuración _Crawler_ {#crawler-config}
|
|
|
|
A continuación se muestra un ejemplo de la configuración que utiliza este sitio:
|
|
|
|
```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: 'section.has-active div h2',
|
|
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'
|
|
}
|
|
}
|
|
})
|
|
```
|