Commit a2386dba90

a2386dba906ab3e0b65572ec07530b387e78cbfb

parent: 437a2b5a6a

Unsigned

taskylizard <75871323+taskylizard@users.noreply.github.com> · 2026-06-05 19:40 UTC

some hotpath improvements

Layout: unified · split

docs/.vitepress/plugins/urlSearchPlugin.ts +25 −17
@@ -137,21 +137,35 @@ function extractLinksFromMarkdown(src: string, pageId: string): PageLink[] {
137 return results 137 return results
138} 138}
139 139
140const collected: PageLink[] = [] 140const collectedByPage = new Map<string, PageLink[]>()
141
142function getPageId(page: string) {
143 return '/' + page.replace(/\.md$/, '').replace(/\/index$/, '/')
144}
145
146function getDedupedLinks() {
147 const seen = new Set<string>()
148 const deduped: PageLink[] = []
149
150 for (const links of collectedByPage.values()) {
151 for (const link of links) {
152 const key = link.href + '\x00' + link.pageId + '\x00' + link.anchor
153 if (seen.has(key)) continue
154 seen.add(key)
155 deduped.push(link)
156 }
157 }
158
159 return deduped
160}
141 161
142export function collectPageLinks(src: string, page: string) { 162export function collectPageLinks(src: string, page: string) {
143 const pageId = '/' + page.replace(/\.md$/, '').replace(/\/index$/, '/') 163 const pageId = getPageId(page)
144 collected.push(...extractLinksFromMarkdown(src, pageId)) 164 collectedByPage.set(pageId, extractLinksFromMarkdown(src, pageId))
145} 165}
146 166
147export function writeUrlSearchIndex(outDir: string) { 167export function writeUrlSearchIndex(outDir: string) {
148 const seen = new Set<string>() 168 const deduped = getDedupedLinks()
149 const deduped = collected.filter((l) => {
150 const key = l.href + '\x00' + l.pageId + '\x00' + l.anchor
151 if (seen.has(key)) return false
152 seen.add(key)
153 return true
154 })
155 const outPath = join(outDir, 'url-search-index.json') 169 const outPath = join(outDir, 'url-search-index.json')
156 writeFileSync(outPath, JSON.stringify(deduped), 'utf-8') 170 writeFileSync(outPath, JSON.stringify(deduped), 'utf-8')
157 console.log(`[url-search] wrote ${deduped.length} links → ${outPath}`) 171 console.log(`[url-search] wrote ${deduped.length} links → ${outPath}`)
@@ -164,13 +178,7 @@ export function urlSearchDevPlugin() {
164 server.middlewares.use( 178 server.middlewares.use(
165 '/url-search-index.json', 179 '/url-search-index.json',
166 (_req: any, res: any) => { 180 (_req: any, res: any) => {
167 const seen = new Set<string>() 181 const deduped = getDedupedLinks()
168 const deduped = collected.filter((l) => {
169 const key = l.href + '\x00' + l.pageId + '\x00' + l.anchor
170 if (seen.has(key)) return false
171 seen.add(key)
172 return true
173 })
174 res.setHeader('Content-Type', 'application/json') 182 res.setHeader('Content-Type', 'application/json')
175 res.end(JSON.stringify(deduped)) 183 res.end(JSON.stringify(deduped))
176 } 184 }
docs/.vitepress/theme/components/Search.vue +39 −18
@@ -654,16 +654,17 @@ watch(
654 () => 654 () =>
655 [ 655 [
656 searchWorkerReady.value, 656 searchWorkerReady.value,
657 showSearch.value,
657 urlSearchMode.value, 658 urlSearchMode.value,
658 urlFilterDebounced.value 659 urlFilterDebounced.value
659 ] as const, 660 ] as const,
660 async ([ready, active, query], _old, onCleanup) => { 661 async ([ready, isOpen, active, query], _old, onCleanup) => {
661 let canceled = false 662 let canceled = false
662 onCleanup(() => { 663 onCleanup(() => {
663 canceled = true 664 canceled = true
664 }) 665 })
665 666
666 if (!active || !query.trim()) { 667 if (!active || !isOpen || !query.trim()) {
667 urlMatches.value = [] 668 urlMatches.value = []
668 urlPageGroupCounts.value = [] 669 urlPageGroupCounts.value = []
669 return 670 return
@@ -691,7 +692,17 @@ watch(
691) 692)
692 693
693function getPageKey(id: string) { 694function getPageKey(id: string) {
694 return id.split('#')[0].replace(/\/$/, '') 695 return getDocId(id).replace(/\/$/, '')
696}
697
698function getDocId(id: string) {
699 const hashIndex = id.indexOf('#')
700 return hashIndex === -1 ? id : id.slice(0, hashIndex)
701}
702
703function getDocAnchor(id: string) {
704 const hashIndex = id.indexOf('#')
705 return hashIndex === -1 ? '' : id.slice(hashIndex + 1)
695} 706}
696 707
697function getPageLabel(key: string) { 708function getPageLabel(key: string) {
@@ -1014,6 +1025,7 @@ watchDebounced(
1014 () => 1025 () =>
1015 [ 1026 [
1016 searchWorkerReady.value, 1027 searchWorkerReady.value,
1028 showSearch.value,
1017 localeIndex.value, 1029 localeIndex.value,
1018 searchIndexVersion.value, 1030 searchIndexVersion.value,
1019 searchWorkerConfigKey.value, 1031 searchWorkerConfigKey.value,
@@ -1024,6 +1036,7 @@ watchDebounced(
1024 async ( 1036 async (
1025 [ 1037 [
1026 ready, 1038 ready,
1039 isOpen,
1027 localeIndexValue, 1040 localeIndexValue,
1028 indexVersion, 1041 indexVersion,
1029 configKey, 1042 configKey,
@@ -1035,9 +1048,9 @@ watchDebounced(
1035 onCleanup 1048 onCleanup
1036 ) => { 1049 ) => {
1037 if ( 1050 if (
1038 old?.[1] !== localeIndexValue || 1051 old?.[2] !== localeIndexValue ||
1039 old?.[2] !== indexVersion || 1052 old?.[3] !== indexVersion ||
1040 old?.[3] !== configKey 1053 old?.[4] !== configKey
1041 ) { 1054 ) {
1042 // Locale/config changes rebuild the worker index, so cached excerpts may no 1055 // Locale/config changes rebuild the worker index, so cached excerpts may no
1043 // longer match the active result set. 1056 // longer match the active result set.
@@ -1049,13 +1062,21 @@ watchDebounced(
1049 canceled = true 1062 canceled = true
1050 }) 1063 })
1051 1064
1052 if (!ready) return 1065 if (!ready || !isOpen) return
1053 1066
1054 // In URL search mode, skip normal text search 1067 // In URL search mode, skip normal text search
1055 if (mode === 'url') { 1068 if (mode === 'url') {
1056 results.value = [] 1069 results.value = []
1057 currentTerms.value = new Set() 1070 currentTerms.value = new Set()
1058 enableNoResults.value = true 1071 enableNoResults.value = Boolean(filterTextValue.trim())
1072 return
1073 }
1074
1075 const searchQuery = filterTextValue.trim()
1076 if (!searchQuery) {
1077 results.value = []
1078 currentTerms.value = new Set()
1079 enableNoResults.value = false
1059 return 1080 return
1060 } 1081 }
1061 1082
@@ -1080,7 +1101,7 @@ watchDebounced(
1080 1101
1081 workerPayload = await postSearchWorker<TextSearchWorkerPayload>({ 1102 workerPayload = await postSearchWorker<TextSearchWorkerPayload>({
1082 type: 'text-search', 1103 type: 'text-search',
1083 query: filterTextValue, 1104 query: searchQuery,
1084 mode, 1105 mode,
1085 pageOrderEntries: getPageOrderEntries() 1106 pageOrderEntries: getPageOrderEntries()
1086 }) 1107 })
@@ -1107,8 +1128,9 @@ watchDebounced(
1107 1128
1108 const toRows = (list: (SearchResult & Result)[]) => 1129 const toRows = (list: (SearchResult & Result)[]) =>
1109 list.map((r) => { 1130 list.map((r) => {
1110 const [id, anchor] = r.id.split('#') 1131 const id = getDocId(r.id)
1111 const map = cache.get(`${id}\n${filterTextValue}`) 1132 const anchor = getDocAnchor(r.id)
1133 const map = cache.get(`${id}\n${searchQuery}`)
1112 return { ...r, text: map?.get(anchor) ?? '' } 1134 return { ...r, text: map?.get(anchor) ?? '' }
1113 }) 1135 })
1114 1136
@@ -1130,7 +1152,7 @@ watchDebounced(
1130 const docOrderList: string[] = [] 1152 const docOrderList: string[] = []
1131 const seenDocs = new Set<string>() 1153 const seenDocs = new Set<string>()
1132 for (const r of _result) { 1154 for (const r of _result) {
1133 const docId = r.id.slice(0, r.id.indexOf('#')) 1155 const docId = getDocId(r.id)
1134 if (!seenDocs.has(docId)) { 1156 if (!seenDocs.has(docId)) {
1135 seenDocs.add(docId) 1157 seenDocs.add(docId)
1136 docOrderList.push(docId) 1158 docOrderList.push(docId)
@@ -1144,10 +1166,10 @@ watchDebounced(
1144 if (showDetailedListValue) { 1166 if (showDetailedListValue) {
1145 const eagerDocs = new Set<string>() 1167 const eagerDocs = new Set<string>()
1146 for (const r of _result.slice(0, EAGER_EXCERPTS)) { 1168 for (const r of _result.slice(0, EAGER_EXCERPTS)) {
1147 eagerDocs.add(r.id.slice(0, r.id.indexOf('#'))) 1169 eagerDocs.add(getDocId(r.id))
1148 } 1170 }
1149 await Promise.all( 1171 await Promise.all(
1150 [...eagerDocs].map((docId) => buildDocExcerpt(docId, filterTextValue)) 1172 [...eagerDocs].map((docId) => buildDocExcerpt(docId, searchQuery))
1151 ) 1173 )
1152 if (canceled) return 1174 if (canceled) return
1153 } 1175 }
@@ -1169,12 +1191,12 @@ watchDebounced(
1169 if (showDetailedListValue) { 1191 if (showDetailedListValue) {
1170 const built = new Set<string>() 1192 const built = new Set<string>()
1171 for (const r of _result.slice(0, EAGER_EXCERPTS)) { 1193 for (const r of _result.slice(0, EAGER_EXCERPTS)) {
1172 built.add(r.id.slice(0, r.id.indexOf('#'))) 1194 built.add(getDocId(r.id))
1173 } 1195 }
1174 const remaining = docOrderList.filter((d) => !built.has(d)) 1196 const remaining = docOrderList.filter((d) => !built.has(d))
1175 if (remaining.length) { 1197 if (remaining.length) {
1176 for (const docId of remaining) { 1198 for (const docId of remaining) {
1177 await buildDocExcerpt(docId, filterTextValue) 1199 await buildDocExcerpt(docId, searchQuery)
1178 if (canceled) return 1200 if (canceled) return
1179 await nextFrame() 1201 await nextFrame()
1180 if (canceled) return 1202 if (canceled) return
@@ -1254,8 +1276,7 @@ onBeforeUnmount(() => {
1254const excerptCache = new LRUCache<string, unknown>(24) 1276const excerptCache = new LRUCache<string, unknown>(24)
1255 1277
1256async function fetchExcerpt(id: string) { 1278async function fetchExcerpt(id: string) {
1257 const hashIndex = id.indexOf('#') 1279 const docId = getDocId(id)
1258 const docId = hashIndex === -1 ? id : id.slice(0, hashIndex)
1259 1280
1260 const cached = excerptCache.get(docId) 1281 const cached = excerptCache.get(docId)
1261 if (cached) return { id, mod: cached } 1282 if (cached) return { id, mod: cached }
docs/.vitepress/theme/workers/searchWorker.ts +7 −2
@@ -61,6 +61,7 @@ type WorkerSearchResult = SearchResult & Result
61let searchIndex: MiniSearch<Result> | null = null 61let searchIndex: MiniSearch<Result> | null = null
62let searchIndexKey = '' 62let searchIndexKey = ''
63let urlLinks: PageLink[] = [] 63let urlLinks: PageLink[] = []
64let urlLinkHrefsLower: string[] = []
64let urlLinksPromise: Promise<void> | null = null 65let urlLinksPromise: Promise<void> | null = null
65let pageOrder = new Map<string, number>() 66let pageOrder = new Map<string, number>()
66let docOrder = new Map<string, number>() 67let docOrder = new Map<string, number>()
@@ -212,9 +213,11 @@ async function ensureUrlLinks() {
212 .then((data: PageLink[]) => { 213 .then((data: PageLink[]) => {
213 data.sort((a, b) => getPageOrder(a.pageId) - getPageOrder(b.pageId)) 214 data.sort((a, b) => getPageOrder(a.pageId) - getPageOrder(b.pageId))
214 urlLinks = data 215 urlLinks = data
216 urlLinkHrefsLower = data.map((link) => link.href.toLowerCase())
215 }) 217 })
216 .catch(() => { 218 .catch(() => {
217 urlLinks = [] 219 urlLinks = []
220 urlLinkHrefsLower = []
218 }) 221 })
219 .finally(() => { 222 .finally(() => {
220 urlLinksPromise = null 223 urlLinksPromise = null
@@ -231,8 +234,10 @@ async function urlSearch(message: UrlSearchRequest) {
231 234
232 const matches: PageLink[] = [] 235 const matches: PageLink[] = []
233 const pageGroups = new Map<string, PageGroupCount>() 236 const pageGroups = new Map<string, PageGroupCount>()
234 for (const link of urlLinks) { 237 for (let i = 0; i < urlLinks.length; i++) {
235 if (!link.href.toLowerCase().includes(query)) continue 238 if (!urlLinkHrefsLower[i].includes(query)) continue
239
240 const link = urlLinks[i]
236 241
237 matches.push(link) 242 matches.push(link)
238 const existing = pageGroups.get(link.pageId) 243 const existing = pageGroups.get(link.pageId)