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[] {
137137 return results
138138}
139139
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}
141161
142162export function collectPageLinks(src: string, page: string) {
143 const pageId = '/' + page.replace(/\.md$/, '').replace(/\/index$/, '/')
144 collected.push(...extractLinksFromMarkdown(src, pageId))
163 const pageId = getPageId(page)
164 collectedByPage.set(pageId, extractLinksFromMarkdown(src, pageId))
145165}
146166
147167export function writeUrlSearchIndex(outDir: string) {
148 const seen = new Set<string>()
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 })
168 const deduped = getDedupedLinks()
155169 const outPath = join(outDir, 'url-search-index.json')
156170 writeFileSync(outPath, JSON.stringify(deduped), 'utf-8')
157171 console.log(`[url-search] wrote ${deduped.length} links → ${outPath}`)
@@ -164,13 +178,7 @@ export function urlSearchDevPlugin() {
164178 server.middlewares.use(
165179 '/url-search-index.json',
166180 (_req: any, res: any) => {
167 const seen = new Set<string>()
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 })
181 const deduped = getDedupedLinks()
174182 res.setHeader('Content-Type', 'application/json')
175183 res.end(JSON.stringify(deduped))
176184 }
docs/.vitepress/theme/components/Search.vue +39 −18
@@ -654,16 +654,17 @@ watch(
654654 () =>
655655 [
656656 searchWorkerReady.value,
657 showSearch.value,
657658 urlSearchMode.value,
658659 urlFilterDebounced.value
659660 ] as const,
660 async ([ready, active, query], _old, onCleanup) => {
661 async ([ready, isOpen, active, query], _old, onCleanup) => {
661662 let canceled = false
662663 onCleanup(() => {
663664 canceled = true
664665 })
665666
666 if (!active || !query.trim()) {
667 if (!active || !isOpen || !query.trim()) {
667668 urlMatches.value = []
668669 urlPageGroupCounts.value = []
669670 return
@@ -691,7 +692,17 @@ watch(
691692)
692693
693694function 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)
695706}
696707
697708function getPageLabel(key: string) {
@@ -1014,6 +1025,7 @@ watchDebounced(
10141025 () =>
10151026 [
10161027 searchWorkerReady.value,
1028 showSearch.value,
10171029 localeIndex.value,
10181030 searchIndexVersion.value,
10191031 searchWorkerConfigKey.value,
@@ -1024,6 +1036,7 @@ watchDebounced(
10241036 async (
10251037 [
10261038 ready,
1039 isOpen,
10271040 localeIndexValue,
10281041 indexVersion,
10291042 configKey,
@@ -1035,9 +1048,9 @@ watchDebounced(
10351048 onCleanup
10361049 ) => {
10371050 if (
1038 old?.[1] !== localeIndexValue ||
1039 old?.[2] !== indexVersion ||
1040 old?.[3] !== configKey
1051 old?.[2] !== localeIndexValue ||
1052 old?.[3] !== indexVersion ||
1053 old?.[4] !== configKey
10411054 ) {
10421055 // Locale/config changes rebuild the worker index, so cached excerpts may no
10431056 // longer match the active result set.
@@ -1049,13 +1062,21 @@ watchDebounced(
10491062 canceled = true
10501063 })
10511064
1052 if (!ready) return
1065 if (!ready || !isOpen) return
10531066
10541067 // In URL search mode, skip normal text search
10551068 if (mode === 'url') {
10561069 results.value = []
10571070 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
10591080 return
10601081 }
10611082
@@ -1080,7 +1101,7 @@ watchDebounced(
10801101
10811102 workerPayload = await postSearchWorker<TextSearchWorkerPayload>({
10821103 type: 'text-search',
1083 query: filterTextValue,
1104 query: searchQuery,
10841105 mode,
10851106 pageOrderEntries: getPageOrderEntries()
10861107 })
@@ -1107,8 +1128,9 @@ watchDebounced(
11071128
11081129 const toRows = (list: (SearchResult & Result)[]) =>
11091130 list.map((r) => {
1110 const [id, anchor] = r.id.split('#')
1111 const map = cache.get(`${id}\n${filterTextValue}`)
1131 const id = getDocId(r.id)
1132 const anchor = getDocAnchor(r.id)
1133 const map = cache.get(`${id}\n${searchQuery}`)
11121134 return { ...r, text: map?.get(anchor) ?? '' }
11131135 })
11141136
@@ -1130,7 +1152,7 @@ watchDebounced(
11301152 const docOrderList: string[] = []
11311153 const seenDocs = new Set<string>()
11321154 for (const r of _result) {
1133 const docId = r.id.slice(0, r.id.indexOf('#'))
1155 const docId = getDocId(r.id)
11341156 if (!seenDocs.has(docId)) {
11351157 seenDocs.add(docId)
11361158 docOrderList.push(docId)
@@ -1144,10 +1166,10 @@ watchDebounced(
11441166 if (showDetailedListValue) {
11451167 const eagerDocs = new Set<string>()
11461168 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))
11481170 }
11491171 await Promise.all(
1150 [...eagerDocs].map((docId) => buildDocExcerpt(docId, filterTextValue))
1172 [...eagerDocs].map((docId) => buildDocExcerpt(docId, searchQuery))
11511173 )
11521174 if (canceled) return
11531175 }
@@ -1169,12 +1191,12 @@ watchDebounced(
11691191 if (showDetailedListValue) {
11701192 const built = new Set<string>()
11711193 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))
11731195 }
11741196 const remaining = docOrderList.filter((d) => !built.has(d))
11751197 if (remaining.length) {
11761198 for (const docId of remaining) {
1177 await buildDocExcerpt(docId, filterTextValue)
1199 await buildDocExcerpt(docId, searchQuery)
11781200 if (canceled) return
11791201 await nextFrame()
11801202 if (canceled) return
@@ -1254,8 +1276,7 @@ onBeforeUnmount(() => {
12541276const excerptCache = new LRUCache<string, unknown>(24)
12551277
12561278async function fetchExcerpt(id: string) {
1257 const hashIndex = id.indexOf('#')
1258 const docId = hashIndex === -1 ? id : id.slice(0, hashIndex)
1279 const docId = getDocId(id)
12591280
12601281 const cached = excerptCache.get(docId)
12611282 if (cached) return { id, mod: cached }
docs/.vitepress/theme/workers/searchWorker.ts +7 −2
@@ -61,6 +61,7 @@ type WorkerSearchResult = SearchResult & Result
6161let searchIndex: MiniSearch<Result> | null = null
6262let searchIndexKey = ''
6363let urlLinks: PageLink[] = []
64let urlLinkHrefsLower: string[] = []
6465let urlLinksPromise: Promise<void> | null = null
6566let pageOrder = new Map<string, number>()
6667let docOrder = new Map<string, number>()
@@ -212,9 +213,11 @@ async function ensureUrlLinks() {
212213 .then((data: PageLink[]) => {
213214 data.sort((a, b) => getPageOrder(a.pageId) - getPageOrder(b.pageId))
214215 urlLinks = data
216 urlLinkHrefsLower = data.map((link) => link.href.toLowerCase())
215217 })
216218 .catch(() => {
217219 urlLinks = []
220 urlLinkHrefsLower = []
218221 })
219222 .finally(() => {
220223 urlLinksPromise = null
@@ -231,8 +234,10 @@ async function urlSearch(message: UrlSearchRequest) {
231234
232235 const matches: PageLink[] = []
233236 const pageGroups = new Map<string, PageGroupCount>()
234 for (const link of urlLinks) {
235 if (!link.href.toLowerCase().includes(query)) continue
237 for (let i = 0; i < urlLinks.length; i++) {
238 if (!urlLinkHrefsLower[i].includes(query)) continue
239
240 const link = urlLinks[i]
236241
237242 matches.push(link)
238243 const existing = pageGroups.get(link.pageId)