Commit 0182ddb741

0182ddb741fdc5682af2e2f2ded069ab45964d42

parent: e64713b578

Unsigned

taskylizard <75871323+taskylizard@users.noreply.github.com> · 2026-05-29 13:47 UTC

search fixes and improvs

Layout: unified · split

docs/.vitepress/theme/Layout.vue +87 −4
@@ -57,15 +57,93 @@ watch(() => route.path, () => {
5757})
5858
5959let searchNavigated = false
60let searchQuery = ''
6061
6162if (!import.meta.env.SSR) {
62 useEventListener(window, 'search-nav', () => {
63 useEventListener(window, 'search-nav', (event) => {
6364 searchNavigated = true
65 searchQuery = event instanceof CustomEvent &&
66 typeof event.detail?.query === 'string'
67 ? event.detail.query
68 : ''
6469 tryOpenCollapsibles()
6570 })
6671}
6772
68const openCollapsibles = (target: HTMLElement) => {
73const nextFrame = () =>
74 new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
75
76const getSearchTerms = (query: string) =>
77 query
78 .toLowerCase()
79 .split(/\s+/)
80 .map((term) => term.trim())
81 .filter(Boolean)
82
83const getTabsInSearchScope = (target: HTMLElement) => {
84 const tabs = new Set<HTMLElement>()
85 const closestTabs = target.closest<HTMLElement>('.plugin-tabs')
86 if (closestTabs) tabs.add(closestTabs)
87
88 const tagName = target.tagName
89 if (tagName.length === 2 && tagName[0] === 'H') {
90 const level = +tagName[1]
91 if (level >= 1 && level <= 6) {
92 let sibling = target.nextElementSibling
93
94 while (sibling) {
95 const siblingTag = sibling.tagName
96 if (
97 siblingTag.length === 2 && siblingTag[0] === 'H' &&
98 +siblingTag[1] <= level
99 ) break
100
101 if (sibling.matches('.plugin-tabs')) tabs.add(sibling as HTMLElement)
102 sibling.querySelectorAll<HTMLElement>('.plugin-tabs').forEach((tab) => {
103 tabs.add(tab)
104 })
105
106 sibling = sibling.nextElementSibling
107 }
108 }
109 }
110
111 return [...tabs]
112}
113
114const selectTabContainingQuery = async (target: HTMLElement, query: string) => {
115 const terms = getSearchTerms(query)
116 if (!terms.length) return
117
118 for (const tabs of getTabsInSearchScope(target)) {
119 const selectedTab = tabs.querySelector<HTMLButtonElement>(
120 '.plugin-tabs--tab[aria-selected="true"]'
121 )
122 const buttons = tabs.querySelectorAll<HTMLButtonElement>(
123 '.plugin-tabs--tab[role="tab"]'
124 )
125
126 for (let i = 0, len = buttons.length; i < len; i++) {
127 const button = buttons[i]
128 button.click()
129 await nextTick()
130 await nextFrame()
131
132 const panelId = button.getAttribute('aria-controls')
133 const panel = panelId
134 ? document.getElementById(panelId)
135 : tabs.querySelector<HTMLElement>('.plugin-tabs--content')
136 const text = panel?.textContent?.toLowerCase() ?? ''
137
138 if (terms.every((term) => text.includes(term))) return
139 }
140
141 selectedTab?.click()
142 await nextTick()
143 }
144}
145
146const openCollapsibles = async (target: HTMLElement) => {
69147 let scrollTarget = target
70148 let el: HTMLElement | null = target
71149
@@ -104,6 +182,8 @@ const openCollapsibles = (target: HTMLElement) => {
104182 }
105183 }
106184
185 await selectTabContainingQuery(target, searchQuery)
186
107187 requestAnimationFrame(() => {
108188 requestAnimationFrame(() => {
109189 const navBar = document.querySelector<HTMLElement>('.VPNavBar')
@@ -138,6 +218,7 @@ const tryOpenCollapsibles = async () => {
138218 if (target) {
139219 openCollapsibles(target)
140220 searchNavigated = false
221 searchQuery = ''
141222 }
142223 })
143224}
@@ -289,7 +370,9 @@ onMounted(() => {
289370 document.body.appendChild(tooltipEl)
290371
291372 const showTooltip = (e: MouseEvent) => {
292 const target = (e.target as HTMLElement).closest('.icon-tip') as HTMLElement | null
373 const target = (e.target as HTMLElement).closest('.icon-tip') as
374 | HTMLElement
375 | null
293376 if (!target) return
294377 const tip = target.dataset.tip
295378 if (!tip) return
@@ -527,4 +610,4 @@ onUnmounted(() => {
527610 </span>
528611 </template>
529612 </Layout>
530</template>
\ No newline at end of file
613</template>
docs/.vitepress/theme/components/Search.vue +78 −10
@@ -43,6 +43,7 @@ import {
4343 TextAlignStart
4444} from 'lucide-vue-next'
4545import Mark from 'mark.js/dist/mark.es6.js'
46import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client'
4647import { LRUCache } from '../composables/search/lru-cache'
4748import { createSearchTranslate } from '../composables/search/translation'
4849import { useData } from '../composables/search/use-data'
@@ -214,20 +215,22 @@ watchDebounced(
214215
215216 // Highlighting
216217 const mods = showDetailedListValue
217 ? await Promise.all(results.value.map((r) => fetchExcerpt(r.id)))
218 ? await Promise.all(_result.map((r) => fetchExcerpt(r.id)))
218219 : []
219220 if (canceled) return
220221 for (const { id, mod } of mods) {
221222 const mapId = id.slice(0, id.indexOf('#'))
222 let map = cache.get(mapId)
223 const cacheId = `${mapId}\n${filterTextValue}`
224 let map = cache.get(cacheId)
223225 if (map) continue
224226 map = new Map()
225 cache.set(mapId, map)
227 cache.set(cacheId, map)
226228 const comp = mod.default ?? mod
227229 if (comp?.render || comp?.setup) {
228230 const app = createApp(comp)
229231 // Silence warnings about missing components
230232 app.config.warnHandler = () => {}
233 enhanceAppWithTabs(app)
231234 app.provide(dataSymbol, vitePressData)
232235 Object.defineProperties(app.config.globalProperties, {
233236 $frontmatter: {
@@ -243,6 +246,7 @@ watchDebounced(
243246 })
244247 const div = document.createElement('div')
245248 app.mount(div)
249 await selectExcerptTabs(div, filterTextValue)
246250 const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6')
247251 headings.forEach((el) => {
248252 const href = el.querySelector('a')?.getAttribute('href')
@@ -266,7 +270,7 @@ watchDebounced(
266270
267271 results.value = _result.map((r) => {
268272 const [id, anchor] = r.id.split('#')
269 const map = cache.get(id)
273 const map = cache.get(`${id}\n${filterTextValue}`)
270274 const text = map?.get(anchor) ?? ''
271275 for (const term in r.match) {
272276 terms.add(term)
@@ -290,13 +294,17 @@ watchDebounced(
290294 for (let i = 0; i < excerpts.length; i++) {
291295 const excerptElement = excerpts[i] as HTMLElement
292296
293 const markNode = excerptElement.querySelector('mark[data-markjs="true"]') as HTMLElement | null
297 const markNode = excerptElement.querySelector(
298 'mark[data-markjs="true"]'
299 ) as HTMLElement | null
294300 if (markNode) {
295301 const markRect = markNode.getBoundingClientRect()
296302 const excerptRect = excerptElement.getBoundingClientRect()
297303
298 const relativeTop = (markRect.top - excerptRect.top) + excerptElement.scrollTop
299 const targetScrollTop = relativeTop - (excerptElement.clientHeight / 2) + (markRect.height / 2)
304 const relativeTop = (markRect.top - excerptRect.top) +
305 excerptElement.scrollTop
306 const targetScrollTop = relativeTop -
307 (excerptElement.clientHeight / 2) + (markRect.height / 2)
300308
301309 scrollMutations.push({
302310 element: excerptElement,
@@ -334,6 +342,58 @@ async function fetchExcerpt(id: string) {
334342 }
335343}
336344
345const nextFrame = () =>
346 new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
347
348const getSearchTerms = (query: string) =>
349 query
350 .toLowerCase()
351 .split(/\s+/)
352 .map((term) => term.trim())
353 .filter(Boolean)
354
355async function selectExcerptTabs(root: HTMLElement, query: string) {
356 const terms = getSearchTerms(query)
357 if (!terms.length) return
358
359 const tabs = root.querySelectorAll<HTMLElement>('.plugin-tabs')
360
361 for (let i = 0; i < tabs.length; i++) {
362 const tab = tabs[i]
363 const selectedTab = tab.querySelector<HTMLButtonElement>(
364 '.plugin-tabs--tab[aria-selected="true"]'
365 )
366 const buttons = tab.querySelectorAll<HTMLButtonElement>(
367 '.plugin-tabs--tab[role="tab"]'
368 )
369
370 let found = false
371
372 for (let j = 0; j < buttons.length; j++) {
373 const button = buttons[j]
374 button.click()
375 await nextTick()
376 await nextFrame()
377
378 const panelId = button.getAttribute('aria-controls')
379 const panel = panelId
380 ? root.querySelector<HTMLElement>(`#${CSS.escape(panelId)}`)
381 : tab.querySelector<HTMLElement>('.plugin-tabs--content')
382 const text = panel?.textContent?.toLowerCase() ?? ''
383
384 if (terms.every((term) => text.includes(term))) {
385 found = true
386 break
387 }
388 }
389
390 if (!found) {
391 selectedTab?.click()
392 await nextTick()
393 }
394 }
395}
396
337397/* Search input focus */
338398
339399const searchInput = ref<HTMLInputElement>()
@@ -417,14 +477,22 @@ onKeyStroke('Enter', (e) => {
417477 }
418478
419479 if (selectedPackage) {
420 window.dispatchEvent(new CustomEvent('search-nav'))
480 window.dispatchEvent(
481 new CustomEvent('search-nav', {
482 detail: { query: filterText.value }
483 })
484 )
421485 router.go(selectedPackage.id)
422486 showSearch.value = false
423487 }
424488})
425489
426490function onResultClick() {
427 window.dispatchEvent(new CustomEvent('search-nav'))
491 window.dispatchEvent(
492 new CustomEvent('search-nav', {
493 detail: { query: filterText.value }
494 })
495 )
428496 showSearch.value = false
429497}
430498
@@ -1140,4 +1208,4 @@ svg {
11401208 background-color: rgba(var(--vp-c-brand-1), 0.1);
11411209 border-radius: 4px;
11421210}
1143</style>
\ No newline at end of file
1211</style>