Commit de045e8d05

de045e8d05932ee4956686179f372d5458533c63

parent: 2f464e5a9a

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-05-31 01:23 UTC

Update Search.vue

Layout: unified · split

docs/.vitepress/theme/components/Search.vue +59 −13
@@ -118,7 +118,7 @@ const searchIndex = computedAsync(async () =>
118 prefix: matchExact.value ? false : (term) => term.length > 3, 118 prefix: matchExact.value ? false : (term) => term.length > 3,
119 // Disable max fuzzy if match exact is enabled 119 // Disable max fuzzy if match exact is enabled
120 maxFuzzy: matchExact.value ? 0 : 1, 120 maxFuzzy: matchExact.value ? 0 : 1,
121 boost: { title: 4, text: 2, titles: 1 }, 121 boost: { title: 10, titles: 4, text: 1 },
122 ...(theme.value.search?.provider === 'local' && 122 ...(theme.value.search?.provider === 'local' &&
123 theme.value.search.options?.miniSearch?.searchOptions) 123 theme.value.search.options?.miniSearch?.searchOptions)
124 }, 124 },
@@ -205,6 +205,34 @@ const pageMeta = (() => {
205 205
206const activePageFilter = ref<string | null>(null) 206const activePageFilter = ref<string | null>(null)
207 207
208const 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
232function getDocOrder(id: string) {
233 return docOrder.value.get(id) ?? Number.MAX_SAFE_INTEGER
234}
235
208function getPageKey(id: string) { 236function getPageKey(id: string) {
209 return id.split('#')[0].replace(/\/$/, '') 237 return id.split('#')[0].replace(/\/$/, '')
210} 238}
@@ -361,9 +389,20 @@ watchDebounced(
361 if (!index) return 389 if (!index) return
362 390
363 // Search 391 // Search
364 const _result = index 392 const ranked = index.search(filterTextValue) as (SearchResult & Result)[]
365 .search(filterTextValue) 393
366 .slice(0, 16) as (SearchResult & Result)[] 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 enableNoResults.value = true 406 enableNoResults.value = true
368 407
369 if (!_result) { 408 if (!_result) {
@@ -426,15 +465,22 @@ watchDebounced(
426 465
427 const terms = new Set<string>() 466 const terms = new Set<string>()
428 467
429 results.value = _result.map((r) => { 468 results.value = _result
430 const [id, anchor] = r.id.split('#') 469 .map((r) => {
431 const map = cache.get(`${id}\n${filterTextValue}`) 470 const [id, anchor] = r.id.split('#')
432 const text = map?.get(anchor) ?? '' 471 const map = cache.get(`${id}\n${filterTextValue}`)
433 for (const term in r.match) { 472 const text = map?.get(anchor) ?? ''
434 terms.add(term) 473 for (const term in r.match) {
435 } 474 terms.add(term)
436 return { ...r, text } 475 }
437 }) 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 currentTerms.value = terms 485 currentTerms.value = terms
440 486