Skip to content

Commit b6ded16

Browse files
committed
fix(plugin-docsearch): handle navigation url correctly (close #1024)
1 parent 9cbfebb commit b6ded16

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { DocSearchProps } from '@docsearch/react'
2+
import type { InternalDocSearchHit } from '@docsearch/react/dist/esm/types/index.js'
23
import { useSiteData } from '@vuepress/client'
34
import { resolveRoutePathFromUrl } from '@vuepress/shared'
45
import { debounce } from 'ts-debounce'
56
import { useRouter } from 'vue-router'
67

8+
interface TransformedDocSearchHit extends InternalDocSearchHit {
9+
routePath: string
10+
}
11+
712
const isSpecialClick = (event: MouseEvent): boolean =>
813
event.button === 1 ||
914
event.altKey ||
@@ -19,13 +24,18 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
1924
const site = useSiteData()
2025

2126
return {
22-
// render the hit component with custom `onClick` handler
23-
hitComponent: ({ hit, children }) => {
24-
// the `hit.url` is full url with protocol and hostname
25-
// so we have to transform it to vue-router path
26-
const routePath = resolveRoutePathFromUrl(hit.url, site.value.base)
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+
})),
2735

28-
return {
36+
// render the hit component with custom `onClick` handler
37+
hitComponent: ({ hit, children }) =>
38+
({
2939
type: 'a',
3040
ref: undefined,
3141
constructor: undefined,
@@ -38,19 +48,18 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
3848
return
3949
}
4050
event.preventDefault()
41-
router.push(routePath)
51+
router.push((hit as TransformedDocSearchHit).routePath)
4252
},
4353
children,
4454
},
4555
__v: null,
46-
} as unknown
47-
},
56+
} as unknown),
4857

4958
// navigation behavior triggered by `onKeyDown` internally
5059
navigator: {
5160
// when pressing Enter without metaKey
52-
navigate: ({ itemUrl }) => {
53-
router.push(itemUrl)
61+
navigate: ({ item }) => {
62+
router.push((item as TransformedDocSearchHit).routePath)
5463
},
5564
},
5665

0 commit comments

Comments
 (0)