Commit de045e8d05
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Search.vue +59 −13
| @@ -118,7 +118,7 @@ const searchIndex = computedAsync(async () => | ||
| 118 | 118 | prefix: matchExact.value ? false : (term) => term.length > 3, |
| 119 | 119 | // Disable max fuzzy if match exact is enabled |
| 120 | 120 | maxFuzzy: matchExact.value ? 0 : 1, |
| 121 | boost: { title: 4, text: 2, titles: 1 }, | |
| 121 | boost: { title: 10, titles: 4, text: 1 }, | |
| 122 | 122 | ...(theme.value.search?.provider === 'local' && |
| 123 | 123 | theme.value.search.options?.miniSearch?.searchOptions) |
| 124 | 124 | }, |
| @@ -205,6 +205,34 @@ const pageMeta = (() => { | ||
| 205 | 205 | |
| 206 | 206 | const activePageFilter = ref<string | null>(null) |
| 207 | 207 | |
| 208 | const docOrder = computed(() => { | |
| 209 | const map = new Map<string, number>() | |
| 210 | const index = searchIndex.value as any | |
| 211 | if (!index) return map | |
| 212 | // _documentIds maps internalDocId -> original id, in insertion order. | |
| 213 | const ids = index._documentIds | |
| 214 | if (ids instanceof Map) { | |
| 215 | let i = 0 | |
| 216 | for (const originalId of ids.values()) { | |
| 217 | map.set(String(originalId), i++) | |
| 218 | } | |
| 219 | } else if (ids && typeof ids === 'object') { | |
| 220 | // Some versions store it as a plain object keyed by numeric doc id. | |
| 221 | const keys = Object.keys(ids) | |
| 222 | .map(Number) | |
| 223 | .sort((a, b) => a - b) | |
| 224 | let i = 0 | |
| 225 | for (const k of keys) { | |
| 226 | map.set(String(ids[k]), i++) | |
| 227 | } | |
| 228 | } | |
| 229 | return map | |
| 230 | }) | |
| 231 | ||
| 232 | function getDocOrder(id: string) { | |
| 233 | return docOrder.value.get(id) ?? Number.MAX_SAFE_INTEGER | |
| 234 | } | |
| 235 | ||
| 208 | 236 | function getPageKey(id: string) { |
| 209 | 237 | return id.split('#')[0].replace(/\/$/, '') |
| 210 | 238 | } |
| @@ -361,9 +389,20 @@ watchDebounced( | ||
| 361 | 389 | if (!index) return |
| 362 | 390 | |
| 363 | 391 | // Search |
| 364 | const _result = index | |
| 365 | .search(filterTextValue) | |
| 366 | .slice(0, 16) as (SearchResult & Result)[] | |
| 392 | const ranked = index.search(filterTextValue) as (SearchResult & Result)[] | |
| 393 | ||
| 394 | // Guaranteed heading matches: any section whose own heading (title) | |
| 395 | // contains the term should always show, regardless of score or cap. | |
| 396 | const titleHits = index.search(filterTextValue, { | |
| 397 | fields: ['title'] | |
| 398 | }) as (SearchResult & Result)[] | |
| 399 | ||
| 400 | const top = ranked.slice(0, 50) | |
| 401 | const seen = new Set(top.map((r) => r.id)) | |
| 402 | const _result = [ | |
| 403 | ...top, | |
| 404 | ...titleHits.filter((r) => !seen.has(r.id)) | |
| 405 | ] as (SearchResult & Result)[] | |
| 367 | 406 | enableNoResults.value = true |
| 368 | 407 | |
| 369 | 408 | if (!_result) { |
| @@ -426,15 +465,22 @@ watchDebounced( | ||
| 426 | 465 | |
| 427 | 466 | const terms = new Set<string>() |
| 428 | 467 | |
| 429 | results.value = _result.map((r) => { | |
| 430 | const [id, anchor] = r.id.split('#') | |
| 431 | const map = cache.get(`${id}\n${filterTextValue}`) | |
| 432 | const text = map?.get(anchor) ?? '' | |
| 433 | for (const term in r.match) { | |
| 434 | terms.add(term) | |
| 435 | } | |
| 436 | return { ...r, text } | |
| 437 | }) | |
| 468 | results.value = _result | |
| 469 | .map((r) => { | |
| 470 | const [id, anchor] = r.id.split('#') | |
| 471 | const map = cache.get(`${id}\n${filterTextValue}`) | |
| 472 | const text = map?.get(anchor) ?? '' | |
| 473 | for (const term in r.match) { | |
| 474 | terms.add(term) | |
| 475 | } | |
| 476 | return { ...r, text } | |
| 477 | }) | |
| 478 | .sort((a, b) => { | |
| 479 | const pageDiff = | |
| 480 | getPageOrder(getPageKey(a.id)) - getPageOrder(getPageKey(b.id)) | |
| 481 | if (pageDiff !== 0) return pageDiff | |
| 482 | return getDocOrder(a.id) - getDocOrder(b.id) | |
| 483 | }) | |
| 438 | 484 | |
| 439 | 485 | currentTerms.value = terms |
| 440 | 486 | |