Commit 5a7fdfcb19

5a7fdfcb19e4d288ef4bb986fd8817b6dbeec786

parent: b5722d84b1

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-05 14:08 UTC

init

Layout: unified · split

docs/.vitepress/theme/components/Search.vue +118 −80
@@ -907,22 +907,21 @@ function goToNormalPage(p: number) {
907907 if (resultsEl.value) resultsEl.value.scrollTop = 0
908908 scrollToSelectedResult()
909909 reapplyHighlights()
910 void buildVisibleExcerpts()
910911 })
911912}
912913
913// Reset to page 1 when the query changes (results replaced) or filter changes.
914watch([results, activePageFilter], () => {
915 normalPage.value = 1
916})
917914// ---------------------------------------------------------------------------
918915
919916function setPageFilter(key: string | null) {
920917 activePageFilter.value = activePageFilter.value === key ? null : key
918 normalPage.value = 1
921919 selectedIndex.value = filteredResults.value.length ? 1 : -1
922920 nextTick(() => {
923921 if (resultsEl.value) resultsEl.value.scrollTop = 0
924922 scrollToSelectedResult()
925923 reapplyHighlights()
924 void buildVisibleExcerpts()
926925 })
927926}
928927
@@ -944,6 +943,69 @@ async function reapplyHighlights() {
944943 centerExcerptsUntilSettled()
945944}
946945
946const toRows = (list: (SearchResult & Result)[]) =>
947 list.map((r) => {
948 const map = cache.get(getDocId(r.id))
949 return { ...r, text: map?.get(getDocAnchor(r.id)) ?? '' }
950 })
951
952let excerptBuildToken = 0
953
954function visiblePageDocs(pageOffset: number): string[] {
955 const start = (normalCurrentPage.value - 1 + pageOffset) * NORMAL_PAGE_SIZE
956 if (start < 0) return []
957 const list = filteredResults.value
958 const docs: string[] = []
959 const seen = new Set<string>()
960 for (let i = start; i < start + NORMAL_PAGE_SIZE && i < list.length; i++) {
961 const docId = getDocId(String(list[i].id))
962 if (!seen.has(docId)) {
963 seen.add(docId)
964 docs.push(docId)
965 }
966 }
967 return docs
968}
969
970// Builds excerpts for the visible pagination page, then prefetches the next.
971async function buildVisibleExcerpts() {
972 const token = ++excerptBuildToken
973 if (!showDetailedList.value || urlSearchMode.value) return
974
975 let builtAny = false
976 const buildAll = async (docIds: string[]) => {
977 for (const docId of docIds) {
978 if (cache.get(docId)) continue
979 await buildDocExcerpt(docId)
980 if (token !== excerptBuildToken) return false
981 builtAny = true
982 await nextFrame()
983 if (token !== excerptBuildToken) return false
984 }
985 return true
986 }
987
988 if (!(await buildAll(visiblePageDocs(0)))) return
989
990 const rowsStale = pagedResults.value.some((r) => {
991 if (r.text) return false
992 const map = cache.get(getDocId(String(r.id)))
993 return Boolean(map?.get(getDocAnchor(String(r.id))))
994 })
995
996 if (builtAny || rowsStale) {
997 const keepSelected = selectedIndex.value
998 results.value = toRows(results.value)
999 await nextTick()
1000 if (token !== excerptBuildToken) return
1001 selectedIndex.value = keepSelected
1002 await reapplyHighlights()
1003 if (token !== excerptBuildToken) return
1004 }
1005
1006 await buildAll(visiblePageDocs(1))
1007}
1008
9471009watch(results, () => {
9481010 if (
9491011 activePageFilter.value &&
@@ -1079,20 +1141,14 @@ const cache = new LRUCache<string, Map<string, string>>(120)
10791141
10801142const inFlightExcerpts = new Map<string, Promise<void>>()
10811143
1082const EAGER_EXCERPTS = 15
1083
1084function buildDocExcerpt(
1085 docId: string,
1086 filterTextValue: string
1087): Promise<void> {
1088 const cacheId = `${docId}\n${filterTextValue}`
1089 if (cache.get(cacheId)) return Promise.resolve()
1090 const pending = inFlightExcerpts.get(cacheId)
1144function buildDocExcerpt(docId: string): Promise<void> {
1145 if (cache.get(docId)) return Promise.resolve()
1146 const pending = inFlightExcerpts.get(docId)
10911147 if (pending) return pending
10921148
10931149 const job = (async () => {
10941150 const { mod } = await fetchExcerpt(docId)
1095 if (cache.get(cacheId)) return
1151 if (cache.get(docId)) return
10961152
10971153 const comp = (mod as any).default ?? mod
10981154 if (!(comp?.render || comp?.setup)) return // nothing renderable; don't cache
@@ -1116,7 +1172,24 @@ function buildDocExcerpt(
11161172 })
11171173 const div = document.createElement('div')
11181174 try {
1119 app.mount(div)
1175 // force lazy images so the detached render doesn't download them
1176 const originalCreateElement = document.createElement
1177 document.createElement = function (
1178 this: Document,
1179 ...args: Parameters<Document['createElement']>
1180 ) {
1181 const el = originalCreateElement.apply(this, args)
1182 if (el instanceof HTMLImageElement) {
1183 el.loading = 'lazy'
1184 el.decoding = 'async'
1185 }
1186 return el
1187 } as typeof document.createElement
1188 try {
1189 app.mount(div)
1190 } finally {
1191 document.createElement = originalCreateElement
1192 }
11201193 const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6')
11211194 headings.forEach((heading) => {
11221195 const href = heading.querySelector('a')?.getAttribute('href')
@@ -1141,12 +1214,12 @@ function buildDocExcerpt(
11411214 /* ignore */
11421215 }
11431216 }
1144 cache.set(cacheId, map)
1217 cache.set(docId, map)
11451218 })()
11461219
1147 inFlightExcerpts.set(cacheId, job)
1220 inFlightExcerpts.set(docId, job)
11481221 job.finally(() => {
1149 if (inFlightExcerpts.get(cacheId) === job) inFlightExcerpts.delete(cacheId)
1222 if (inFlightExcerpts.get(docId) === job) inFlightExcerpts.delete(docId)
11501223 })
11511224 return job
11521225}
@@ -1192,6 +1265,8 @@ watchDebounced(
11921265 canceled = true
11931266 })
11941267
1268 excerptBuildToken++
1269
11951270 if (!ready || !isOpen) {
11961271 textSearchLoading.value = false
11971272 return
@@ -1264,14 +1339,6 @@ watchDebounced(
12641339 return
12651340 }
12661341
1267 const toRows = (list: (SearchResult & Result)[]) =>
1268 list.map((r) => {
1269 const id = getDocId(r.id)
1270 const anchor = getDocAnchor(r.id)
1271 const map = cache.get(`${id}\n${searchQuery}`)
1272 return { ...r, text: map?.get(anchor) ?? '' }
1273 })
1274
12751342 const highlight = () =>
12761343 new Promise<void>((resolve) => {
12771344 if (!terms.size || !mark.value) {
@@ -1287,31 +1354,9 @@ watchDebounced(
12871354 })
12881355 })
12891356
1290 const docOrderList: string[] = []
1291 const seenDocs = new Set<string>()
1292 for (const r of _result) {
1293 const docId = getDocId(r.id)
1294 if (!seenDocs.has(docId)) {
1295 seenDocs.add(docId)
1296 docOrderList.push(docId)
1297 }
1298 }
1299
1300 // Show results immediately (excerpts may be empty on first detail-view switch;
1301 // they fill in below once built)
1302 results.value = toRows(_result)
1303
1304 if (showDetailedListValue) {
1305 const eagerDocs = new Set<string>()
1306 for (const r of _result.slice(0, EAGER_EXCERPTS)) {
1307 eagerDocs.add(getDocId(r.id))
1308 }
1309 await Promise.all(
1310 [...eagerDocs].map((docId) => buildDocExcerpt(docId, searchQuery))
1311 )
1312 if (canceled) return
1313 }
1357 normalPage.value = 1
13141358
1359 // Show results immediately; excerpts fill in below once built
13151360 results.value = toRows(_result)
13161361
13171362 await nextTick()
@@ -1327,27 +1372,8 @@ watchDebounced(
13271372 centerExcerptsUntilSettled()
13281373
13291374 if (showDetailedListValue) {
1330 const built = new Set<string>()
1331 for (const r of _result.slice(0, EAGER_EXCERPTS)) {
1332 built.add(getDocId(r.id))
1333 }
1334 const remaining = docOrderList.filter((d) => !built.has(d))
1335 if (remaining.length) {
1336 for (const docId of remaining) {
1337 await buildDocExcerpt(docId, searchQuery)
1338 if (canceled) return
1339 await nextFrame()
1340 if (canceled) return
1341 }
1342 results.value = toRows(_result)
1343 await nextTick()
1344 if (canceled) return
1345 await highlight()
1346 await nextTick()
1347 await nextFrame()
1348 if (canceled) return
1349 centerExcerptsUntilSettled()
1350 }
1375 await buildVisibleExcerpts()
1376 if (canceled) return
13511377 }
13521378 textSearchLoading.value = false
13531379 },
@@ -1364,8 +1390,7 @@ function centerExcerpts() {
13641390 ) as HTMLElement | null
13651391 if (!markNode) continue
13661392
1367 const viewportHeight =
1368 Number.parseFloat(getComputedStyle(excerptElement).maxHeight) || 84
1393 const viewportHeight = excerptElement.clientHeight || 84
13691394
13701395 let offset = 0
13711396 let node: HTMLElement | null = markNode
@@ -1439,13 +1464,25 @@ const nextFrame = () =>
14391464
14401465function hasExcerptPreview(html?: string) {
14411466 if (!html) return false
1442 return Boolean(
1443 html
1444 .replace(/<[^>]*>/g, ' ')
1445 .replace(/&nbsp;|&#160;/gi, ' ')
1446 .replace(/\s+/g, ' ')
1447 .trim()
1448 )
1467 for (let i = 0; i < html.length; i++) {
1468 const ch = html[i]
1469 if (ch === '<') {
1470 const close = html.indexOf('>', i + 1)
1471 if (close === -1) return false
1472 i = close
1473 continue
1474 }
1475 if (ch === '&') {
1476 const entity = html.slice(i, i + 6).toLowerCase()
1477 if (entity === '&nbsp;' || entity === '&#160;') {
1478 i += 5
1479 continue
1480 }
1481 return true
1482 }
1483 if (!/\s/.test(ch)) return true
1484 }
1485 return false
14491486}
14501487
14511488/* Search input focus */
@@ -3113,6 +3150,7 @@ function onMouseMove(e: MouseEvent) {
31133150 overflow: hidden;
31143151 position: relative;
31153152 margin-top: 4px;
3153 contain: layout paint;
31163154}
31173155
31183156.result.selected .excerpt {