valaxy 0.20.2 → 0.20.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,7 @@ export * from './outline'
19
19
  // utils
20
20
  export * from './back'
21
21
  export * from './decrypt'
22
+ export * from './search'
22
23
 
23
24
  // app
24
25
  export * from './app'
@@ -0,0 +1,57 @@
1
+ import { computed, onMounted, shallowRef } from 'vue'
2
+ import { useSiteConfig } from 'valaxy'
3
+ import type { MaybeRefOrGetter } from '@vueuse/shared'
4
+ import type { UseFuseOptions } from '@vueuse/integrations/useFuse'
5
+ import { useFuse } from '@vueuse/integrations/useFuse'
6
+ import type { FuseListItem } from 'valaxy/types'
7
+
8
+ export function useFuseSearch<T extends FuseListItem = FuseListItem>(
9
+ search: MaybeRefOrGetter<string>,
10
+ options?: MaybeRefOrGetter<UseFuseOptions<T>>,
11
+ ) {
12
+ const siteConfig = useSiteConfig()
13
+
14
+ const fuseListData = shallowRef<T[]>([])
15
+
16
+ const keys = computed(() => {
17
+ const ks = siteConfig.value.fuse.options.keys || []
18
+ return ks.length === 0 ? ['title', 'tags', 'categories', 'excerpt'] : ks
19
+ })
20
+
21
+ const defaultOptions: UseFuseOptions<T> = {
22
+ fuseOptions: {
23
+ includeMatches: true,
24
+ findAllMatches: true,
25
+ // threshold: 0.99,
26
+ // ignoreLocation: true,
27
+
28
+ ...siteConfig.value.fuse.options,
29
+ keys: keys.value,
30
+ },
31
+ // resultLimit: resultLimit.value,
32
+ // matchAllWhenSearchEmpty: matchAllWhenSearchEmpty.value,
33
+ }
34
+ const useFuseOptions = computed<UseFuseOptions<T>>(() => ({
35
+ ...defaultOptions,
36
+ ...options,
37
+ }))
38
+
39
+ const ruse = useFuse<T>(search, fuseListData, useFuseOptions)
40
+
41
+ async function fetchFuseListData(path?: string) {
42
+ const fuseListDataPath = path
43
+ || (siteConfig.value.fuse.dataPath.startsWith('http')
44
+ ? siteConfig.value.fuse.dataPath
45
+ : `${import.meta.env.BASE_URL}${siteConfig.value.fuse.dataPath}`)
46
+
47
+ const res = await fetch(fuseListDataPath)
48
+ const data = await res.json()
49
+
50
+ if (Array.isArray(data))
51
+ fuseListData.value = data
52
+ }
53
+
54
+ onMounted(fetchFuseListData)
55
+
56
+ return ruse
57
+ }
@@ -0,0 +1 @@
1
+ export * from './fuse'