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) {
907 if (resultsEl.value) resultsEl.value.scrollTop = 0 907 if (resultsEl.value) resultsEl.value.scrollTop = 0
908 scrollToSelectedResult() 908 scrollToSelectedResult()
909 reapplyHighlights() 909 reapplyHighlights()
910 void buildVisibleExcerpts()
910 }) 911 })
911} 912}
912 913
913// Reset to page 1 when the query changes (results replaced) or filter changes.
914watch([results, activePageFilter], () => {
915 normalPage.value = 1
916})
917// --------------------------------------------------------------------------- 914// ---------------------------------------------------------------------------
918 915
919function setPageFilter(key: string | null) { 916function setPageFilter(key: string | null) {
920 activePageFilter.value = activePageFilter.value === key ? null : key 917 activePageFilter.value = activePageFilter.value === key ? null : key
918 normalPage.value = 1
921 selectedIndex.value = filteredResults.value.length ? 1 : -1 919 selectedIndex.value = filteredResults.value.length ? 1 : -1
922 nextTick(() => { 920 nextTick(() => {
923 if (resultsEl.value) resultsEl.value.scrollTop = 0 921 if (resultsEl.value) resultsEl.value.scrollTop = 0
924 scrollToSelectedResult() 922 scrollToSelectedResult()
925 reapplyHighlights() 923 reapplyHighlights()
924 void buildVisibleExcerpts()
926 }) 925 })
927} 926}
928 927
@@ -944,6 +943,69 @@ async function reapplyHighlights() {
944 centerExcerptsUntilSettled() 943 centerExcerptsUntilSettled()
945} 944}
946 945
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
947watch(results, () => { 1009watch(results, () => {
948 if ( 1010 if (
949 activePageFilter.value && 1011 activePageFilter.value &&
@@ -1079,20 +1141,14 @@ const cache = new LRUCache<string, Map<string, string>>(120)
1079 1141
1080const inFlightExcerpts = new Map<string, Promise<void>>() 1142const inFlightExcerpts = new Map<string, Promise<void>>()
1081 1143
1082const EAGER_EXCERPTS = 15 1144function buildDocExcerpt(docId: string): Promise<void> {
1083 1145 if (cache.get(docId)) return Promise.resolve()
1084function buildDocExcerpt( 1146 const pending = inFlightExcerpts.get(docId)
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)
1091 if (pending) return pending 1147 if (pending) return pending
1092 1148
1093 const job = (async () => { 1149 const job = (async () => {
1094 const { mod } = await fetchExcerpt(docId) 1150 const { mod } = await fetchExcerpt(docId)
1095 if (cache.get(cacheId)) return 1151 if (cache.get(docId)) return
1096 1152
1097 const comp = (mod as any).default ?? mod 1153 const comp = (mod as any).default ?? mod
1098 if (!(comp?.render || comp?.setup)) return // nothing renderable; don't cache 1154 if (!(comp?.render || comp?.setup)) return // nothing renderable; don't cache
@@ -1116,7 +1172,24 @@ function buildDocExcerpt(
1116 }) 1172 })
1117 const div = document.createElement('div') 1173 const div = document.createElement('div')
1118 try { 1174 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 }
1120 const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6') 1193 const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6')
1121 headings.forEach((heading) => { 1194 headings.forEach((heading) => {
1122 const href = heading.querySelector('a')?.getAttribute('href') 1195 const href = heading.querySelector('a')?.getAttribute('href')
@@ -1141,12 +1214,12 @@ function buildDocExcerpt(
1141 /* ignore */ 1214 /* ignore */
1142 } 1215 }
1143 } 1216 }
1144 cache.set(cacheId, map) 1217 cache.set(docId, map)
1145 })() 1218 })()
1146 1219
1147 inFlightExcerpts.set(cacheId, job) 1220 inFlightExcerpts.set(docId, job)
1148 job.finally(() => { 1221 job.finally(() => {
1149 if (inFlightExcerpts.get(cacheId) === job) inFlightExcerpts.delete(cacheId) 1222 if (inFlightExcerpts.get(docId) === job) inFlightExcerpts.delete(docId)
1150 }) 1223 })
1151 return job 1224 return job
1152} 1225}
@@ -1192,6 +1265,8 @@ watchDebounced(
1192 canceled = true 1265 canceled = true
1193 }) 1266 })
1194 1267
1268 excerptBuildToken++
1269
1195 if (!ready || !isOpen) { 1270 if (!ready || !isOpen) {
1196 textSearchLoading.value = false 1271 textSearchLoading.value = false
1197 return 1272 return
@@ -1264,14 +1339,6 @@ watchDebounced(
1264 return 1339 return
1265 } 1340 }
1266 1341
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
1275 const highlight = () => 1342 const highlight = () =>
1276 new Promise<void>((resolve) => { 1343 new Promise<void>((resolve) => {
1277 if (!terms.size || !mark.value) { 1344 if (!terms.size || !mark.value) {
@@ -1287,31 +1354,9 @@ watchDebounced(
1287 }) 1354 })
1288 }) 1355 })
1289 1356
1290 const docOrderList: string[] = [] 1357 normalPage.value = 1
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 }
1314 1358
1359 // Show results immediately; excerpts fill in below once built
1315 results.value = toRows(_result) 1360 results.value = toRows(_result)
1316 1361
1317 await nextTick() 1362 await nextTick()
@@ -1327,27 +1372,8 @@ watchDebounced(
1327 centerExcerptsUntilSettled() 1372 centerExcerptsUntilSettled()
1328 1373
1329 if (showDetailedListValue) { 1374 if (showDetailedListValue) {
1330 const built = new Set<string>() 1375 await buildVisibleExcerpts()
1331 for (const r of _result.slice(0, EAGER_EXCERPTS)) { 1376 if (canceled) return
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 }
1351 } 1377 }
1352 textSearchLoading.value = false 1378 textSearchLoading.value = false
1353 }, 1379 },
@@ -1364,8 +1390,7 @@ function centerExcerpts() {
1364 ) as HTMLElement | null 1390 ) as HTMLElement | null
1365 if (!markNode) continue 1391 if (!markNode) continue
1366 1392
1367 const viewportHeight = 1393 const viewportHeight = excerptElement.clientHeight || 84
1368 Number.parseFloat(getComputedStyle(excerptElement).maxHeight) || 84
1369 1394
1370 let offset = 0 1395 let offset = 0
1371 let node: HTMLElement | null = markNode 1396 let node: HTMLElement | null = markNode
@@ -1439,13 +1464,25 @@ const nextFrame = () =>
1439 1464
1440function hasExcerptPreview(html?: string) { 1465function hasExcerptPreview(html?: string) {
1441 if (!html) return false 1466 if (!html) return false
1442 return Boolean( 1467 for (let i = 0; i < html.length; i++) {
1443 html 1468 const ch = html[i]
1444 .replace(/<[^>]*>/g, ' ') 1469 if (ch === '<') {
1445 .replace(/&nbsp;|&#160;/gi, ' ') 1470 const close = html.indexOf('>', i + 1)
1446 .replace(/\s+/g, ' ') 1471 if (close === -1) return false
1447 .trim() 1472 i = close
1448 ) 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
1449} 1486}
1450 1487
1451/* Search input focus */ 1488/* Search input focus */
@@ -3113,6 +3150,7 @@ function onMouseMove(e: MouseEvent) {
3113 overflow: hidden; 3150 overflow: hidden;
3114 position: relative; 3151 position: relative;
3115 margin-top: 4px; 3152 margin-top: 4px;
3153 contain: layout paint;
3116} 3154}
3117 3155
3118.result.selected .excerpt { 3156.result.selected .excerpt {