|
|
|
@ -2,23 +2,36 @@
|
|
|
|
|
<div class="algolia-search-box" id="docsearch" />
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
// TODO: @vue/compiler-sfc currently has a bug that removes `import 'foo'`
|
|
|
|
|
// statements in <script setup> so we put it here for now
|
|
|
|
|
import '@docsearch/css'
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import type { AlgoliaSearchOptions } from 'algoliasearch'
|
|
|
|
|
import { useRoute, useRouter } from 'vitepress'
|
|
|
|
|
import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
|
|
|
|
|
import docsearch from '@docsearch/js'
|
|
|
|
|
import type { DefaultTheme } from '../config'
|
|
|
|
|
import type { DocSearchHit } from '@docsearch/react/dist/esm/types'
|
|
|
|
|
|
|
|
|
|
const { options } = defineProps<{
|
|
|
|
|
options: AlgoliaSearchOptions
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
options: DefaultTheme.AlgoliaSearchOptions
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
const vm = getCurrentInstance()
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
|
|
watch(() => options, (value) => { update(value) })
|
|
|
|
|
watch(
|
|
|
|
|
() => props.options,
|
|
|
|
|
(value) => {
|
|
|
|
|
update(value)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
initialize(options)
|
|
|
|
|
initialize(props.options)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function isSpecialClick(event: MouseEvent) {
|
|
|
|
@ -39,85 +52,87 @@ function getRelativePath(absoluteUrl: string) {
|
|
|
|
|
|
|
|
|
|
function update(options: any) {
|
|
|
|
|
if (vm && vm.vnode.el) {
|
|
|
|
|
vm.vnode.el.innerHTML = '<div class="algolia-search-box" id="docsearch"></div>'
|
|
|
|
|
vm.vnode.el.innerHTML =
|
|
|
|
|
'<div class="algolia-search-box" id="docsearch"></div>'
|
|
|
|
|
initialize(options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initialize(userOptions: any) {
|
|
|
|
|
Promise.all([
|
|
|
|
|
import('@docsearch/js'),
|
|
|
|
|
import('@docsearch/css')
|
|
|
|
|
]).then(([docsearch]) => {
|
|
|
|
|
docsearch.default(
|
|
|
|
|
Object.assign({}, userOptions, {
|
|
|
|
|
container: '#docsearch',
|
|
|
|
|
|
|
|
|
|
searchParameters: Object.assign({}, userOptions.searchParameters),
|
|
|
|
|
|
|
|
|
|
navigator: {
|
|
|
|
|
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
|
|
|
|
|
const { pathname: hitPathname } = new URL(
|
|
|
|
|
window.location.origin + suggestionUrl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Router doesn't handle same-page navigation so we use the native
|
|
|
|
|
// browser location API for anchor navigation
|
|
|
|
|
if (route.path === hitPathname) {
|
|
|
|
|
window.location.assign(window.location.origin + suggestionUrl)
|
|
|
|
|
} else {
|
|
|
|
|
router.go(suggestionUrl)
|
|
|
|
|
}
|
|
|
|
|
docsearch(
|
|
|
|
|
Object.assign({}, userOptions, {
|
|
|
|
|
container: '#docsearch',
|
|
|
|
|
|
|
|
|
|
searchParameters: Object.assign({}, userOptions.searchParameters),
|
|
|
|
|
|
|
|
|
|
navigator: {
|
|
|
|
|
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
|
|
|
|
|
const { pathname: hitPathname } = new URL(
|
|
|
|
|
window.location.origin + suggestionUrl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Router doesn't handle same-page navigation so we use the native
|
|
|
|
|
// browser location API for anchor navigation
|
|
|
|
|
if (route.path === hitPathname) {
|
|
|
|
|
window.location.assign(window.location.origin + suggestionUrl)
|
|
|
|
|
} else {
|
|
|
|
|
router.go(suggestionUrl)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
transformItems: (items) => {
|
|
|
|
|
return items.map((item) => {
|
|
|
|
|
return Object.assign({}, item, {
|
|
|
|
|
url: getRelativePath(item.url)
|
|
|
|
|
})
|
|
|
|
|
transformItems: (items: DocSearchHit[]) => {
|
|
|
|
|
return items.map((item) => {
|
|
|
|
|
return Object.assign({}, item, {
|
|
|
|
|
url: getRelativePath(item.url)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hitComponent: ({ hit, children }) => {
|
|
|
|
|
const relativeHit = hit.url.startsWith('http')
|
|
|
|
|
? getRelativePath(hit.url as string)
|
|
|
|
|
: hit.url
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'a',
|
|
|
|
|
ref: undefined,
|
|
|
|
|
constructor: undefined,
|
|
|
|
|
key: undefined,
|
|
|
|
|
props: {
|
|
|
|
|
href: hit.url,
|
|
|
|
|
onClick: (event: MouseEvent) => {
|
|
|
|
|
if (isSpecialClick(event)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we rely on the native link scrolling when user is already on
|
|
|
|
|
// the right anchor because Router doesn't support duplicated
|
|
|
|
|
// history entries
|
|
|
|
|
if (route.path === relativeHit) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the hits goes to another page, we prevent the native link
|
|
|
|
|
// behavior to leverage the Router loading feature
|
|
|
|
|
if (route.path !== relativeHit) {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.go(relativeHit)
|
|
|
|
|
},
|
|
|
|
|
children
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hitComponent: ({
|
|
|
|
|
hit,
|
|
|
|
|
children
|
|
|
|
|
}: {
|
|
|
|
|
hit: DocSearchHit
|
|
|
|
|
children: any
|
|
|
|
|
}) => {
|
|
|
|
|
const relativeHit = hit.url.startsWith('http')
|
|
|
|
|
? getRelativePath(hit.url as string)
|
|
|
|
|
: hit.url
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'a',
|
|
|
|
|
ref: undefined,
|
|
|
|
|
constructor: undefined,
|
|
|
|
|
key: undefined,
|
|
|
|
|
props: {
|
|
|
|
|
href: hit.url,
|
|
|
|
|
onClick: (event: MouseEvent) => {
|
|
|
|
|
if (isSpecialClick(event)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we rely on the native link scrolling when user is already on
|
|
|
|
|
// the right anchor because Router doesn't support duplicated
|
|
|
|
|
// history entries
|
|
|
|
|
if (route.path === relativeHit) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the hits goes to another page, we prevent the native link
|
|
|
|
|
// behavior to leverage the Router loading feature
|
|
|
|
|
if (route.path !== relativeHit) {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.go(relativeHit)
|
|
|
|
|
},
|
|
|
|
|
children
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
@ -139,7 +154,7 @@ function initialize(userOptions: any) {
|
|
|
|
|
|
|
|
|
|
.algolia-search-box .DocSearch-Button-Placeholder {
|
|
|
|
|
padding-left: 8px;
|
|
|
|
|
font-size: .9rem;
|
|
|
|
|
font-size: 0.9rem;
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|