Commit e6c47a758b
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Search.vue +5 −72
| @@ -326,7 +326,6 @@ interface UrlResult { | ||
| 326 | 326 | anchor: string |
| 327 | 327 | titles: string[] |
| 328 | 328 | highlighted: string |
| 329 | excerptHtml: string | |
| 330 | 329 | } |
| 331 | 330 | |
| 332 | 331 | const urlResults = computed((): UrlResult[] => { |
| @@ -339,8 +338,7 @@ const urlResults = computed((): UrlResult[] => { | ||
| 339 | 338 | .filter(({ href }) => href.toLowerCase().includes(query)) |
| 340 | 339 | .map(({ href, linkText, pageId, anchor, titles }) => ({ |
| 341 | 340 | href, linkText, pageId, anchor, titles, |
| 342 | highlighted: href.replace(regex, '<mark class="url-highlight">$1</mark>'), | |
| 343 | excerptHtml: '' | |
| 341 | highlighted: href.replace(regex, '<mark class="url-highlight">$1</mark>') | |
| 344 | 342 | })) |
| 345 | 343 | }) |
| 346 | 344 | |
| @@ -359,9 +357,8 @@ const urlPageGroups = computed(() => { | ||
| 359 | 357 | }) |
| 360 | 358 | |
| 361 | 359 | const filteredUrlResults = computed(() => { |
| 362 | const src = urlResultsWithExcerpts.value | |
| 363 | if (!urlActivePageFilter.value) return src | |
| 364 | return src.filter((r) => r.pageId === urlActivePageFilter.value) | |
| 360 | if (!urlActivePageFilter.value) return urlResults.value | |
| 361 | return urlResults.value.filter((r) => r.pageId === urlActivePageFilter.value) | |
| 365 | 362 | }) |
| 366 | 363 | |
| 367 | 364 | // Reset URL filter when query changes or mode switches |
| @@ -381,66 +378,9 @@ function setUrlPageFilter(key: string | null) { | ||
| 381 | 378 | nextTick(() => { |
| 382 | 379 | if (resultsEl.value) resultsEl.value.scrollTop = 0 |
| 383 | 380 | scrollToSelectedResult() |
| 384 | // Sync currentTerms from the URL query so reapplyHighlights can highlight + center | |
| 385 | if (filterText.value.trim()) { | |
| 386 | currentTerms.value = new Set( | |
| 387 | filterText.value.trim().toLowerCase().split(/\s+/).filter(Boolean) | |
| 388 | ) | |
| 389 | } | |
| 390 | reapplyHighlights() | |
| 391 | 381 | }) |
| 392 | 382 | } |
| 393 | 383 | |
| 394 | // Reactive list used by the template — same as urlResults but with excerptHtml filled in | |
| 395 | const urlResultsWithExcerpts = shallowRef<UrlResult[]>([]) | |
| 396 | ||
| 397 | watchDebounced( | |
| 398 | () => [urlResults.value, showDetailedList.value, debouncedFilterText.value] as const, | |
| 399 | async ([results, detailed, query], _old, onCleanup) => { | |
| 400 | let canceled = false | |
| 401 | onCleanup(() => { canceled = true }) | |
| 402 | ||
| 403 | // Always sync the list immediately from the already-computed urlResults | |
| 404 | urlResultsWithExcerpts.value = results.map(r => ({ ...r, excerptHtml: '' })) | |
| 405 | if (!detailed || !results.length) return | |
| 406 | ||
| 407 | // Build excerpts once per page (empty query = permanent cache, no re-render per keystroke) | |
| 408 | const uniquePageIds = [...new Set(results.map(r => r.pageId))] | |
| 409 | await Promise.all(uniquePageIds.map(pid => buildDocExcerpt(pid, ''))) | |
| 410 | if (canceled) return | |
| 411 | ||
| 412 | // Fill in excerptHtml from cache keyed by pageId + '' | |
| 413 | urlResultsWithExcerpts.value = results.map(r => { | |
| 414 | const map = cache.get(`${r.pageId}\n`) | |
| 415 | const html = map?.get(r.anchor) | |
| 416 | ?? (r.anchor ? [...(map?.entries() ?? [])].find(([k]) => k.includes(r.anchor))?.[1] : undefined) | |
| 417 | ?? map?.values().next().value | |
| 418 | ?? '' | |
| 419 | return { ...r, excerptHtml: html } | |
| 420 | }) | |
| 421 | if (canceled) return | |
| 422 | ||
| 423 | // Highlight query terms and center excerpts on the highlighted mark (like normal search) | |
| 424 | if (query.trim()) { | |
| 425 | currentTerms.value = new Set( | |
| 426 | query.trim().toLowerCase().split(/\s+/).filter(Boolean) | |
| 427 | ) | |
| 428 | } | |
| 429 | await nextTick() | |
| 430 | if (canceled) return | |
| 431 | await new Promise<void>(resolve => { | |
| 432 | mark.value?.unmark({ | |
| 433 | done: () => { | |
| 434 | mark.value?.markRegExp(formMarkRegex(currentTerms.value), { done: () => resolve() }) | |
| 435 | } | |
| 436 | }) | |
| 437 | }) | |
| 438 | if (canceled) return | |
| 439 | await nextTick() | |
| 440 | centerExcerptsUntilSettled() | |
| 441 | }, | |
| 442 | { debounce: 80, immediate: true } | |
| 443 | ) | |
| 444 | 384 | |
| 445 | 385 | const disableDetailedView = computed(() => { |
| 446 | 386 | return ( |
| @@ -1284,7 +1224,7 @@ function onMouseMove(e: MouseEvent) { | ||
| 1284 | 1224 | type="search" |
| 1285 | 1225 | /> |
| 1286 | 1226 | <div class="search-actions"> |
| 1287 | <div v-if="!disableDetailedView" class="view-group toolbar-group"> | |
| 1227 | <div v-if="!disableDetailedView && searchMode !== 'url'" class="view-group toolbar-group"> | |
| 1288 | 1228 | <button |
| 1289 | 1229 | type="button" |
| 1290 | 1230 | class="mode-btn" |
| @@ -1480,14 +1420,7 @@ function onMouseMove(e: MouseEvent) { | ||
| 1480 | 1420 | <span class="text">{{ item.linkText || item.href }}</span> |
| 1481 | 1421 | </span> |
| 1482 | 1422 | </div> |
| 1483 | <div class="excerpt-wrapper" v-if="showDetailedList"> | |
| 1484 | <div class="excerpt" inert> | |
| 1485 | <div v-if="item.excerptHtml" class="vp-doc" v-html="item.excerptHtml" /> | |
| 1486 | <div v-else class="url-excerpt-href" v-html="item.highlighted" /> | |
| 1487 | </div> | |
| 1488 | <div class="excerpt-gradient-bottom" /> | |
| 1489 | <div class="excerpt-gradient-top" /> | |
| 1490 | </div> | |
| 1423 | ||
| 1491 | 1424 | </div> |
| 1492 | 1425 | <a |
| 1493 | 1426 | :href="item.href" |