Commit e6c47a758b
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Search.vue +5 −72
| @@ -326,7 +326,6 @@ interface UrlResult { | |||
| 326 | anchor: string | 326 | anchor: string |
| 327 | titles: string[] | 327 | titles: string[] |
| 328 | highlighted: string | 328 | highlighted: string |
| 329 | excerptHtml: string | ||
| 330 | } | 329 | } |
| 331 | 330 | ||
| 332 | const urlResults = computed((): UrlResult[] => { | 331 | const urlResults = computed((): UrlResult[] => { |
| @@ -339,8 +338,7 @@ const urlResults = computed((): UrlResult[] => { | |||
| 339 | .filter(({ href }) => href.toLowerCase().includes(query)) | 338 | .filter(({ href }) => href.toLowerCase().includes(query)) |
| 340 | .map(({ href, linkText, pageId, anchor, titles }) => ({ | 339 | .map(({ href, linkText, pageId, anchor, titles }) => ({ |
| 341 | href, linkText, pageId, anchor, titles, | 340 | href, linkText, pageId, anchor, titles, |
| 342 | highlighted: href.replace(regex, '<mark class="url-highlight">$1</mark>'), | 341 | highlighted: href.replace(regex, '<mark class="url-highlight">$1</mark>') |
| 343 | excerptHtml: '' | ||
| 344 | })) | 342 | })) |
| 345 | }) | 343 | }) |
| 346 | 344 | ||
| @@ -359,9 +357,8 @@ const urlPageGroups = computed(() => { | |||
| 359 | }) | 357 | }) |
| 360 | 358 | ||
| 361 | const filteredUrlResults = computed(() => { | 359 | const filteredUrlResults = computed(() => { |
| 362 | const src = urlResultsWithExcerpts.value | 360 | if (!urlActivePageFilter.value) return urlResults.value |
| 363 | if (!urlActivePageFilter.value) return src | 361 | return urlResults.value.filter((r) => r.pageId === urlActivePageFilter.value) |
| 364 | return src.filter((r) => r.pageId === urlActivePageFilter.value) | ||
| 365 | }) | 362 | }) |
| 366 | 363 | ||
| 367 | // Reset URL filter when query changes or mode switches | 364 | // Reset URL filter when query changes or mode switches |
| @@ -381,66 +378,9 @@ function setUrlPageFilter(key: string | null) { | |||
| 381 | nextTick(() => { | 378 | nextTick(() => { |
| 382 | if (resultsEl.value) resultsEl.value.scrollTop = 0 | 379 | if (resultsEl.value) resultsEl.value.scrollTop = 0 |
| 383 | scrollToSelectedResult() | 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 | const disableDetailedView = computed(() => { | 385 | const disableDetailedView = computed(() => { |
| 446 | return ( | 386 | return ( |
| @@ -1284,7 +1224,7 @@ function onMouseMove(e: MouseEvent) { | |||
| 1284 | type="search" | 1224 | type="search" |
| 1285 | /> | 1225 | /> |
| 1286 | <div class="search-actions"> | 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 | <button | 1228 | <button |
| 1289 | type="button" | 1229 | type="button" |
| 1290 | class="mode-btn" | 1230 | class="mode-btn" |
| @@ -1480,14 +1420,7 @@ function onMouseMove(e: MouseEvent) { | |||
| 1480 | <span class="text">{{ item.linkText || item.href }}</span> | 1420 | <span class="text">{{ item.linkText || item.href }}</span> |
| 1481 | </span> | 1421 | </span> |
| 1482 | </div> | 1422 | </div> |
| 1483 | <div class="excerpt-wrapper" v-if="showDetailedList"> | 1423 | |
| 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> | ||
| 1491 | </div> | 1424 | </div> |
| 1492 | <a | 1425 | <a |
| 1493 | :href="item.href" | 1426 | :href="item.href" |