Skip to content

Commit bf880cd

Browse files
committed
refactor(plugin-docsearch): reduce route path resolving
1 parent 8d7e700 commit bf880cd

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

ecosystem/plugin-docsearch/src/client/composables/useDocsearchShim.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import type { DocSearchProps } from '@docsearch/react'
2-
import type { InternalDocSearchHit } from '@docsearch/react/dist/esm/types/index.js'
32
import { useSiteData } from '@vuepress/client'
43
import { resolveRoutePathFromUrl } from '@vuepress/shared'
54
import { debounce } from 'ts-debounce'
65
import { useRouter } from 'vue-router'
76

8-
interface TransformedDocSearchHit extends InternalDocSearchHit {
9-
routePath: string
10-
}
11-
127
const isSpecialClick = (event: MouseEvent): boolean =>
138
event.button === 1 ||
149
event.altKey ||
@@ -24,15 +19,6 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
2419
const site = useSiteData()
2520

2621
return {
27-
// transform full url to route path
28-
transformItems: (items) =>
29-
items.map((item) => ({
30-
...item,
31-
// the `item.url` is full url with protocol and hostname
32-
// so we have to transform it to vue-router path
33-
routePath: resolveRoutePathFromUrl(item.url, site.value.base),
34-
})),
35-
3622
// render the hit component with custom `onClick` handler
3723
hitComponent: ({ hit, children }) =>
3824
({
@@ -48,7 +34,7 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
4834
return
4935
}
5036
event.preventDefault()
51-
router.push((hit as TransformedDocSearchHit).routePath)
37+
router.push(resolveRoutePathFromUrl(hit.url, site.value.base))
5238
},
5339
children,
5440
},
@@ -58,8 +44,8 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
5844
// navigation behavior triggered by `onKeyDown` internally
5945
navigator: {
6046
// when pressing Enter without metaKey
61-
navigate: ({ item }) => {
62-
router.push((item as TransformedDocSearchHit).routePath)
47+
navigate: ({ itemUrl }) => {
48+
router.push(resolveRoutePathFromUrl(itemUrl, site.value.base))
6349
},
6450
},
6551

0 commit comments

Comments
 (0)