Skip to content

Commit a7a33b0

Browse files
committed
fix(addon-algolia): new initialize
1 parent 54eb54a commit a7a33b0

File tree

3 files changed

+58
-79
lines changed

3 files changed

+58
-79
lines changed

packages/shims.d.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
// this module's typing is broken.
22

3-
declare module '@docsearch/js' {
4-
function docsearch<T = any>(props: T): void
5-
export default docsearch
6-
}
7-
8-
declare module '@docsearch/react/dist/esm/types' {
9-
export type DocSearchHit = any
10-
}
11-
123
declare module '*.md' {
134
import type { DefineComponent } from 'vue'
145

packages/valaxy-addon-algolia/components/AlgoliaSearchBox.vue

+53-70
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
<script setup lang="ts">
2-
import type { DocSearchHit } from '@docsearch/react/dist/esm/types'
32
import type { AlgoliaSearchOptions } from '../types'
43
import docsearch from '@docsearch/js'
5-
import { onMounted } from 'vue'
4+
import { nextTick, onMounted, watch } from 'vue'
65
import { useI18n } from 'vue-i18n'
7-
import { useRoute, useRouter } from 'vue-router'
6+
import { useRouter } from 'vue-router'
87
import { useAddonAlgoliaConfig } from '../client'
98
import '@docsearch/css'
109
1110
const router = useRouter()
12-
const route = useRoute()
1311
1412
const algolia = useAddonAlgoliaConfig()
1513
1614
const { locale } = useI18n()
1715
16+
type DocSearchProps = Parameters<typeof docsearch>[0]
17+
18+
onMounted(update)
19+
watch(locale, update)
20+
21+
async function update() {
22+
await nextTick()
23+
const options = {
24+
...algolia.value.options!,
25+
...algolia.value.options?.locales?.[locale.value],
26+
}
27+
28+
// now only lang:en
29+
// const rawFacetFilters = options.searchParameters?.facetFilters ?? []
30+
// const facetFilters = [
31+
// ...(Array.isArray(rawFacetFilters)
32+
// ? rawFacetFilters
33+
// : [rawFacetFilters]
34+
// ).filter(f => !f.startsWith('lang:')),
35+
// `lang:${lang.value}`,
36+
// ]
37+
38+
initialize({
39+
...options,
40+
searchParameters: {
41+
...options.searchParameters,
42+
// facetFilters,
43+
},
44+
})
45+
}
46+
1847
onMounted(() => {
1948
const options = {
2049
...algolia.value.options,
@@ -47,79 +76,33 @@ onMounted(() => {
4776
function initialize(userOptions: AlgoliaSearchOptions) {
4877
// note: multi-lang search support is removed since the theme
4978
// doesn't support multiple locales as of now.
50-
const options = Object.assign({}, userOptions, {
51-
container: '#docsearch',
52-
navigator: {
53-
navigate({ itemUrl }: { itemUrl: string }) {
54-
const { pathname: hitPathname } = new URL(
55-
window.location.origin + itemUrl,
56-
)
57-
// router doesn't handle same-page navigation so we use the native
58-
// browser location API for anchor navigation
59-
if (route.path === hitPathname)
60-
window.location.assign(window.location.origin + itemUrl)
61-
62-
else
79+
const options = Object.assign<object, AlgoliaSearchOptions, Partial<DocSearchProps>>(
80+
{},
81+
userOptions,
82+
{
83+
container: '#docsearch',
84+
85+
navigator: {
86+
navigate({ itemUrl }) {
6387
router.push(itemUrl)
88+
},
6489
},
65-
},
66-
transformItems(items: DocSearchHit[]) {
67-
return items.map((item) => {
68-
return Object.assign({}, item, {
69-
url: getRelativePath(item.url),
90+
transformItems(items) {
91+
return items.map((item) => {
92+
return Object.assign({}, item, {
93+
url: getRelativePath(item.url),
94+
})
7095
})
71-
})
72-
},
73-
hitComponent({ hit, children }: { hit: DocSearchHit, children: any }) {
74-
const relativeHit = hit.url.startsWith('http')
75-
? getRelativePath(hit.url as string)
76-
: hit.url
77-
return {
78-
__v: null,
79-
type: 'a',
80-
ref: undefined,
81-
constructor: undefined,
82-
key: undefined,
83-
props: {
84-
href: hit.url,
85-
onClick(event: MouseEvent) {
86-
if (isSpecialClick(event))
87-
return
88-
89-
// we rely on the native link scrolling when user is already on
90-
// the right anchor because Router doesn't support duplicated
91-
// history entries.
92-
if (route.path === relativeHit)
93-
return
94-
95-
// if the hits goes to another page, we prevent the native link
96-
// behavior to leverage the Router loading feature.
97-
if (route.path !== relativeHit)
98-
event.preventDefault()
99-
100-
router.push(relativeHit)
101-
},
102-
children,
103-
},
104-
}
96+
},
10597
},
106-
})
107-
docsearch(options)
108-
}
98+
) as DocSearchProps
10999
110-
function isSpecialClick(event: MouseEvent) {
111-
return (
112-
event.button === 1
113-
|| event.altKey
114-
|| event.ctrlKey
115-
|| event.metaKey
116-
|| event.shiftKey
117-
)
100+
docsearch(options)
118101
}
119102
120-
function getRelativePath(absoluteUrl: string) {
121-
const { pathname, hash } = new URL(absoluteUrl)
122-
return pathname + hash
103+
function getRelativePath(url: string) {
104+
const { pathname, hash } = new URL(url, location.origin)
105+
return pathname.replace(/\.html$/, '') + hash
123106
}
124107
</script>
125108

packages/valaxy-addon-algolia/types/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Algolia search options. Partially copied from
3+
* `@docsearch/react/dist/esm/DocSearch.d.ts`
4+
*/
15
export interface AlgoliaSearchOptions extends DocSearchProps {
26
locales?: Record<string, Partial<DocSearchProps>>
37
}
@@ -10,6 +14,7 @@ export interface DocSearchProps {
1014
searchParameters?: SearchOptions
1115
disableUserPersonalization?: boolean
1216
initialQuery?: string
17+
insights?: boolean
1318
translations?: DocSearchTranslations
1419
}
1520

0 commit comments

Comments
 (0)