1
1
import type { DocSearchProps } from '@docsearch/react'
2
+ import type { InternalDocSearchHit } from '@docsearch/react/dist/esm/types/index.js'
2
3
import { useSiteData } from '@vuepress/client'
3
4
import { resolveRoutePathFromUrl } from '@vuepress/shared'
4
5
import { debounce } from 'ts-debounce'
5
6
import { useRouter } from 'vue-router'
6
7
8
+ interface TransformedDocSearchHit extends InternalDocSearchHit {
9
+ routePath : string
10
+ }
11
+
7
12
const isSpecialClick = ( event : MouseEvent ) : boolean =>
8
13
event . button === 1 ||
9
14
event . altKey ||
@@ -19,13 +24,18 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
19
24
const site = useSiteData ( )
20
25
21
26
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
+ } ) ) ,
27
35
28
- return {
36
+ // render the hit component with custom `onClick` handler
37
+ hitComponent : ( { hit, children } ) =>
38
+ ( {
29
39
type : 'a' ,
30
40
ref : undefined ,
31
41
constructor : undefined ,
@@ -38,19 +48,18 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
38
48
return
39
49
}
40
50
event . preventDefault ( )
41
- router . push ( routePath )
51
+ router . push ( ( hit as TransformedDocSearchHit ) . routePath )
42
52
} ,
43
53
children,
44
54
} ,
45
55
__v : null ,
46
- } as unknown
47
- } ,
56
+ } as unknown ) ,
48
57
49
58
// navigation behavior triggered by `onKeyDown` internally
50
59
navigator : {
51
60
// when pressing Enter without metaKey
52
- navigate : ( { itemUrl } ) => {
53
- router . push ( itemUrl )
61
+ navigate : ( { item } ) => {
62
+ router . push ( ( item as TransformedDocSearchHit ) . routePath )
54
63
} ,
55
64
} ,
56
65
0 commit comments