Skip to content

Commit

Permalink
feat(plugin-search): add built-in locale data (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Dec 29, 2024
1 parent 3c44f45 commit bdb9902
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 23 deletions.
4 changes: 1 addition & 3 deletions docs/plugins/search/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ However, when your site has a large number of pages, the size of search index fi

### locales

- Type: `Record<string, { placeholder: string }>`
- Type: `Record<string, { placeholder?: string }>`

- Details:

The text of the search box in different locales.

If this option is not specified, it will fallback to default text.

- Example:

```ts
Expand Down
4 changes: 1 addition & 3 deletions docs/zh/plugins/search/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ export default {

### locales

- 类型: `Record<string, { placeholder: string }>`
- 类型: `Record<string, { placeholder?: string }>`

- 详情:

搜索框在不同 locales 下的文字。

如果没有指定该配置项,它会降级使用默认文字。

- 示例:

```ts
Expand Down
1 change: 1 addition & 0 deletions plugins/search/plugin-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"style": "sass src:lib --embed-sources --style=compressed"
},
"dependencies": {
"@vuepress/helper": "workspace:*",
"chokidar": "^3.6.0",
"vue": "^3.5.13"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PropType } from 'vue'
import { computed, defineComponent, h, ref, toRefs } from 'vue'
import { useRouteLocale, useRouter } from 'vuepress/client'
import type { LocaleConfig } from 'vuepress/shared'
import type { HotKeyOptions } from '../../shared/index.js'
import type { SearchSuggestion } from '../composables/index.js'
import {
Expand All @@ -10,17 +9,14 @@ import {
useSearchSuggestions,
useSuggestionsFocus,
} from '../composables/index.js'

export type SearchBoxLocales = LocaleConfig<{
placeholder: string
}>
import type { SearchPluginLocaleConfig } from '../types.js'

export const SearchBox = defineComponent({
name: 'SearchBox',

props: {
locales: {
type: Object as PropType<SearchBoxLocales>,
type: Object as PropType<SearchPluginLocaleConfig>,
default: () => ({}),
},

Expand Down
6 changes: 3 additions & 3 deletions plugins/search/plugin-search/src/client/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { h } from 'vue'
import { defineClientConfig } from 'vuepress/client'
import type { SearchBoxLocales } from './components/index.js'
import { SearchBox } from './components/index.js'
import type { SearchPluginLocaleConfig } from './types.js'

import './styles/vars.css'
import './styles/search.css'

declare const __SEARCH_LOCALES__: SearchBoxLocales
declare const __SEARCH_LOCALES__: SearchPluginLocaleConfig
declare const __SEARCH_HOT_KEYS__: string[]
declare const __SEARCH_MAX_SUGGESTIONS__: number

Expand All @@ -20,7 +20,7 @@ export default defineClientConfig({
app.component(
'SearchBox',
(props: {
locales?: SearchBoxLocales
locales?: SearchPluginLocaleConfig
hotKeys?: string[]
maxSuggestions?: number
}) =>
Expand Down
4 changes: 4 additions & 0 deletions plugins/search/plugin-search/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { ExactLocaleConfig } from '@vuepress/helper/client'
import type { SearchPluginLocaleData } from '../shared/index.js'

export type SearchPluginLocaleConfig = ExactLocaleConfig<SearchPluginLocaleData>
27 changes: 27 additions & 0 deletions plugins/search/plugin-search/src/node/locales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { DefaultLocaleInfo } from '@vuepress/helper'
import type { SearchPluginLocaleData } from '../shared/index.js'

/**
* Default locale info for `@vuepress/plugin-search`
*/
export const searchLocaleInfo: DefaultLocaleInfo<SearchPluginLocaleData> = [
[['en', 'en-US'], { placeholder: 'Search' }],
[['zh', 'zh-CN', 'zh-Hans', 'zh-TW', 'zh-Hant'], { placeholder: '搜索' }],
[['de', 'de-DE'], { placeholder: 'Suche' }],
[['de-AT'], { placeholder: 'Suche' }],
[['vi', 'vi-VN'], { placeholder: 'Tìm kiếm' }],
[['uk'], { placeholder: 'Пошук' }],
[['ru', 'ru-RU'], { placeholder: 'Поиск' }],
[['br'], { placeholder: 'Pesquisar' }],
[['pl', 'pl-PL'], { placeholder: 'Szukaj' }],
[['sk', 'sk-SK'], { placeholder: 'Hľadať' }],
[['fr', 'fr-FR'], { placeholder: 'Rechercher' }],
[['es', 'es-ES'], { placeholder: 'Buscar' }],
[['ja', 'ja-JP'], { placeholder: '検索' }],
[['tr', 'tr-TR'], { placeholder: 'Ara' }],
[['ko', 'ko-KO'], { placeholder: '검색' }],
[['fi', 'fi-FI'], { placeholder: 'Etsi' }],
[['hu', 'hu-HU'], { placeholder: 'Keresés' }],
[['id', 'id-ID'], { placeholder: 'Cari sesuatu' }],
[['nl', 'nl-NL'], { placeholder: 'Zoeken' }],
]
23 changes: 15 additions & 8 deletions plugins/search/plugin-search/src/node/searchPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getFullLocaleConfig } from '@vuepress/helper'
import { watch } from 'chokidar'
import type { Page, Plugin } from 'vuepress/core'
import type { LocaleConfig } from 'vuepress/shared'
import { getDirname, path } from 'vuepress/utils'
import type { HotKeyOptions } from '../shared/index.js'
import type { HotKeyOptions, SearchPluginLocaleData } from '../shared/index.js'
import { searchLocaleInfo } from './locales.js'
import { prepareSearchIndex } from './prepareSearchIndex.js'

const __dirname = import.meta.dirname || getDirname(import.meta.url)
Expand All @@ -14,9 +16,7 @@ export interface SearchPluginOptions {
/**
* Locales config for search box
*/
locales?: LocaleConfig<{
placeholder: string
}>
locales?: LocaleConfig<SearchPluginLocaleData>

/**
* Specify the [event.key](http://keycode.info/) of the hotkeys
Expand Down Expand Up @@ -47,22 +47,29 @@ export interface SearchPluginOptions {
getExtraFields?: (page: Page) => string[]
}

const PLUGIN_NAME = '@vuepress/plugin-search'

export const searchPlugin = ({
locales = {},
hotKeys = ['s', '/'],
maxSuggestions = 5,
isSearchable = () => true,
getExtraFields = () => [],
}: SearchPluginOptions = {}): Plugin => ({
name: '@vuepress/plugin-search',
name: PLUGIN_NAME,

clientConfigFile: path.resolve(__dirname, '../client/config.js'),

define: {
__SEARCH_LOCALES__: locales,
define: (app) => ({
__SEARCH_HOT_KEYS__: hotKeys,
__SEARCH_LOCALES__: getFullLocaleConfig({
app,
name: PLUGIN_NAME,
default: searchLocaleInfo,
config: locales,
}),
__SEARCH_MAX_SUGGESTIONS__: maxSuggestions,
},
}),

onPrepared: async (app) => {
await prepareSearchIndex({ app, isSearchable, getExtraFields })
Expand Down
1 change: 1 addition & 0 deletions plugins/search/plugin-search/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type * from './hotKey.js'
export type * from './locales.js'
export type * from './searchIndex.js'
6 changes: 6 additions & 0 deletions plugins/search/plugin-search/src/shared/locales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface SearchPluginLocaleData {
/**
* The placeholder of the search box
*/
placeholder: string
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bdb9902

Please sign in to comment.