Skip to content

Commit ce1cf18

Browse files
committed
feat(plugin-docsearch): allow more fields in locales config
1 parent 93f945c commit ce1cf18

File tree

3 files changed

+83
-76
lines changed

3 files changed

+83
-76
lines changed

packages/@vuepress/plugin-docsearch/src/clientAppEnhance.ts

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,93 @@
1+
import { createElement } from 'preact'
12
import { computed, h } from 'vue'
2-
import { useRouteLocale } from '@vuepress/client'
3+
import { useRouter } from 'vue-router'
4+
import { usePageLang, useRouteLocale, useSiteData } from '@vuepress/client'
35
import type { ClientAppEnhance } from '@vuepress/client'
6+
import { resolveRoutePathFromUrl } from '@vuepress/shared'
47
import type { LocaleConfig } from '@vuepress/shared'
58
import { Docsearch } from './components/Docsearch'
69
import type { DocsearchProps } from './components/Docsearch'
710

811
declare const DOCSEARCH_PROPS: DocsearchProps
9-
declare const DOCSEARCH_LOCALES: LocaleConfig<
10-
Pick<DocsearchProps, 'placeholder'>
11-
>
12+
declare const DOCSEARCH_LOCALES: LocaleConfig<DocsearchProps>
1213

1314
const props = DOCSEARCH_PROPS
1415
const locales = DOCSEARCH_LOCALES
1516

17+
const isSpecialClick = (event: MouseEvent): boolean => {
18+
return (
19+
event.button === 1 ||
20+
event.altKey ||
21+
event.ctrlKey ||
22+
event.metaKey ||
23+
event.shiftKey
24+
)
25+
}
26+
1627
const clientAppEnhance: ClientAppEnhance = ({ app }) => {
1728
// wrap the `<Docsearch />` component with plugin options
1829
// eslint-disable-next-line vue/match-component-file-name
1930
app.component('Docsearch', {
2031
setup() {
32+
const router = useRouter()
2133
const routeLocale = useRouteLocale()
34+
const lang = usePageLang()
35+
const site = useSiteData()
36+
37+
const propsLocale = computed(() => ({
38+
...props,
39+
...locales[routeLocale.value],
40+
}))
41+
42+
// resolve options for docsearch
43+
const options = computed(() => ({
44+
...propsLocale.value,
2245

23-
const locale = computed(() => locales[routeLocale.value])
46+
searchParameters: {
47+
...propsLocale.value.searchParameters,
48+
facetFilters: [`lang:${lang.value}`].concat(
49+
propsLocale.value.searchParameters?.facetFilters || []
50+
),
51+
},
2452

25-
return () =>
26-
h(Docsearch, {
27-
options: {
28-
...props,
29-
...locale.value,
30-
// TODO: add language filter to support multiple locales
53+
// navigation behavior triggered by `onKeyDown` internally
54+
navigator: {
55+
// when pressing Enter without metaKey
56+
navigate: ({ itemUrl }) => {
57+
router.push(itemUrl)
3158
},
32-
})
59+
},
60+
61+
// transform full url to route path
62+
transformItems: (items) =>
63+
items.map((item) => {
64+
// the `item.url` is full url with protocol and hostname
65+
// so we have to transform it to vue-router path
66+
return {
67+
...item,
68+
url: resolveRoutePathFromUrl(item.url, site.value.base),
69+
}
70+
}),
71+
72+
// handle `onClick` by `router.push`
73+
hitComponent: ({ hit, children }) =>
74+
createElement(
75+
'a',
76+
{
77+
href: hit.url,
78+
onClick: (event: MouseEvent) => {
79+
if (isSpecialClick(event)) {
80+
return
81+
}
82+
event.preventDefault()
83+
router.push(hit.url)
84+
},
85+
},
86+
children
87+
),
88+
}))
89+
90+
return () => h(Docsearch, { options: options.value })
3391
},
3492
})
3593
}

packages/@vuepress/plugin-docsearch/src/components/Docsearch.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { createElement } from 'preact'
21
import { defineComponent, getCurrentInstance, h, onMounted, watch } from 'vue'
32
import type { PropType } from 'vue'
4-
import { useRouter } from 'vue-router'
53
import type { DocSearchProps } from '@docsearch/react'
6-
import { useSiteData } from '@vuepress/client'
7-
import { resolveRoutePathFromUrl } from '@vuepress/shared'
84

95
export type DocsearchProps = DocSearchProps
106

@@ -24,16 +20,6 @@ const loadDocsearch = async (): Promise<DocsearchFunc> => {
2420
return docsearch.default
2521
}
2622

27-
const isSpecialClick = (event: MouseEvent): boolean => {
28-
return (
29-
event.button === 1 ||
30-
event.altKey ||
31-
event.ctrlKey ||
32-
event.metaKey ||
33-
event.shiftKey
34-
)
35-
}
36-
3723
export const Docsearch = defineComponent({
3824
name: 'Docsearch',
3925

@@ -45,53 +31,14 @@ export const Docsearch = defineComponent({
4531
},
4632

4733
setup(props) {
48-
const router = useRouter()
49-
const site = useSiteData()
5034
const vm = getCurrentInstance()
5135

5236
const initialize = (options: DocsearchProps): void => {
5337
loadDocsearch().then((docsearch) => {
5438
docsearch({
5539
...options,
56-
5740
// the container selector
5841
container: '#docsearch',
59-
60-
// navigation behavior triggered by `onKeyDown` internally
61-
navigator: {
62-
// when pressing Enter without metaKey
63-
navigate: ({ itemUrl }) => {
64-
router.push(itemUrl)
65-
},
66-
},
67-
68-
// transform full url to route path
69-
transformItems: (items) =>
70-
items.map((item) => {
71-
// the `item.url` is full url with protocol and hostname
72-
// so we have to transform it to vue-router path
73-
return {
74-
...item,
75-
url: resolveRoutePathFromUrl(item.url, site.value.base),
76-
}
77-
}),
78-
79-
// handle `onClick` by `router.push`
80-
hitComponent: ({ hit, children }) =>
81-
createElement(
82-
'a',
83-
{
84-
href: hit.url,
85-
onClick: (event: MouseEvent) => {
86-
if (isSpecialClick(event)) {
87-
return
88-
}
89-
event.preventDefault()
90-
router.push(hit.url)
91-
},
92-
},
93-
children
94-
),
9542
})
9643
})
9744
}

packages/@vuepress/plugin-docsearch/src/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import type { Plugin, PluginObject } from '@vuepress/core'
33
import type { LocaleConfig } from '@vuepress/shared'
44
import { logger, path } from '@vuepress/utils'
55

6-
export interface DocsearchPluginOptions
7-
extends Pick<
8-
DocSearchProps,
9-
| 'appId'
10-
| 'apiKey'
11-
| 'indexName'
12-
| 'searchParameters'
13-
| 'disableUserPersonalization'
14-
| 'initialQuery'
15-
> {
16-
locales?: LocaleConfig<Pick<DocSearchProps, 'placeholder'>>
6+
export type DocsearchPluginLocaleData = Pick<
7+
DocSearchProps,
8+
| 'appId'
9+
| 'apiKey'
10+
| 'indexName'
11+
| 'placeholder'
12+
| 'searchParameters'
13+
| 'disableUserPersonalization'
14+
| 'initialQuery'
15+
>
16+
17+
export interface DocsearchPluginOptions extends DocsearchPluginLocaleData {
18+
locales?: LocaleConfig<DocsearchPluginLocaleData>
1719
}
1820

1921
export const docsearchPlugin: Plugin<DocsearchPluginOptions> = ({

0 commit comments

Comments
 (0)