|
| 1 | +--- |
| 2 | +title: Búsqueda en el sitio |
| 3 | +description: Aprende sobre las funciones de búsqueda de sitios integradas de Starlight y cómo personalizarlas. |
| 4 | +sidebar: |
| 5 | + badge: Nuevo |
| 6 | +--- |
| 7 | + |
| 8 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 9 | + |
| 10 | +Por defecto, los sitios de Starlight incluyen una búsqueda de texto completo impulsada por [Pagefind](https://pagefind.app/), que es una herramienta de búsqueda rápida y de bajo ancho de banda para sitios estáticos. |
| 11 | + |
| 12 | +No es necesaria ninguna configuración para habilitar la búsqueda. Construye e implementa tu sitio, luego usa la barra de búsqueda en el encabezado del sitio para encontrar contenido. |
| 13 | + |
| 14 | +## Ocultar contenido en los resultados de búsqueda |
| 15 | + |
| 16 | +### Excluye una página |
| 17 | + |
| 18 | +Para excluir una página del índice de búsqueda, agrega [`pagefind: false`](/reference/frontmatter/#pagefind) al frontmatter de la página: |
| 19 | + |
| 20 | +```md title="src/content/docs/not-indexed.md" ins={3} |
| 21 | +--- |
| 22 | +title: Contenido para ocultar de la búsqueda |
| 23 | +pagefind: false |
| 24 | +--- |
| 25 | +``` |
| 26 | + |
| 27 | +### Excluye parte de una página |
| 28 | + |
| 29 | +Pagefind ignorará el contenido dentro de un elemento con el atributo [`data-pagefind-ignore`](https://pagefind.app/docs/indexing/#removing-individual-elements-from-the-index). |
| 30 | + |
| 31 | +En el siguiente ejemplo, el primer párrafo se mostrará en los resultados de búsqueda, pero el contenido del `<div>` no: |
| 32 | + |
| 33 | +```md title="src/content/docs/partially-indexed.md" ins="data-pagefind-ignore" |
| 34 | +--- |
| 35 | +title: Página parcialmente indexada |
| 36 | +--- |
| 37 | + |
| 38 | +Este texto será descubrible a través de la búsqueda. |
| 39 | + |
| 40 | +<div data-pagefind-ignore> |
| 41 | + |
| 42 | +Este texto estará oculto de la búsqueda. |
| 43 | + |
| 44 | +</div> |
| 45 | +``` |
| 46 | + |
| 47 | +## Proveedores de búsqueda alternativos |
| 48 | + |
| 49 | +### Algolia DocSearch |
| 50 | + |
| 51 | +Si tienes acceso al [programa DocSearch de Algolia](https://docsearch.algolia.com/) y quieres usarlo en lugar de Pagefind, puedes usar el plugin oficial Starlight DocSearch. |
| 52 | + |
| 53 | +1. Instala `@astrojs/starlight-docsearch`: |
| 54 | + |
| 55 | + <Tabs> |
| 56 | + |
| 57 | + <TabItem label="npm"> |
| 58 | + |
| 59 | + ```sh |
| 60 | + npm install @astrojs/starlight-docsearch |
| 61 | + ``` |
| 62 | + |
| 63 | + </TabItem> |
| 64 | + |
| 65 | + <TabItem label="pnpm"> |
| 66 | + |
| 67 | + ```sh |
| 68 | + pnpm install @astrojs/starlight-docsearch |
| 69 | + ``` |
| 70 | + |
| 71 | + </TabItem> |
| 72 | + |
| 73 | + <TabItem label="Yarn"> |
| 74 | + |
| 75 | + ```sh |
| 76 | + yarn add @astrojs/starlight-docsearch |
| 77 | + ``` |
| 78 | + |
| 79 | + </TabItem> |
| 80 | + |
| 81 | + </Tabs> |
| 82 | + |
| 83 | +2. Agrega DocSearch a tu configuración de Starlight [`plugins`](/es/reference/configuration/#plugins) en `astro.config.mjs` y pásale tu `appId`, `apiKey` e `indexName` de Algolia: |
| 84 | + |
| 85 | + ```js ins={4,10-16} |
| 86 | + // astro.config.mjs |
| 87 | + import { defineConfig } from 'astro/config'; |
| 88 | + import starlight from '@astrojs/starlight'; |
| 89 | + import starlightDocSearch from '@astrojs/starlight-docsearch'; |
| 90 | + |
| 91 | + export default defineConfig({ |
| 92 | + integrations: [ |
| 93 | + starlight({ |
| 94 | + title: 'Sitio con DocSearch', |
| 95 | + plugins: [ |
| 96 | + starlightDocSearch({ |
| 97 | + appId: 'TU_ID_DE_APP' |
| 98 | + apiKey: 'TU_LLAVE_DE_API_DE_BÚSQUEDA' |
| 99 | + indexName: 'TU_NOMBRE_DE_ÍNDICE', |
| 100 | + }), |
| 101 | + ], |
| 102 | + }), |
| 103 | + ], |
| 104 | + }); |
| 105 | + ``` |
| 106 | + |
| 107 | +Con esta configuración actualizada, la barra de búsqueda de tu sitio ahora abrirá un modal de Algolia en lugar del modal de búsqueda predeterminado. |
| 108 | + |
| 109 | +#### Traduciendo la UI de DocSearch |
| 110 | + |
| 111 | +DocSearch solo proporciona cadenas de UI en inglés de forma predeterminada. |
| 112 | +Para agregar traducciones de la UI al modal en otros idiomas, usa el [sistema de internacionalización](/guides/i18n/#traduce-la-ui-de-starlight) incorporado de Starlight. |
| 113 | + |
| 114 | +1. Extiende la definición de la colección de contenido `i18n` de Starlight con el esquema de DocSearch en `src/content/config.ts`: |
| 115 | + |
| 116 | + ```js ins={4} ins=/{ extend: .+ }/ |
| 117 | + // src/content/config.ts |
| 118 | + import { defineCollection } from 'astro:content'; |
| 119 | + import { docsSchema, i18nSchema } from '@astrojs/starlight/schema'; |
| 120 | + import { docSearchI18nSchema } from '@astrojs/starlight-docsearch/schema'; |
| 121 | + |
| 122 | + export const collections = { |
| 123 | + docs: defineCollection({ schema: docsSchema() }), |
| 124 | + i18n: defineCollection({ |
| 125 | + type: 'data', |
| 126 | + schema: i18nSchema({ extend: docSearchI18nSchema() }), |
| 127 | + }), |
| 128 | + }; |
| 129 | + ``` |
| 130 | + |
| 131 | +2. Agrega traducciones a tus archivos JSON en `src/content/i18n/`. |
| 132 | + |
| 133 | + Estas son las predeterminadas en inglés que usa DocSearch: |
| 134 | + |
| 135 | + ```json title="src/content/i18n/en.json" |
| 136 | + { |
| 137 | + "docsearch.searchBox.resetButtonTitle": "Clear the query", |
| 138 | + "docsearch.searchBox.resetButtonAriaLabel": "Clear the query", |
| 139 | + "docsearch.searchBox.cancelButtonText": "Cancel", |
| 140 | + "docsearch.searchBox.cancelButtonAriaLabel": "Cancel", |
| 141 | + |
| 142 | + "docsearch.startScreen.recentSearchesTitle": "Recent", |
| 143 | + "docsearch.startScreen.noRecentSearchesText": "No recent searches", |
| 144 | + "docsearch.startScreen.saveRecentSearchButtonTitle": "Save this search", |
| 145 | + "docsearch.startScreen.removeRecentSearchButtonTitle": "Remove this search from history", |
| 146 | + "docsearch.startScreen.favoriteSearchesTitle": "Favorite", |
| 147 | + "docsearch.startScreen.removeFavoriteSearchButtonTitle": "Remove this search from favorites", |
| 148 | + |
| 149 | + "docsearch.errorScreen.titleText": "Unable to fetch results", |
| 150 | + "docsearch.errorScreen.helpText": "You might want to check your network connection.", |
| 151 | + |
| 152 | + "docsearch.footer.selectText": "to select", |
| 153 | + "docsearch.footer.selectKeyAriaLabel": "Enter key", |
| 154 | + "docsearch.footer.navigateText": "to navigate", |
| 155 | + "docsearch.footer.navigateUpKeyAriaLabel": "Arrow up", |
| 156 | + "docsearch.footer.navigateDownKeyAriaLabel": "Arrow down", |
| 157 | + "docsearch.footer.closeText": "to close", |
| 158 | + "docsearch.footer.closeKeyAriaLabel": "Escape key", |
| 159 | + "docsearch.footer.searchByText": "Search by", |
| 160 | + |
| 161 | + "docsearch.noResultsScreen.noResultsText": "No results for", |
| 162 | + "docsearch.noResultsScreen.suggestedQueryText": "Try searching for", |
| 163 | + "docsearch.noResultsScreen.reportMissingResultsText": "Believe this query should return results?", |
| 164 | + "docsearch.noResultsScreen.reportMissingResultsLinkText": "Let us know." |
| 165 | + } |
| 166 | + ``` |
0 commit comments