@stacksjs/bunpress 0.1.21 → 0.2.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.
Files changed (57) hide show
  1. package/dist/bin/cli.d.ts +3 -0
  2. package/dist/bin/cli.js +1 -1
  3. package/dist/bin/commands/doctor.d.ts +1 -0
  4. package/dist/bin/commands/seo.d.ts +19 -0
  5. package/dist/{chunk-qrp18ybj.js → chunk-desvs9bx.js} +1 -1
  6. package/dist/chunk-em1tv2bw.js +38 -0
  7. package/dist/{chunk-kxr6zf7e.js → chunk-kfbpnqaw.js} +1308 -696
  8. package/dist/chunk-qqn901qf.js +393 -0
  9. package/dist/{chunk-qdmkac4z.js → chunk-qzhr4d2q.js} +10 -11
  10. package/dist/chunk-y3z183hg.js +10 -0
  11. package/dist/chunk-yfwm5337.js +2 -0
  12. package/dist/{chunk-m0g9vk1e.js → chunk-zkdm5pr7.js} +401 -194
  13. package/dist/src/build-manifest.d.ts +22 -0
  14. package/dist/src/config.d.ts +12 -0
  15. package/dist/src/data-loader.d.ts +6 -3
  16. package/dist/src/highlighter.d.ts +2 -1
  17. package/dist/src/i18n.d.ts +67 -0
  18. package/dist/src/index.d.ts +2 -0
  19. package/dist/src/index.js +1 -1
  20. package/dist/src/markdown-fences.d.ts +37 -0
  21. package/dist/src/page-meta.d.ts +19 -0
  22. package/dist/src/search-index.d.ts +61 -0
  23. package/dist/src/serve.d.ts +20 -5
  24. package/dist/src/toc.d.ts +20 -2
  25. package/dist/src/types.d.ts +80 -8
  26. package/dist/templates/blocks/alerts/caution.stx +2 -2
  27. package/dist/templates/blocks/alerts/important.stx +2 -2
  28. package/dist/templates/blocks/alerts/note.stx +2 -2
  29. package/dist/templates/blocks/alerts/tip.stx +2 -2
  30. package/dist/templates/blocks/alerts/warning.stx +2 -2
  31. package/dist/templates/blocks/containers/danger.stx +2 -2
  32. package/dist/templates/blocks/containers/details.stx +2 -2
  33. package/dist/templates/blocks/containers/info.stx +2 -2
  34. package/dist/templates/blocks/containers/raw.stx +1 -1
  35. package/dist/templates/blocks/containers/tip.stx +2 -2
  36. package/dist/templates/blocks/containers/warning.stx +2 -2
  37. package/dist/templates/copy-page.stx +81 -0
  38. package/dist/templates/doc-footer.stx +51 -0
  39. package/dist/templates/footer.stx +45 -0
  40. package/dist/templates/hero.stx +9 -2
  41. package/dist/templates/layout-doc.stx +8 -125
  42. package/dist/templates/layout-home.stx +7 -14
  43. package/dist/templates/layout-page.stx +7 -14
  44. package/dist/templates/locale-switcher.stx +115 -0
  45. package/dist/templates/navbar.stx +49 -0
  46. package/dist/templates/page-outline.stx +149 -0
  47. package/dist/templates/page-toc.stx +1 -1
  48. package/dist/templates/search-algolia.stx +85 -0
  49. package/dist/templates/search.stx +661 -0
  50. package/dist/templates/sidebar-section.stx +44 -11
  51. package/dist/templates/sidebar.stx +11 -2
  52. package/dist/themes/vitepress/index.ts +558 -165
  53. package/package.json +16 -7
  54. package/dist/chunk-9cm3e4r4.js +0 -688
  55. package/dist/chunk-mjhqecp5.js +0 -467
  56. package/dist/chunk-wmvgjzgh.js +0 -3
  57. package/dist/chunk-zmxtnx2v.js +0 -8
@@ -0,0 +1,661 @@
1
+ <div class="BPSearch" id="bp-search" hidden>
2
+ <div class="BPSearch-backdrop" data-bp-search-close></div>
3
+ <div class="BPSearch-panel search-modal" role="dialog" aria-modal="true" aria-label="{{ placeholder }}">
4
+ <div class="BPSearch-inputRow">
5
+ <svg class="BPSearch-inputIcon" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
6
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
7
+ </svg>
8
+ <input
9
+ type="search"
10
+ class="BPSearch-input search-input"
11
+ id="bp-search-input"
12
+ placeholder="{{ placeholder }}"
13
+ aria-label="{{ placeholder }}"
14
+ aria-controls="bp-search-results"
15
+ autocomplete="off"
16
+ autocorrect="off"
17
+ autocapitalize="off"
18
+ spellcheck="false"
19
+ />
20
+ <button class="BPSearch-cancel" type="button" data-bp-search-close>Esc</button>
21
+ </div>
22
+ <div class="BPSearch-results" id="bp-search-results" role="listbox" aria-label="Search results"></div>
23
+ <div class="BPSearch-footer">
24
+ <span><kbd>↑</kbd><kbd>↓</kbd> to navigate</span>
25
+ <span><kbd>↵</kbd> to select</span>
26
+ <span><kbd>esc</kbd> to close</span>
27
+ </div>
28
+ </div>
29
+ </div>
30
+
31
+ <style>
32
+ .BPSearch {
33
+ position: fixed;
34
+ inset: 0;
35
+ z-index: var(--bp-z-index-search, 200);
36
+ display: flex;
37
+ justify-content: center;
38
+ /* Anchored near the top rather than centred: the results list grows
39
+ downward, and a centred panel would jump as it does. */
40
+ align-items: flex-start;
41
+ padding: 12vh 16px 16px;
42
+ }
43
+
44
+ .BPSearch[hidden] {
45
+ display: none;
46
+ }
47
+
48
+ .BPSearch-backdrop {
49
+ position: absolute;
50
+ inset: 0;
51
+ background-color: rgba(15, 15, 17, 0.55);
52
+ backdrop-filter: blur(2px);
53
+ }
54
+
55
+ .BPSearch-panel {
56
+ position: relative;
57
+ display: flex;
58
+ flex-direction: column;
59
+ width: 100%;
60
+ max-width: 640px;
61
+ max-height: 100%;
62
+ overflow: hidden;
63
+ background-color: var(--bp-c-bg);
64
+ border: 1px solid var(--bp-c-divider);
65
+ border-radius: 12px;
66
+ box-shadow: 0 16px 48px rgba(0, 0, 0, 0.24);
67
+ }
68
+
69
+ .BPSearch-inputRow {
70
+ display: flex;
71
+ align-items: center;
72
+ gap: 10px;
73
+ padding: 12px 14px;
74
+ border-bottom: 1px solid var(--bp-c-divider);
75
+ }
76
+
77
+ .BPSearch-inputIcon {
78
+ width: 18px;
79
+ height: 18px;
80
+ flex-shrink: 0;
81
+ color: var(--bp-c-text-3);
82
+ }
83
+
84
+ .BPSearch-input {
85
+ flex: 1;
86
+ min-width: 0;
87
+ background: transparent;
88
+ border: none;
89
+ outline: none;
90
+ font-family: inherit;
91
+ font-size: 16px;
92
+ color: var(--bp-c-text-1);
93
+ }
94
+
95
+ .BPSearch-input::placeholder {
96
+ color: var(--bp-c-text-3);
97
+ }
98
+
99
+ /* The type=search clear affordance differs per engine and collides with the
100
+ Esc button; the Esc button is the documented way out. */
101
+ .BPSearch-input::-webkit-search-cancel-button {
102
+ -webkit-appearance: none;
103
+ appearance: none;
104
+ }
105
+
106
+ .BPSearch-cancel {
107
+ flex-shrink: 0;
108
+ padding: 3px 7px;
109
+ font-family: inherit;
110
+ font-size: 11px;
111
+ font-weight: 600;
112
+ color: var(--bp-c-text-3);
113
+ background-color: var(--bp-c-bg-soft);
114
+ border: 1px solid var(--bp-c-divider);
115
+ border-radius: 5px;
116
+ cursor: pointer;
117
+ transition: color 0.2s, border-color 0.2s;
118
+ }
119
+
120
+ .BPSearch-cancel:hover {
121
+ color: var(--bp-c-text-1);
122
+ border-color: var(--bp-c-text-3);
123
+ }
124
+
125
+ .BPSearch-results {
126
+ flex: 1;
127
+ min-height: 0;
128
+ overflow-y: auto;
129
+ overscroll-behavior: contain;
130
+ padding: 8px;
131
+ }
132
+
133
+ .BPSearch-results:empty {
134
+ display: none;
135
+ }
136
+
137
+ .BPSearch-hit {
138
+ display: block;
139
+ padding: 10px 12px;
140
+ border-radius: 8px;
141
+ text-decoration: none;
142
+ color: inherit;
143
+ border: 1px solid transparent;
144
+ }
145
+
146
+ .BPSearch-hit + .BPSearch-hit {
147
+ margin-top: 2px;
148
+ }
149
+
150
+ .BPSearch-hit:hover,
151
+ .BPSearch-hit.is-active {
152
+ background-color: var(--bp-c-default-soft, var(--bp-c-gray-soft));
153
+ border-color: var(--bp-c-divider);
154
+ }
155
+
156
+ .BPSearch-hit-breadcrumb {
157
+ display: block;
158
+ font-size: 11px;
159
+ font-weight: 500;
160
+ text-transform: uppercase;
161
+ letter-spacing: 0.04em;
162
+ color: var(--bp-c-text-3);
163
+ margin-bottom: 2px;
164
+ }
165
+
166
+ .BPSearch-hit-title {
167
+ display: block;
168
+ font-size: 14px;
169
+ font-weight: 600;
170
+ color: var(--bp-c-text-1);
171
+ line-height: 1.4;
172
+ }
173
+
174
+ .BPSearch-hit-text {
175
+ /* Two lines of preview: enough to judge relevance, not enough to turn the
176
+ list into a wall of prose. */
177
+ display: -webkit-box;
178
+ -webkit-line-clamp: 2;
179
+ -webkit-box-orient: vertical;
180
+ overflow: hidden;
181
+ margin-top: 2px;
182
+ font-size: 13px;
183
+ line-height: 1.5;
184
+ color: var(--bp-c-text-2);
185
+ }
186
+
187
+ .BPSearch-hit mark {
188
+ padding: 0;
189
+ background: transparent;
190
+ color: var(--bp-c-brand-1);
191
+ font-weight: 700;
192
+ }
193
+
194
+ .BPSearch-empty {
195
+ padding: 28px 16px;
196
+ text-align: center;
197
+ font-size: 14px;
198
+ color: var(--bp-c-text-2);
199
+ }
200
+
201
+ .BPSearch-empty strong {
202
+ color: var(--bp-c-text-1);
203
+ overflow-wrap: anywhere;
204
+ }
205
+
206
+ .BPSearch-footer {
207
+ display: flex;
208
+ gap: 16px;
209
+ padding: 8px 14px;
210
+ border-top: 1px solid var(--bp-c-divider);
211
+ background-color: var(--bp-c-bg-soft);
212
+ font-size: 11px;
213
+ color: var(--bp-c-text-3);
214
+ }
215
+
216
+ .BPSearch-footer kbd {
217
+ display: inline-block;
218
+ min-width: 16px;
219
+ margin-right: 3px;
220
+ padding: 1px 4px;
221
+ font-family: inherit;
222
+ font-size: 10px;
223
+ text-align: center;
224
+ color: var(--bp-c-text-2);
225
+ background-color: var(--bp-c-bg);
226
+ border: 1px solid var(--bp-c-divider);
227
+ border-radius: 4px;
228
+ }
229
+
230
+ /* The documented class names (.search-modal, .search-input, .search-result,
231
+ .search-highlight) are applied alongside the BPSearch-* ones so the CSS in
232
+ the search guide targets real elements instead of dead selectors. */
233
+
234
+ /* Phones: a centred card wastes the screen and fights the keyboard. */
235
+ @media (max-width: 639px) {
236
+ .BPSearch {
237
+ padding: 0;
238
+ }
239
+
240
+ .BPSearch-panel {
241
+ max-width: none;
242
+ height: 100%;
243
+ max-height: none;
244
+ border: none;
245
+ border-radius: 0;
246
+ }
247
+
248
+ .BPSearch-footer {
249
+ display: none;
250
+ }
251
+ }
252
+ </style>
253
+
254
+
255
+ <script type="application/json" id="bp-search-config">{!! runtimeConfig !!}</script>
256
+ <script>
257
+ (function () {
258
+ if (window.__bpSearchBound) return;
259
+ window.__bpSearchBound = true;
260
+
261
+ var CONFIG = (function () {
262
+ try { return JSON.parse(document.getElementById('bp-search-config').textContent); }
263
+ catch (_) { return {}; }
264
+ })();
265
+
266
+ // A config function arrives as source text (it was written in a build-time
267
+ // module). Compiling it can fail under a strict CSP, so every use falls back
268
+ // to the built-in behaviour rather than breaking search outright.
269
+ function compile(source) {
270
+ if (!source) return null;
271
+ try {
272
+ // eslint-disable-next-line no-new-func
273
+ var fn = new Function('return (' + source + ')')();
274
+ return typeof fn === 'function' ? fn : null;
275
+ } catch (_) {
276
+ return null;
277
+ }
278
+ }
279
+
280
+ var customTokenize = compile(CONFIG.tokenizeSource);
281
+ var onSearch = compile(CONFIG.onSearchSource);
282
+
283
+ var indexPromise = null;
284
+ var records = [];
285
+ var activeIndex = 0;
286
+ var lastFocused = null;
287
+
288
+ function el(id) { return document.getElementById(id); }
289
+ function root() { return el('bp-search'); }
290
+
291
+ function loadIndex() {
292
+ if (indexPromise) return indexPromise;
293
+ indexPromise = fetch(CONFIG.indexUrl)
294
+ .then(function (res) { return res.ok ? res.json() : []; })
295
+ .then(function (data) { records = Array.isArray(data) ? data : []; return records; })
296
+ .catch(function () { records = []; return records; });
297
+ return indexPromise;
298
+ }
299
+
300
+ function escapeHtml(value) {
301
+ return String(value)
302
+ .replace(/&/g, '&amp;')
303
+ .replace(/</g, '&lt;')
304
+ .replace(/>/g, '&gt;')
305
+ .replace(/"/g, '&quot;');
306
+ }
307
+
308
+ function escapeRegExp(value) {
309
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
310
+ }
311
+
312
+ /* Tokenizing and stemming
313
+ ---------------------------------------------------------------------- */
314
+
315
+ function defaultTokenize(text) {
316
+ return String(text).toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
317
+ }
318
+
319
+ function tokenize(text) {
320
+ if (!customTokenize) return defaultTokenize(text);
321
+ try {
322
+ var out = customTokenize(String(text));
323
+ return Array.isArray(out) ? out.map(String).filter(Boolean) : defaultTokenize(text);
324
+ } catch (_) {
325
+ return defaultTokenize(text);
326
+ }
327
+ }
328
+
329
+ // A light suffix stripper, not a full Porter implementation: it collapses
330
+ // the inflections that actually cost documentation searches hits (plurals,
331
+ // -ing/-ed, -ly) and leaves everything else alone. Over-stemming a technical
332
+ // vocabulary does more harm than the recall it buys.
333
+ function stem(token) {
334
+ if (CONFIG.stemmer !== 'english' || token.length < 4) return token;
335
+ var word = token;
336
+ if (/(sses|ies)$/.test(word)) word = word.slice(0, -2);
337
+ else if (/[^s]s$/.test(word)) word = word.slice(0, -1);
338
+ if (/(ational)$/.test(word)) word = word.slice(0, -5) + 'e';
339
+ else if (/(ing|ed)$/.test(word) && /[aeiouy]/.test(word.replace(/(ing|ed)$/, ''))) {
340
+ word = word.replace(/(ing|ed)$/, '');
341
+ if (/(bb|dd|gg|mm|nn|pp|rr|tt)$/.test(word)) word = word.slice(0, -1);
342
+ }
343
+ if (/ly$/.test(word) && word.length > 4) word = word.slice(0, -2);
344
+ return word || token;
345
+ }
346
+
347
+ function prepareTerms(text) {
348
+ var seen = Object.create(null);
349
+ var out = [];
350
+ var tokens = tokenize(text);
351
+ for (var i = 0; i < tokens.length; i++) {
352
+ var value = stem(tokens[i]);
353
+ if (!seen[value]) { seen[value] = true; out.push(value); }
354
+ }
355
+ return out;
356
+ }
357
+
358
+ // Bounded edit distance: bail as soon as the length gap alone exceeds the
359
+ // allowance, so a long word never costs a full matrix.
360
+ function withinDistance(a, b, max) {
361
+ if (a === b) return true;
362
+ if (Math.abs(a.length - b.length) > max) return false;
363
+
364
+ var previous = [];
365
+ for (var j = 0; j <= b.length; j++) previous[j] = j;
366
+
367
+ for (var i = 1; i <= a.length; i++) {
368
+ var current = [i];
369
+ var best = i;
370
+ for (var k = 1; k <= b.length; k++) {
371
+ var cost = a.charAt(i - 1) === b.charAt(k - 1) ? 0 : 1;
372
+ current[k] = Math.min(current[k - 1] + 1, previous[k] + 1, previous[k - 1] + cost);
373
+ if (current[k] < best) best = current[k];
374
+ }
375
+ if (best > max) return false;
376
+ previous = current;
377
+ }
378
+ return previous[b.length] <= max;
379
+ }
380
+
381
+ /* Matching
382
+ ---------------------------------------------------------------------- */
383
+
384
+ // Field names come from the documented searchFields/boost vocabulary:
385
+ // `title` is the section heading, `headings` the page it sits under,
386
+ // `content` the section prose, `keywords` the frontmatter list.
387
+ function fieldValue(record, field) {
388
+ if (field === 'title') return record.title || '';
389
+ if (field === 'headings') return record.page || '';
390
+ if (field === 'content') return record.text || '';
391
+ if (field === 'keywords') return (record.keywords || []).join(' ');
392
+ return '';
393
+ }
394
+
395
+ function prepare(record) {
396
+ if (record.__bp) return record.__bp;
397
+ var prepared = { raw: {}, terms: {} };
398
+ var fields = CONFIG.searchFields || ['title', 'headings', 'content', 'keywords'];
399
+ for (var i = 0; i < fields.length; i++) {
400
+ var field = fields[i];
401
+ var value = fieldValue(record, field);
402
+ prepared.raw[field] = value.toLowerCase();
403
+ prepared.terms[field] = prepareTerms(value);
404
+ }
405
+ Object.defineProperty(record, '__bp', { value: prepared, enumerable: false });
406
+ return prepared;
407
+ }
408
+
409
+ // 0 = no match. Higher means a better kind of match, before field weighting.
410
+ function fieldStrength(prepared, field, rawTerm, stemmedTerm) {
411
+ var haystack = prepared.raw[field] || '';
412
+ if (haystack) {
413
+ if (haystack === rawTerm) return 4;
414
+ if (haystack.indexOf(rawTerm) === 0) return 3;
415
+ if (haystack.indexOf(rawTerm) !== -1) return 2;
416
+ }
417
+
418
+ var tokens = prepared.terms[field] || [];
419
+ for (var i = 0; i < tokens.length; i++) {
420
+ if (tokens[i] === stemmedTerm) return 2;
421
+ }
422
+
423
+ if (CONFIG.fuzzy) {
424
+ for (var j = 0; j < tokens.length; j++) {
425
+ if (withinDistance(tokens[j], stemmedTerm, CONFIG.fuzziness || 1)) return 1;
426
+ }
427
+ }
428
+ return 0;
429
+ }
430
+
431
+ function score(record, terms) {
432
+ var prepared = prepare(record);
433
+ var fields = CONFIG.searchFields || ['title', 'headings', 'content', 'keywords'];
434
+ var boost = CONFIG.boost || { title: 10, headings: 5, content: 1 };
435
+ var total = 0;
436
+
437
+ for (var t = 0; t < terms.length; t++) {
438
+ var matched = 0;
439
+ for (var f = 0; f < fields.length; f++) {
440
+ var field = fields[f];
441
+ var strength = fieldStrength(prepared, field, terms[t].raw, terms[t].stem);
442
+ if (!strength) continue;
443
+ // keywords are an authoring hint, weighted like a heading.
444
+ var weight = field === 'keywords' ? (boost.headings || 5) : (boost[field] || 1);
445
+ matched += strength * weight;
446
+ }
447
+ // Every term must match somewhere, so a two-word query narrows rather
448
+ // than widens the result set.
449
+ if (!matched) return 0;
450
+ total += matched;
451
+ }
452
+
453
+ // Shallower headings are more general destinations, so break ties toward
454
+ // them rather than toward a deeply nested subsection.
455
+ return total + Math.max(0, 6 - (record.level || 6));
456
+ }
457
+
458
+ function queryTerms(query) {
459
+ var raw = String(query).toLowerCase().split(/\s+/).filter(Boolean);
460
+ return raw.map(function (term) { return { raw: term, stem: stem(term) }; });
461
+ }
462
+
463
+ function search(query) {
464
+ if (query.length < (CONFIG.minQueryLength || 1)) return [];
465
+ var terms = queryTerms(query);
466
+ if (!terms.length) return [];
467
+
468
+ var hits = [];
469
+ for (var i = 0; i < records.length; i++) {
470
+ // On a multi-language site the index holds every locale, so results are
471
+ // narrowed to the one being read — otherwise every hit appears N times.
472
+ if (CONFIG.locale && records[i].locale && records[i].locale !== CONFIG.locale) continue;
473
+ var value = score(records[i], terms);
474
+ if (value > 0) hits.push({ record: records[i], score: value });
475
+ }
476
+
477
+ hits.sort(function (a, b) { return b.score - a.score; });
478
+ return hits.slice(0, CONFIG.maxResults || 10).map(function (hit) { return hit.record; });
479
+ }
480
+
481
+ /* Rendering
482
+ ---------------------------------------------------------------------- */
483
+
484
+ function highlight(escaped, terms) {
485
+ if (!CONFIG.result || !CONFIG.result.highlightMatches) return escaped;
486
+ if (!terms.length) return escaped;
487
+ var pattern = new RegExp('(' + terms.map(function (t) { return escapeRegExp(t.raw); }).join('|') + ')', 'gi');
488
+ return escaped.replace(pattern, '<mark class="search-highlight">$1</mark>');
489
+ }
490
+
491
+ // Centre the preview on the first match instead of always showing the
492
+ // opening words, which for a long section rarely contain the query.
493
+ function snippet(text, terms) {
494
+ if (!text) return '';
495
+ var length = (CONFIG.result && CONFIG.result.descriptionLength) || 180;
496
+ var lower = text.toLowerCase();
497
+ var at = -1;
498
+ for (var i = 0; i < terms.length; i++) {
499
+ var found = lower.indexOf(terms[i].raw);
500
+ if (found !== -1 && (at === -1 || found < at)) at = found;
501
+ }
502
+ if (at <= 60) return text.slice(0, length);
503
+ var start = Math.max(0, at - 60);
504
+ return '…' + text.slice(start, start + length);
505
+ }
506
+
507
+ function render(query, hits) {
508
+ var results = el('bp-search-results');
509
+ if (!results) return;
510
+
511
+ if (!query) { results.innerHTML = ''; return; }
512
+
513
+ if (!hits.length) {
514
+ results.innerHTML = '<p class="BPSearch-empty">No results for <strong>' + escapeHtml(query) + '</strong></p>';
515
+ return;
516
+ }
517
+
518
+ var terms = queryTerms(query);
519
+ var showPath = !CONFIG.result || CONFIG.result.showPath;
520
+ var showDescription = !CONFIG.result || CONFIG.result.showDescription;
521
+
522
+ results.innerHTML = hits.map(function (hit, i) {
523
+ var breadcrumb = (showPath && hit.page && hit.title !== hit.page)
524
+ ? '<span class="BPSearch-hit-breadcrumb">' + escapeHtml(hit.page) + '</span>'
525
+ : '';
526
+ var text = showDescription ? snippet(hit.text, terms) : '';
527
+ return '<a class="BPSearch-hit search-result' + (i === 0 ? ' is-active' : '') + '" role="option"' +
528
+ ' aria-selected="' + (i === 0 ? 'true' : 'false') + '" href="' + escapeHtml(hit.url) + '">' +
529
+ breadcrumb +
530
+ '<span class="BPSearch-hit-title">' + highlight(escapeHtml(hit.title || hit.url), terms) + '</span>' +
531
+ (text ? '<span class="BPSearch-hit-text">' + highlight(escapeHtml(text), terms) + '</span>' : '') +
532
+ '</a>';
533
+ }).join('');
534
+ activeIndex = 0;
535
+ }
536
+
537
+ function runSearch(query) {
538
+ return loadIndex().then(function () {
539
+ var hits = search(query);
540
+ render(query, hits);
541
+ // Both an event and the config callback: the event needs no build-time
542
+ // code, the callback matches the documented API.
543
+ document.dispatchEvent(new CustomEvent('bp:search', { detail: { query: query, results: hits } }));
544
+ if (onSearch) {
545
+ try { onSearch(query, hits); }
546
+ catch (err) { console.error('search onSearch handler failed:', err); }
547
+ }
548
+ return hits;
549
+ });
550
+ }
551
+
552
+ /* Dialog
553
+ ---------------------------------------------------------------------- */
554
+
555
+ function hits() { return Array.prototype.slice.call(document.querySelectorAll('.BPSearch-hit')); }
556
+
557
+ function setActive(next) {
558
+ var list = hits();
559
+ if (!list.length) return;
560
+ activeIndex = (next + list.length) % list.length;
561
+ list.forEach(function (node, i) {
562
+ var on = i === activeIndex;
563
+ node.classList.toggle('is-active', on);
564
+ node.setAttribute('aria-selected', on ? 'true' : 'false');
565
+ if (on) node.scrollIntoView({ block: 'nearest' });
566
+ });
567
+ }
568
+
569
+ function open() {
570
+ var node = root();
571
+ if (!node) return;
572
+ lastFocused = document.activeElement;
573
+ node.hidden = false;
574
+ document.documentElement.style.overflow = 'hidden';
575
+ var input = el('bp-search-input');
576
+ if (input) {
577
+ input.focus();
578
+ input.select();
579
+ if (input.value) runSearch(input.value.trim());
580
+ else loadIndex();
581
+ }
582
+ }
583
+
584
+ function close() {
585
+ var node = root();
586
+ if (!node || node.hidden) return;
587
+ node.hidden = true;
588
+ document.documentElement.style.overflow = '';
589
+ if (lastFocused && lastFocused.focus) lastFocused.focus();
590
+ }
591
+
592
+ function isOpen() {
593
+ var node = root();
594
+ return !!node && !node.hidden;
595
+ }
596
+
597
+ function matchesShortcut(e) {
598
+ var keys = (CONFIG.shortcuts && CONFIG.shortcuts.keys) || [];
599
+ for (var i = 0; i < keys.length; i++) {
600
+ var combo = keys[i];
601
+ if (e.key.toLowerCase() !== combo.key) continue;
602
+ if (!!combo.meta !== e.metaKey) continue;
603
+ if (!!combo.ctrl !== e.ctrlKey) continue;
604
+ if (!!combo.alt !== e.altKey) continue;
605
+ if (combo.shift && !e.shiftKey) continue;
606
+ return combo;
607
+ }
608
+ return null;
609
+ }
610
+
611
+ function isTyping() {
612
+ var active = document.activeElement;
613
+ if (!active) return false;
614
+ return /^(INPUT|TEXTAREA|SELECT)$/.test(active.tagName) || active.isContentEditable;
615
+ }
616
+
617
+ document.addEventListener('click', function (e) {
618
+ if (e.target.closest('[data-bp-search-close]')) { e.preventDefault(); close(); return; }
619
+ if (e.target.closest('.BPNavBarSearch')) { e.preventDefault(); open(); return; }
620
+ if (e.target.closest('.BPSearch-hit')) close();
621
+ });
622
+
623
+ document.addEventListener('keydown', function (e) {
624
+ if (CONFIG.shortcuts && CONFIG.shortcuts.keyboard !== false) {
625
+ var combo = matchesShortcut(e);
626
+ // A bare printable shortcut like `/` must not hijack typing.
627
+ var bare = combo && !combo.meta && !combo.ctrl && !combo.alt;
628
+ if (combo && !(bare && (isOpen() || isTyping()))) {
629
+ e.preventDefault();
630
+ isOpen() ? close() : open();
631
+ return;
632
+ }
633
+ }
634
+
635
+ if (!isOpen()) return;
636
+
637
+ // Escape always closes, whatever the shortcut configuration says.
638
+ if (e.key === 'Escape') { e.preventDefault(); close(); }
639
+ else if (e.key === 'ArrowDown') { e.preventDefault(); setActive(activeIndex + 1); }
640
+ else if (e.key === 'ArrowUp') { e.preventDefault(); setActive(activeIndex - 1); }
641
+ else if (e.key === 'Enter') {
642
+ var list = hits();
643
+ if (list[activeIndex]) { e.preventDefault(); list[activeIndex].click(); }
644
+ }
645
+ });
646
+
647
+ var debounce;
648
+ document.addEventListener('input', function (e) {
649
+ if (e.target.id !== 'bp-search-input') return;
650
+ var query = e.target.value.trim();
651
+ clearTimeout(debounce);
652
+ debounce = setTimeout(function () { runSearch(query); }, 80);
653
+ });
654
+
655
+ // Eager loading trades bandwidth for a first search with no wait.
656
+ if (CONFIG.lazy === false) {
657
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', loadIndex);
658
+ else loadIndex();
659
+ }
660
+ })();
661
+ </script>