@kawapress/plugin-search 0.0.1-beta.1
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.
- package/LICENSE +21 -0
- package/README.md +145 -0
- package/README.zh-CN.md +145 -0
- package/package.json +64 -0
- package/src/client.ts +3 -0
- package/src/components/Search.vue +103 -0
- package/src/components/SearchOverlay.vue +209 -0
- package/src/components/SearchResult.vue +61 -0
- package/src/composables/useLocalSearch.ts +66 -0
- package/src/index.ts +4 -0
- package/src/platform-detection.js +8 -0
- package/src/plugin.ts +32 -0
- package/src/runtime-plugin.ts +12 -0
- package/src/search-highlight.ts +53 -0
- package/src/search-index-plugin.ts +484 -0
- package/src/search.css +476 -0
- package/src/search.ts +47 -0
- package/src/translations.ts +48 -0
- package/src/virtual-modules.d.ts +6 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { SearchTranslations } from '../translations'
|
|
3
|
+
import { useRouter } from 'kawapress/client'
|
|
4
|
+
import {
|
|
5
|
+
computed,
|
|
6
|
+
nextTick,
|
|
7
|
+
onBeforeUnmount,
|
|
8
|
+
onMounted,
|
|
9
|
+
shallowRef,
|
|
10
|
+
useTemplateRef,
|
|
11
|
+
watch,
|
|
12
|
+
} from 'vue'
|
|
13
|
+
import { useLocalSearch } from '../composables/useLocalSearch'
|
|
14
|
+
import SearchResult from './SearchResult.vue'
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
translations: SearchTranslations
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Emits {
|
|
21
|
+
close: []
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const props = defineProps<Props>()
|
|
25
|
+
const emit = defineEmits<Emits>()
|
|
26
|
+
const router = useRouter()
|
|
27
|
+
const { load, query, results, status } = useLocalSearch()
|
|
28
|
+
const dialog = useTemplateRef<HTMLDialogElement>('dialog')
|
|
29
|
+
const input = useTemplateRef<HTMLInputElement>('input')
|
|
30
|
+
const resultsScroll = useTemplateRef<HTMLElement>('resultsScroll')
|
|
31
|
+
const selectedIndex = shallowRef(-1)
|
|
32
|
+
const hasQuery = computed(() => query.value.trim().length > 0)
|
|
33
|
+
const activeDescendant = computed(() => (
|
|
34
|
+
selectedIndex.value >= 0 ? `kawa-search-result-${selectedIndex.value}` : undefined
|
|
35
|
+
))
|
|
36
|
+
|
|
37
|
+
watch(results, (items) => {
|
|
38
|
+
selectedIndex.value = items.length > 0 ? 0 : -1
|
|
39
|
+
scrollToSelectedResult()
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
onMounted(async () => {
|
|
43
|
+
if (!dialog.value?.open) {
|
|
44
|
+
dialog.value?.showModal()
|
|
45
|
+
}
|
|
46
|
+
await nextTick()
|
|
47
|
+
input.value?.focus()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
onBeforeUnmount(() => {
|
|
51
|
+
if (dialog.value?.open) {
|
|
52
|
+
dialog.value.close()
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
function close(): void {
|
|
57
|
+
if (dialog.value?.open) {
|
|
58
|
+
dialog.value.close()
|
|
59
|
+
}
|
|
60
|
+
emit('close')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function handleDialogClick(event: MouseEvent): void {
|
|
64
|
+
if (event.target === dialog.value) {
|
|
65
|
+
close()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function handleKeydown(event: KeyboardEvent): void {
|
|
70
|
+
if (event.key === 'Escape') {
|
|
71
|
+
event.preventDefault()
|
|
72
|
+
close()
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
if (event.key === 'ArrowDown') {
|
|
76
|
+
event.preventDefault()
|
|
77
|
+
selectResult(1)
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
if (event.key === 'ArrowUp') {
|
|
81
|
+
event.preventDefault()
|
|
82
|
+
selectResult(-1)
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
if (event.key === 'Enter'
|
|
86
|
+
&& !(event.target instanceof HTMLAnchorElement)
|
|
87
|
+
&& selectedIndex.value >= 0) {
|
|
88
|
+
event.preventDefault()
|
|
89
|
+
const result = results.value[selectedIndex.value]
|
|
90
|
+
if (result) {
|
|
91
|
+
router.push(result.id)
|
|
92
|
+
close()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function selectResult(offset: number): void {
|
|
98
|
+
if (results.value.length === 0) {
|
|
99
|
+
selectedIndex.value = -1
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
selectedIndex.value = (
|
|
103
|
+
selectedIndex.value + offset + results.value.length
|
|
104
|
+
) % results.value.length
|
|
105
|
+
scrollToSelectedResult()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function scrollToSelectedResult(): Promise<void> {
|
|
109
|
+
await nextTick()
|
|
110
|
+
resultsScroll.value
|
|
111
|
+
?.querySelector(`[data-search-index="${selectedIndex.value}"]`)
|
|
112
|
+
?.scrollIntoView({ block: 'nearest' })
|
|
113
|
+
}
|
|
114
|
+
</script>
|
|
115
|
+
|
|
116
|
+
<template>
|
|
117
|
+
<dialog
|
|
118
|
+
ref="dialog"
|
|
119
|
+
class="kawa-search"
|
|
120
|
+
:aria-label="props.translations.buttonLabel"
|
|
121
|
+
@cancel.prevent="close"
|
|
122
|
+
@click="handleDialogClick"
|
|
123
|
+
@keydown="handleKeydown"
|
|
124
|
+
>
|
|
125
|
+
<div class="kawa-search__panel">
|
|
126
|
+
<form class="kawa-search__form" role="search" @submit.prevent>
|
|
127
|
+
<svg class="kawa-search__icon" viewBox="0 0 24 24" aria-hidden="true">
|
|
128
|
+
<circle cx="11" cy="11" r="6.5" />
|
|
129
|
+
<path d="m16 16 4 4" />
|
|
130
|
+
</svg>
|
|
131
|
+
<label class="kawa-search__label" for="kawa-search-input">
|
|
132
|
+
{{ props.translations.placeholder }}
|
|
133
|
+
</label>
|
|
134
|
+
<input
|
|
135
|
+
id="kawa-search-input"
|
|
136
|
+
ref="input"
|
|
137
|
+
v-model="query"
|
|
138
|
+
class="kawa-search__input"
|
|
139
|
+
type="search"
|
|
140
|
+
role="combobox"
|
|
141
|
+
:placeholder="props.translations.placeholder"
|
|
142
|
+
:aria-activedescendant="activeDescendant"
|
|
143
|
+
:aria-controls="results.length ? 'kawa-search-results' : undefined"
|
|
144
|
+
:aria-expanded="results.length > 0"
|
|
145
|
+
aria-autocomplete="list"
|
|
146
|
+
autocomplete="off"
|
|
147
|
+
autocapitalize="off"
|
|
148
|
+
autocorrect="off"
|
|
149
|
+
spellcheck="false"
|
|
150
|
+
>
|
|
151
|
+
<button
|
|
152
|
+
type="button"
|
|
153
|
+
class="kawa-search__close"
|
|
154
|
+
:aria-label="props.translations.closeLabel"
|
|
155
|
+
@click="close"
|
|
156
|
+
>
|
|
157
|
+
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
158
|
+
<path d="m7 7 10 10M17 7 7 17" />
|
|
159
|
+
</svg>
|
|
160
|
+
</button>
|
|
161
|
+
</form>
|
|
162
|
+
|
|
163
|
+
<div class="kawa-search__body" aria-live="polite">
|
|
164
|
+
<p v-if="status === 'loading'" class="kawa-search__state" role="status">
|
|
165
|
+
<span class="kawa-search__loader" aria-hidden="true" />
|
|
166
|
+
{{ props.translations.loadingLabel }}
|
|
167
|
+
</p>
|
|
168
|
+
<div v-else-if="status === 'error'" class="kawa-search__state" role="alert">
|
|
169
|
+
<span>{{ props.translations.errorLabel }}</span>
|
|
170
|
+
<button type="button" class="kawa-search__retry" @click="load">
|
|
171
|
+
{{ props.translations.retryLabel }}
|
|
172
|
+
</button>
|
|
173
|
+
</div>
|
|
174
|
+
<p v-else-if="!hasQuery" class="kawa-search__state">
|
|
175
|
+
{{ props.translations.emptyLabel }}
|
|
176
|
+
</p>
|
|
177
|
+
<p v-else-if="results.length === 0" class="kawa-search__state">
|
|
178
|
+
{{ props.translations.noResultsLabel }} “<strong>{{ query.trim() }}</strong>”
|
|
179
|
+
</p>
|
|
180
|
+
<div v-else ref="resultsScroll" class="kawa-search__results-scroll">
|
|
181
|
+
<ul id="kawa-search-results" class="kawa-search__results" role="listbox">
|
|
182
|
+
<li
|
|
183
|
+
v-for="(result, index) in results"
|
|
184
|
+
:id="`kawa-search-result-${index}`"
|
|
185
|
+
:key="result.id"
|
|
186
|
+
role="option"
|
|
187
|
+
:aria-selected="selectedIndex === index"
|
|
188
|
+
:data-search-index="index"
|
|
189
|
+
>
|
|
190
|
+
<SearchResult
|
|
191
|
+
:result="result"
|
|
192
|
+
:query="query"
|
|
193
|
+
:selected="selectedIndex === index"
|
|
194
|
+
@activate="selectedIndex = index"
|
|
195
|
+
@choose="close"
|
|
196
|
+
/>
|
|
197
|
+
</li>
|
|
198
|
+
</ul>
|
|
199
|
+
</div>
|
|
200
|
+
</div>
|
|
201
|
+
|
|
202
|
+
<div class="kawa-search__shortcuts" aria-hidden="true">
|
|
203
|
+
<span><kbd>↑</kbd><kbd>↓</kbd>{{ props.translations.navigateLabel }}</span>
|
|
204
|
+
<span><kbd>↵</kbd>{{ props.translations.selectLabel }}</span>
|
|
205
|
+
<span><kbd>Esc</kbd>{{ props.translations.closeLabel }}</span>
|
|
206
|
+
</div>
|
|
207
|
+
</div>
|
|
208
|
+
</dialog>
|
|
209
|
+
</template>
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { LocalSearchResult } from '../search'
|
|
3
|
+
import { useSite, withBase } from 'kawapress/client'
|
|
4
|
+
import { computed } from 'vue'
|
|
5
|
+
import { createSearchExcerpt, highlightSearchText } from '../search-highlight'
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
result: LocalSearchResult
|
|
9
|
+
query: string
|
|
10
|
+
selected: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Emits {
|
|
14
|
+
activate: []
|
|
15
|
+
choose: []
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const props = defineProps<Props>()
|
|
19
|
+
const emit = defineEmits<Emits>()
|
|
20
|
+
const site = useSite()
|
|
21
|
+
const href = computed(() => withBase(props.result.id, site.value.base))
|
|
22
|
+
const titleParts = computed(() => highlightSearchText(
|
|
23
|
+
props.result.title,
|
|
24
|
+
props.query,
|
|
25
|
+
))
|
|
26
|
+
const excerptParts = computed(() => highlightSearchText(
|
|
27
|
+
createSearchExcerpt(props.result.text, props.query),
|
|
28
|
+
props.query,
|
|
29
|
+
))
|
|
30
|
+
const ariaLabel = computed(() => (
|
|
31
|
+
[...props.result.titles, props.result.title].join(' › ')
|
|
32
|
+
))
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
<template>
|
|
36
|
+
<a
|
|
37
|
+
:href="href"
|
|
38
|
+
class="kawa-search-result"
|
|
39
|
+
:class="{ 'is-selected': props.selected }"
|
|
40
|
+
:aria-label="ariaLabel"
|
|
41
|
+
@click="emit('choose')"
|
|
42
|
+
@focus="emit('activate')"
|
|
43
|
+
@mouseenter="emit('activate')"
|
|
44
|
+
>
|
|
45
|
+
<span v-if="props.result.titles.length" class="kawa-search-result__path">
|
|
46
|
+
{{ props.result.titles.join(' › ') }}
|
|
47
|
+
</span>
|
|
48
|
+
<span class="kawa-search-result__title">
|
|
49
|
+
<template v-for="(part, index) in titleParts" :key="index">
|
|
50
|
+
<mark v-if="part.highlighted">{{ part.text }}</mark>
|
|
51
|
+
<template v-else>{{ part.text }}</template>
|
|
52
|
+
</template>
|
|
53
|
+
</span>
|
|
54
|
+
<span v-if="props.result.text" class="kawa-search-result__excerpt">
|
|
55
|
+
<template v-for="(part, index) in excerptParts" :key="index">
|
|
56
|
+
<mark v-if="part.highlighted">{{ part.text }}</mark>
|
|
57
|
+
<template v-else>{{ part.text }}</template>
|
|
58
|
+
</template>
|
|
59
|
+
</span>
|
|
60
|
+
</a>
|
|
61
|
+
</template>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type MiniSearch from 'minisearch'
|
|
2
|
+
import type { LocalSearchResult, SearchDocument } from '../search'
|
|
3
|
+
import { useLocale } from 'kawapress/client'
|
|
4
|
+
import { searchIndexLoaders } from 'virtual:kawapress-search-index'
|
|
5
|
+
import { computed, onScopeDispose, readonly, shallowRef, watch } from 'vue'
|
|
6
|
+
|
|
7
|
+
export type LocalSearchStatus = 'loading' | 'ready' | 'error'
|
|
8
|
+
|
|
9
|
+
export function useLocalSearch() {
|
|
10
|
+
const { localeIndex } = useLocale()
|
|
11
|
+
const query = shallowRef('')
|
|
12
|
+
const index = shallowRef<MiniSearch<SearchDocument> | null>(null)
|
|
13
|
+
const status = shallowRef<LocalSearchStatus>('loading')
|
|
14
|
+
let requestId = 0
|
|
15
|
+
let active = true
|
|
16
|
+
|
|
17
|
+
const results = computed<LocalSearchResult[]>(() => {
|
|
18
|
+
const value = query.value.trim()
|
|
19
|
+
if (!value || !index.value) {
|
|
20
|
+
return []
|
|
21
|
+
}
|
|
22
|
+
return index.value.search(value).slice(0, 12) as LocalSearchResult[]
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
async function load(): Promise<void> {
|
|
26
|
+
const currentRequest = ++requestId
|
|
27
|
+
status.value = 'loading'
|
|
28
|
+
index.value = null
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const loader = searchIndexLoaders[localeIndex.value]
|
|
32
|
+
?? searchIndexLoaders.root
|
|
33
|
+
if (!loader) {
|
|
34
|
+
throw new Error(`Missing search index for locale ${JSON.stringify(localeIndex.value)}`)
|
|
35
|
+
}
|
|
36
|
+
const [{ loadSearchIndex }, indexModule] = await Promise.all([
|
|
37
|
+
import('../search'),
|
|
38
|
+
loader(),
|
|
39
|
+
])
|
|
40
|
+
if (!active || currentRequest !== requestId) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
index.value = loadSearchIndex(indexModule.default)
|
|
44
|
+
status.value = 'ready'
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (!active || currentRequest !== requestId) {
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
console.error('KawaPress: failed to load the local search index.', error)
|
|
51
|
+
status.value = 'error'
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
watch(localeIndex, load, { immediate: true })
|
|
56
|
+
onScopeDispose(() => {
|
|
57
|
+
active = false
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
query,
|
|
62
|
+
results,
|
|
63
|
+
status: readonly(status),
|
|
64
|
+
load,
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { KawaPressPlugin, SiteConfig } from 'kawapress'
|
|
2
|
+
import type { SearchCalloutOptions } from './search-index-plugin'
|
|
3
|
+
import { definePlugin } from 'kawapress'
|
|
4
|
+
import { searchIndexPlugin } from './search-index-plugin'
|
|
5
|
+
|
|
6
|
+
export type SearchPluginOptions = SearchCalloutOptions
|
|
7
|
+
|
|
8
|
+
export function searchPlugin(options: SearchPluginOptions = {}): KawaPressPlugin {
|
|
9
|
+
let siteConfig: SiteConfig | undefined
|
|
10
|
+
|
|
11
|
+
return definePlugin({
|
|
12
|
+
name: '@kawapress/plugin-search',
|
|
13
|
+
setup(api) {
|
|
14
|
+
api.config((config) => {
|
|
15
|
+
siteConfig = config
|
|
16
|
+
})
|
|
17
|
+
api.vite((config) => {
|
|
18
|
+
config.plugins ??= []
|
|
19
|
+
config.plugins.push(searchIndexPlugin({
|
|
20
|
+
srcDir: siteConfig?.srcDir ?? '.',
|
|
21
|
+
locales: Object.keys(siteConfig?.locales ?? { root: {} }),
|
|
22
|
+
localeLanguages: Object.fromEntries(
|
|
23
|
+
Object.entries(siteConfig?.locales ?? {}).map(([locale, config]) => (
|
|
24
|
+
[locale, config.lang]
|
|
25
|
+
)),
|
|
26
|
+
),
|
|
27
|
+
callouts: options,
|
|
28
|
+
}))
|
|
29
|
+
})
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineRuntimePlugin } from 'kawapress'
|
|
2
|
+
import Search from './components/Search.vue'
|
|
3
|
+
import './search.css'
|
|
4
|
+
|
|
5
|
+
export default defineRuntimePlugin({
|
|
6
|
+
name: '@kawapress/plugin-search',
|
|
7
|
+
setup(api) {
|
|
8
|
+
api.vueApp((app) => {
|
|
9
|
+
app.component('KawaSearch', Search)
|
|
10
|
+
})
|
|
11
|
+
},
|
|
12
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface SearchTextPart {
|
|
2
|
+
text: string
|
|
3
|
+
highlighted: boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function highlightSearchText(
|
|
7
|
+
text: string,
|
|
8
|
+
query: string,
|
|
9
|
+
): SearchTextPart[] {
|
|
10
|
+
const terms = query.trim().split(/\s+/).filter(Boolean)
|
|
11
|
+
if (terms.length === 0) {
|
|
12
|
+
return [{ text, highlighted: false }]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const source = terms
|
|
16
|
+
.sort((left, right) => right.length - left.length)
|
|
17
|
+
.map(escapeRegExp)
|
|
18
|
+
.join('|')
|
|
19
|
+
const splitPattern = new RegExp(`(${source})`, 'giu')
|
|
20
|
+
const highlightPattern = new RegExp(`^(?:${source})$`, 'iu')
|
|
21
|
+
return text.split(splitPattern).filter(Boolean).map(part => ({
|
|
22
|
+
text: part,
|
|
23
|
+
highlighted: highlightPattern.test(part),
|
|
24
|
+
}))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createSearchExcerpt(
|
|
28
|
+
text: string,
|
|
29
|
+
query: string,
|
|
30
|
+
maxLength = 180,
|
|
31
|
+
): string {
|
|
32
|
+
const normalized = text.replace(/\s+/g, ' ').trim()
|
|
33
|
+
if (normalized.length <= maxLength) {
|
|
34
|
+
return normalized
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const terms = query.trim().toLocaleLowerCase().split(/\s+/).filter(Boolean)
|
|
38
|
+
const lowerText = normalized.toLocaleLowerCase()
|
|
39
|
+
const matchIndex = terms
|
|
40
|
+
.map(term => lowerText.indexOf(term))
|
|
41
|
+
.filter(index => index >= 0)
|
|
42
|
+
.sort((left, right) => left - right)[0] ?? 0
|
|
43
|
+
const start = Math.max(0, Math.min(
|
|
44
|
+
matchIndex - Math.floor(maxLength / 3),
|
|
45
|
+
normalized.length - maxLength,
|
|
46
|
+
))
|
|
47
|
+
const excerpt = normalized.slice(start, start + maxLength).trim()
|
|
48
|
+
return `${start > 0 ? '…' : ''}${excerpt}${start + maxLength < normalized.length ? '…' : ''}`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function escapeRegExp(value: string): string {
|
|
52
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
53
|
+
}
|