@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/src/search.css ADDED
@@ -0,0 +1,476 @@
1
+ .kawa-search-button,
2
+ .kawa-search,
3
+ .kawa-search * {
4
+ box-sizing: border-box;
5
+ }
6
+
7
+ .kawa-search-button {
8
+ display: grid;
9
+ flex: none;
10
+ place-items: center;
11
+ width: 2.25rem;
12
+ height: 2.25rem;
13
+ padding: 0;
14
+ color: var(--kawa-search-c-text-2, light-dark(
15
+ oklch(0.5 0.012 286),
16
+ oklch(0.68 0.012 286)
17
+ ));
18
+ font: inherit;
19
+ background: transparent;
20
+ border: 0;
21
+ border-radius: 0.5rem;
22
+ cursor: pointer;
23
+ transition:
24
+ color 180ms cubic-bezier(0.22, 1, 0.36, 1),
25
+ background-color 180ms cubic-bezier(0.22, 1, 0.36, 1);
26
+ }
27
+
28
+ .kawa-search-button:hover {
29
+ color: var(--kawa-search-c-text-1, light-dark(
30
+ oklch(0.32 0.012 286),
31
+ oklch(0.9 0.01 286)
32
+ ));
33
+ background: var(--kawa-search-c-bg-soft, light-dark(
34
+ oklch(0.965 0.005 286),
35
+ oklch(0.26 0.01 286)
36
+ ));
37
+ }
38
+
39
+ .kawa-search-button:focus-visible,
40
+ .kawa-search :where(button, a):focus-visible {
41
+ outline: 2px solid var(--kawa-search-c-brand, light-dark(
42
+ oklch(0.48 0.18 275),
43
+ oklch(0.78 0.13 275)
44
+ ));
45
+ outline-offset: 2px;
46
+ }
47
+
48
+ .kawa-search-button svg,
49
+ .kawa-search__icon,
50
+ .kawa-search__close svg {
51
+ width: 1.125rem;
52
+ height: 1.125rem;
53
+ fill: none;
54
+ stroke: currentcolor;
55
+ stroke-linecap: round;
56
+ stroke-linejoin: round;
57
+ stroke-width: 1.75;
58
+ }
59
+
60
+ .kawa-search-button__text,
61
+ .kawa-search-button__keys {
62
+ display: none;
63
+ }
64
+
65
+ .kawa-search {
66
+ position: fixed;
67
+ inset: 8vh auto auto 50%;
68
+ width: min(calc(100vw - 2rem), 42rem);
69
+ max-width: none;
70
+ max-height: none;
71
+ margin: 0;
72
+ padding: 0;
73
+ color: var(--kawa-search-c-text-1, light-dark(
74
+ oklch(0.32 0.012 286),
75
+ oklch(0.9 0.01 286)
76
+ ));
77
+ font-family: var(--kawa-search-font-family, system-ui, sans-serif);
78
+ background: transparent;
79
+ border: 0;
80
+ overflow: visible;
81
+ transform: translateX(-50%);
82
+ animation: kawa-search-enter 180ms cubic-bezier(0.22, 1, 0.36, 1);
83
+ }
84
+
85
+ .kawa-search::backdrop {
86
+ background: var(--kawa-search-backdrop, oklch(0.12 0.01 286 / 58%));
87
+ animation: kawa-search-backdrop-enter 150ms ease-out;
88
+ }
89
+
90
+ .kawa-search__panel {
91
+ display: flex;
92
+ flex-direction: column;
93
+ max-height: min(84dvh, 42rem);
94
+ overflow: hidden;
95
+ background: var(--kawa-search-c-bg, light-dark(
96
+ oklch(0.995 0.002 286),
97
+ oklch(0.22 0.008 286)
98
+ ));
99
+ border: 1px solid var(--kawa-search-c-border, light-dark(
100
+ oklch(0.8 0.008 286),
101
+ oklch(0.36 0.012 286)
102
+ ));
103
+ border-radius: 0.875rem;
104
+ box-shadow: var(--kawa-search-shadow, 0 1.5rem 4rem oklch(0.08 0.01 286 / 28%));
105
+ }
106
+
107
+ .kawa-search__form {
108
+ display: flex;
109
+ flex: none;
110
+ align-items: center;
111
+ gap: 0.75rem;
112
+ min-height: 3.75rem;
113
+ margin: 0.75rem;
114
+ padding: 0 0.875rem;
115
+ background: var(--kawa-search-c-bg-soft, light-dark(
116
+ oklch(0.965 0.005 286),
117
+ oklch(0.26 0.01 286)
118
+ ));
119
+ border: 1px solid var(--kawa-search-c-divider, light-dark(
120
+ oklch(0.9 0.006 286),
121
+ oklch(0.29 0.01 286)
122
+ ));
123
+ border-radius: 0.625rem;
124
+ }
125
+
126
+ .kawa-search__form:focus-within {
127
+ border-color: var(--kawa-search-c-border, light-dark(
128
+ oklch(0.8 0.008 286),
129
+ oklch(0.36 0.012 286)
130
+ ));
131
+ }
132
+
133
+ .kawa-search__icon {
134
+ flex: none;
135
+ color: var(--kawa-search-c-brand, light-dark(
136
+ oklch(0.48 0.18 275),
137
+ oklch(0.78 0.13 275)
138
+ ));
139
+ }
140
+
141
+ .kawa-search__label {
142
+ position: absolute;
143
+ width: 1px;
144
+ height: 1px;
145
+ padding: 0;
146
+ margin: -1px;
147
+ overflow: hidden;
148
+ clip: rect(0, 0, 0, 0);
149
+ white-space: nowrap;
150
+ }
151
+
152
+ .kawa-search__input {
153
+ min-width: 0;
154
+ width: 100%;
155
+ padding: 0.75rem 0;
156
+ color: inherit;
157
+ font: inherit;
158
+ font-size: 1rem;
159
+ background: transparent;
160
+ border: 0;
161
+ outline: none;
162
+ }
163
+
164
+ .kawa-search__input::placeholder {
165
+ color: var(--kawa-search-c-text-3, light-dark(
166
+ oklch(0.64 0.01 286),
167
+ oklch(0.52 0.012 286)
168
+ ));
169
+ }
170
+
171
+ .kawa-search__input::-webkit-search-cancel-button {
172
+ display: none;
173
+ }
174
+
175
+ .kawa-search__close {
176
+ display: grid;
177
+ flex: none;
178
+ place-items: center;
179
+ width: 2rem;
180
+ height: 2rem;
181
+ padding: 0;
182
+ color: var(--kawa-search-c-text-2, light-dark(
183
+ oklch(0.5 0.012 286),
184
+ oklch(0.68 0.012 286)
185
+ ));
186
+ background: transparent;
187
+ border: 0;
188
+ border-radius: 0.375rem;
189
+ cursor: pointer;
190
+ }
191
+
192
+ .kawa-search__close:hover {
193
+ color: var(--kawa-search-c-text-1, light-dark(
194
+ oklch(0.32 0.012 286),
195
+ oklch(0.9 0.01 286)
196
+ ));
197
+ background: color-mix(in oklab, currentcolor 8%, transparent);
198
+ }
199
+
200
+ .kawa-search__body {
201
+ display: flex;
202
+ min-height: 12rem;
203
+ min-width: 0;
204
+ overflow: hidden;
205
+ border-top: 1px solid var(--kawa-search-c-divider, light-dark(
206
+ oklch(0.9 0.006 286),
207
+ oklch(0.29 0.01 286)
208
+ ));
209
+ }
210
+
211
+ .kawa-search__state {
212
+ display: flex;
213
+ flex: 1;
214
+ align-items: center;
215
+ justify-content: center;
216
+ gap: 0.75rem;
217
+ min-height: 12rem;
218
+ padding: 2rem;
219
+ color: var(--kawa-search-c-text-2, light-dark(
220
+ oklch(0.5 0.012 286),
221
+ oklch(0.68 0.012 286)
222
+ ));
223
+ font-size: 0.875rem;
224
+ text-align: center;
225
+ }
226
+
227
+ .kawa-search__loader {
228
+ width: 1rem;
229
+ height: 1rem;
230
+ border: 2px solid var(--kawa-search-c-divider, light-dark(
231
+ oklch(0.9 0.006 286),
232
+ oklch(0.29 0.01 286)
233
+ ));
234
+ border-top-color: var(--kawa-search-c-brand, light-dark(
235
+ oklch(0.48 0.18 275),
236
+ oklch(0.78 0.13 275)
237
+ ));
238
+ border-radius: 50%;
239
+ animation: kawa-search-spin 700ms linear infinite;
240
+ }
241
+
242
+ .kawa-search__retry {
243
+ padding: 0.375rem 0.75rem;
244
+ color: var(--kawa-search-c-brand, light-dark(
245
+ oklch(0.48 0.18 275),
246
+ oklch(0.78 0.13 275)
247
+ ));
248
+ font: inherit;
249
+ font-weight: 600;
250
+ background: transparent;
251
+ border: 1px solid currentcolor;
252
+ border-radius: 0.375rem;
253
+ cursor: pointer;
254
+ }
255
+
256
+ .kawa-search__results-scroll {
257
+ width: 100%;
258
+ max-height: min(62dvh, 32rem);
259
+ padding: 0.5rem;
260
+ overflow: auto;
261
+ overscroll-behavior: contain;
262
+ }
263
+
264
+ .kawa-search__results {
265
+ display: flex;
266
+ flex-direction: column;
267
+ gap: 0.125rem;
268
+ margin: 0;
269
+ padding: 0;
270
+ list-style: none;
271
+ }
272
+
273
+ .kawa-search-result {
274
+ display: flex;
275
+ flex-direction: column;
276
+ gap: 0.25rem;
277
+ padding: 0.75rem 0.875rem;
278
+ color: inherit;
279
+ text-decoration: none;
280
+ border-radius: 0.5rem;
281
+ outline: none;
282
+ }
283
+
284
+ .kawa-search-result:hover,
285
+ .kawa-search-result.is-selected {
286
+ background: var(--kawa-search-c-bg-soft, light-dark(
287
+ oklch(0.965 0.005 286),
288
+ oklch(0.26 0.01 286)
289
+ ));
290
+ }
291
+
292
+ .kawa-search-result.is-selected {
293
+ box-shadow: inset 0 0 0 1px var(--kawa-search-c-divider, light-dark(
294
+ oklch(0.9 0.006 286),
295
+ oklch(0.29 0.01 286)
296
+ ));
297
+ }
298
+
299
+ .kawa-search-result__path {
300
+ color: var(--kawa-search-c-text-3, light-dark(
301
+ oklch(0.64 0.01 286),
302
+ oklch(0.52 0.012 286)
303
+ ));
304
+ font-size: 0.75rem;
305
+ line-height: 1.4;
306
+ }
307
+
308
+ .kawa-search-result__title {
309
+ color: var(--kawa-search-c-text-1, light-dark(
310
+ oklch(0.32 0.012 286),
311
+ oklch(0.9 0.01 286)
312
+ ));
313
+ font-size: 0.9375rem;
314
+ font-weight: 650;
315
+ line-height: 1.5;
316
+ }
317
+
318
+ .kawa-search-result__excerpt {
319
+ display: -webkit-box;
320
+ overflow: hidden;
321
+ color: var(--kawa-search-c-text-2, light-dark(
322
+ oklch(0.5 0.012 286),
323
+ oklch(0.68 0.012 286)
324
+ ));
325
+ font-size: 0.8125rem;
326
+ line-height: 1.55;
327
+ -webkit-box-orient: vertical;
328
+ -webkit-line-clamp: 2;
329
+ }
330
+
331
+ .kawa-search-result mark {
332
+ padding: 0 0.1em;
333
+ color: inherit;
334
+ background: color-mix(
335
+ in oklab,
336
+ var(--kawa-search-c-brand, oklch(0.58 0.17 275)) 20%,
337
+ transparent
338
+ );
339
+ border-radius: 0.15rem;
340
+ }
341
+
342
+ .kawa-search__shortcuts {
343
+ display: flex;
344
+ flex: none;
345
+ align-items: center;
346
+ gap: 1rem;
347
+ min-height: 2.75rem;
348
+ padding: 0 1rem;
349
+ color: var(--kawa-search-c-text-3, light-dark(
350
+ oklch(0.64 0.01 286),
351
+ oklch(0.52 0.012 286)
352
+ ));
353
+ font-size: 0.75rem;
354
+ border-top: 1px solid var(--kawa-search-c-divider, light-dark(
355
+ oklch(0.9 0.006 286),
356
+ oklch(0.29 0.01 286)
357
+ ));
358
+ }
359
+
360
+ .kawa-search__shortcuts span {
361
+ display: flex;
362
+ align-items: center;
363
+ gap: 0.25rem;
364
+ }
365
+
366
+ .kawa-search__shortcuts kbd,
367
+ .kawa-search-button__keys kbd {
368
+ display: inline-grid;
369
+ min-width: 1.25rem;
370
+ min-height: 1.25rem;
371
+ place-items: center;
372
+ padding: 0 0.25rem;
373
+ color: inherit;
374
+ font: inherit;
375
+ line-height: 1;
376
+ background: color-mix(in oklab, currentcolor 8%, transparent);
377
+ border: 1px solid var(--kawa-search-c-divider, light-dark(
378
+ oklch(0.9 0.006 286),
379
+ oklch(0.29 0.01 286)
380
+ ));
381
+ border-radius: 0.25rem;
382
+ }
383
+
384
+ .kawa-search-button__command {
385
+ display: none;
386
+ }
387
+
388
+ :root[data-kawa-platform='apple'] .kawa-search-button__command {
389
+ display: inline;
390
+ }
391
+
392
+ :root[data-kawa-platform='apple'] .kawa-search-button__control {
393
+ display: none;
394
+ }
395
+
396
+ @media (min-width: 48rem) {
397
+ .kawa-search-button {
398
+ display: flex;
399
+ width: auto;
400
+ gap: 0.625rem;
401
+ padding: 0 0.625rem;
402
+ background: var(--kawa-search-c-bg-soft, light-dark(
403
+ oklch(0.965 0.005 286),
404
+ oklch(0.26 0.01 286)
405
+ ));
406
+ }
407
+
408
+ .kawa-search-button__text {
409
+ display: inline;
410
+ font-size: 0.8125rem;
411
+ font-weight: 500;
412
+ }
413
+
414
+ .kawa-search-button__keys {
415
+ display: flex;
416
+ align-items: center;
417
+ gap: 0.125rem;
418
+ color: var(--kawa-search-c-text-3, light-dark(
419
+ oklch(0.64 0.01 286),
420
+ oklch(0.52 0.012 286)
421
+ ));
422
+ }
423
+ }
424
+
425
+ @media (max-width: 39.999rem) {
426
+ .kawa-search {
427
+ inset: 0;
428
+ width: 100%;
429
+ transform: none;
430
+ height: 100dvh;
431
+ }
432
+
433
+ .kawa-search__panel {
434
+ height: 100dvh;
435
+ max-height: none;
436
+ border: 0;
437
+ border-radius: 0;
438
+ }
439
+
440
+ .kawa-search__body,
441
+ .kawa-search__results-scroll {
442
+ flex: 1;
443
+ max-height: none;
444
+ }
445
+
446
+ .kawa-search__shortcuts {
447
+ display: none;
448
+ }
449
+ }
450
+
451
+ @media (prefers-reduced-motion: reduce) {
452
+ .kawa-search,
453
+ .kawa-search::backdrop,
454
+ .kawa-search__loader {
455
+ animation: none;
456
+ }
457
+ }
458
+
459
+ @keyframes kawa-search-enter {
460
+ from {
461
+ opacity: 0;
462
+ transform: translateX(-50%) translateY(-0.5rem) scale(0.985);
463
+ }
464
+ }
465
+
466
+ @keyframes kawa-search-backdrop-enter {
467
+ from {
468
+ opacity: 0;
469
+ }
470
+ }
471
+
472
+ @keyframes kawa-search-spin {
473
+ to {
474
+ transform: rotate(360deg);
475
+ }
476
+ }
package/src/search.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type { Options, SearchResult } from 'minisearch'
2
+ import MiniSearch from 'minisearch'
3
+
4
+ export interface SearchDocument {
5
+ id: string
6
+ title: string
7
+ titles: string[]
8
+ text: string
9
+ }
10
+
11
+ export type LocalSearchResult = SearchResult & SearchDocument
12
+
13
+ const CJK_OR_WORD_RE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]|[\p{Letter}\p{Number}]+/gu
14
+
15
+ export const searchIndexOptions: Options<SearchDocument> = {
16
+ fields: ['title', 'titles', 'text'],
17
+ storeFields: ['title', 'titles', 'text'],
18
+ tokenize: tokenizeSearchText,
19
+ processTerm: normalizeSearchTerm,
20
+ searchOptions: {
21
+ boost: { title: 4, titles: 2, text: 1 },
22
+ combineWith: 'AND',
23
+ fuzzy: term => term.length >= 5 ? 0.2 : false,
24
+ prefix: true,
25
+ },
26
+ }
27
+
28
+ export function createSearchIndex(
29
+ documents: SearchDocument[] = [],
30
+ ): MiniSearch<SearchDocument> {
31
+ const index = new MiniSearch<SearchDocument>(searchIndexOptions)
32
+ index.addAll(documents)
33
+ return index
34
+ }
35
+
36
+ export function loadSearchIndex(json: string): MiniSearch<SearchDocument> {
37
+ return MiniSearch.loadJSON<SearchDocument>(json, searchIndexOptions)
38
+ }
39
+
40
+ export function tokenizeSearchText(text: string): string[] {
41
+ return text.normalize('NFKC').match(CJK_OR_WORD_RE) ?? []
42
+ }
43
+
44
+ function normalizeSearchTerm(term: string): string | null {
45
+ const normalized = term.toLocaleLowerCase()
46
+ return normalized || null
47
+ }
@@ -0,0 +1,48 @@
1
+ export interface SearchTranslations {
2
+ buttonLabel: string
3
+ placeholder: string
4
+ loadingLabel: string
5
+ emptyLabel: string
6
+ noResultsLabel: string
7
+ errorLabel: string
8
+ retryLabel: string
9
+ navigateLabel: string
10
+ selectLabel: string
11
+ closeLabel: string
12
+ }
13
+
14
+ const EN_TRANSLATIONS: SearchTranslations = {
15
+ buttonLabel: 'Search',
16
+ placeholder: 'Search documentation',
17
+ loadingLabel: 'Loading search index',
18
+ emptyLabel: 'Type to search the documentation',
19
+ noResultsLabel: 'No results found for',
20
+ errorLabel: 'Search could not be loaded',
21
+ retryLabel: 'Try again',
22
+ navigateLabel: 'Navigate',
23
+ selectLabel: 'Open',
24
+ closeLabel: 'Close',
25
+ }
26
+
27
+ const ZH_TRANSLATIONS: SearchTranslations = {
28
+ buttonLabel: '搜索',
29
+ placeholder: '搜索文档',
30
+ loadingLabel: '正在加载搜索索引',
31
+ emptyLabel: '输入关键词开始搜索',
32
+ noResultsLabel: '未找到相关内容',
33
+ errorLabel: '无法加载搜索',
34
+ retryLabel: '重试',
35
+ navigateLabel: '切换结果',
36
+ selectLabel: '打开',
37
+ closeLabel: '关闭',
38
+ }
39
+
40
+ export function resolveSearchTranslations(
41
+ translations: Partial<SearchTranslations> = {},
42
+ lang = 'en',
43
+ ): SearchTranslations {
44
+ const defaults = lang.toLocaleLowerCase().startsWith('zh')
45
+ ? ZH_TRANSLATIONS
46
+ : EN_TRANSLATIONS
47
+ return { ...defaults, ...translations }
48
+ }
@@ -0,0 +1,6 @@
1
+ declare module 'virtual:kawapress-search-index' {
2
+ export const searchIndexLoaders: Record<
3
+ string,
4
+ () => Promise<{ default: string }>
5
+ >
6
+ }