|
1 | 1 | <script lang="ts"> |
2 | 2 | import { AlgoliaSearchOptions } from 'algoliasearch' |
3 | | -// import docsearch from '@docsearch/js' |
4 | | -// import '@docsearch/css' |
| 3 | +import docsearch from '@docsearch/js' |
| 4 | +import '@docsearch/css' |
5 | 5 | import { useRoute, useRouter } from 'vitepress' |
6 | 6 | import { getCurrentInstance, onMounted, PropType, watch } from 'vue' |
7 | 7 |
|
@@ -53,89 +53,79 @@ export default { |
53 | 53 | } |
54 | 54 |
|
55 | 55 | function initialize(userOptions: any) { |
56 | | - Promise.all([ |
57 | | - import('@docsearch/js'), |
58 | | - import('@docsearch/css'), |
59 | | - // import(/* webpackChunkName: "docsearch" */ '@docsearch/js'), |
60 | | - // Promise.resolve(docsearch), |
61 | | - // import(/* webpackChunkName: "docsearch" */ '@docsearch/css'), |
62 | | - ]).then(([docsearch]) => { |
63 | | - docsearch = docsearch.default |
64 | | -
|
65 | | - docsearch( |
66 | | - Object.assign({}, userOptions, { |
67 | | - container: '#docsearch', |
68 | | - // #697 Make DocSearch work well in i18n mode. |
69 | | - searchParameters: Object.assign( |
70 | | - {}, |
71 | | - // lang && { |
72 | | - // facetFilters: [`lang:${lang}`].concat( |
73 | | - // userOptions.facetFilters || [] |
74 | | - // ) |
75 | | - // }, |
76 | | - userOptions.searchParameters |
77 | | - ), |
78 | | - navigator: { |
79 | | - navigate: ({ suggestionUrl }: { suggestionUrl: string }) => { |
80 | | - const { pathname: hitPathname } = new URL( |
81 | | - window.location.origin + suggestionUrl |
82 | | - ) |
83 | | -
|
84 | | - // Vue Router doesn't handle same-page navigation so we use |
85 | | - // the native browser location API for anchor navigation. |
86 | | - if (route.path === hitPathname) { |
87 | | - window.location.assign(window.location.origin + suggestionUrl) |
88 | | - } else { |
89 | | - router.go(suggestionUrl) |
90 | | - } |
91 | | - }, |
| 56 | + docsearch( |
| 57 | + Object.assign({}, userOptions, { |
| 58 | + container: '#docsearch', |
| 59 | + // #697 Make DocSearch work well in i18n mode. |
| 60 | + searchParameters: Object.assign( |
| 61 | + {}, |
| 62 | + // lang && { |
| 63 | + // facetFilters: [`lang:${lang}`].concat( |
| 64 | + // userOptions.facetFilters || [] |
| 65 | + // ) |
| 66 | + // }, |
| 67 | + userOptions.searchParameters |
| 68 | + ), |
| 69 | + navigator: { |
| 70 | + navigate: ({ suggestionUrl }: { suggestionUrl: string }) => { |
| 71 | + const { pathname: hitPathname } = new URL( |
| 72 | + window.location.origin + suggestionUrl |
| 73 | + ) |
| 74 | +
|
| 75 | + // Vue Router doesn't handle same-page navigation so we use |
| 76 | + // the native browser location API for anchor navigation. |
| 77 | + if (route.path === hitPathname) { |
| 78 | + window.location.assign(window.location.origin + suggestionUrl) |
| 79 | + } else { |
| 80 | + router.go(suggestionUrl) |
| 81 | + } |
92 | 82 | }, |
93 | | - transformItems: items => { |
94 | | - return items.map(item => { |
95 | | - return Object.assign({}, item, { |
96 | | - url: getRelativePath(item.url), |
97 | | - }) |
| 83 | + }, |
| 84 | + transformItems: items => { |
| 85 | + return items.map(item => { |
| 86 | + return Object.assign({}, item, { |
| 87 | + url: getRelativePath(item.url), |
98 | 88 | }) |
99 | | - }, |
100 | | - hitComponent: ({ hit, children }) => { |
101 | | - const relativeHit = hit.url.startsWith('http') |
102 | | - ? getRelativePath(hit.url as string) |
103 | | - : hit.url |
104 | | -
|
105 | | - return { |
106 | | - type: 'a', |
107 | | - ref: undefined, |
108 | | - constructor: undefined, |
109 | | - key: undefined, |
110 | | - props: { |
111 | | - href: hit.url, |
112 | | - onClick: (event: MouseEvent) => { |
113 | | - if (isSpecialClick(event)) { |
114 | | - return |
115 | | - } |
116 | | -
|
117 | | - // We rely on the native link scrolling when user is |
118 | | - // already on the right anchor because Vue Router doesn't |
119 | | - // support duplicated history entries. |
120 | | - if (route.path === relativeHit) { |
121 | | - return |
122 | | - } |
123 | | -
|
124 | | - // If the hits goes to another page, we prevent the native link behavior |
125 | | - // to leverage the Vue Router loading feature. |
126 | | - if (route.path !== relativeHit) { |
127 | | - event.preventDefault() |
128 | | - } |
129 | | -
|
130 | | - router.go(relativeHit) |
131 | | - }, |
132 | | - children, |
| 89 | + }) |
| 90 | + }, |
| 91 | + hitComponent: ({ hit, children }) => { |
| 92 | + const relativeHit = hit.url.startsWith('http') |
| 93 | + ? getRelativePath(hit.url as string) |
| 94 | + : hit.url |
| 95 | +
|
| 96 | + return { |
| 97 | + type: 'a', |
| 98 | + ref: undefined, |
| 99 | + constructor: undefined, |
| 100 | + key: undefined, |
| 101 | + props: { |
| 102 | + href: hit.url, |
| 103 | + onClick: (event: MouseEvent) => { |
| 104 | + if (isSpecialClick(event)) { |
| 105 | + return |
| 106 | + } |
| 107 | +
|
| 108 | + // We rely on the native link scrolling when user is |
| 109 | + // already on the right anchor because Vue Router doesn't |
| 110 | + // support duplicated history entries. |
| 111 | + if (route.path === relativeHit) { |
| 112 | + return |
| 113 | + } |
| 114 | +
|
| 115 | + // If the hits goes to another page, we prevent the native link behavior |
| 116 | + // to leverage the Vue Router loading feature. |
| 117 | + if (route.path !== relativeHit) { |
| 118 | + event.preventDefault() |
| 119 | + } |
| 120 | +
|
| 121 | + router.go(relativeHit) |
133 | 122 | }, |
134 | | - } |
135 | | - }, |
136 | | - }) |
137 | | - ) |
138 | | - }) |
| 123 | + children, |
| 124 | + }, |
| 125 | + } |
| 126 | + }, |
| 127 | + }) |
| 128 | + ) |
139 | 129 | } |
140 | 130 |
|
141 | 131 | onMounted(() => { |
|
0 commit comments