Commit e217236081

e217236081fa75ee9a5ef9cc5f53f76b8a3d4daf

parent: d2453371b6

Unsigned

taskylizard <75871323+taskylizard@users.noreply.github.com> · 2026-06-12 09:09 UTC

fix: TOC not following scroll and no highlight

Layout: unified · split

docs/.vitepress/theme/Layout.vue +70 −9
@@ -192,7 +192,7 @@ const rewriteAnchorLinks = () => {
192 const pathname = window.location.pathname 192 const pathname = window.location.pathname
193 193
194 document 194 document
195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]') 195 .querySelectorAll<HTMLAnchorElement>('.VPDoc .vp-doc a[href]')
196 .forEach((link) => { 196 .forEach((link) => {
197 // Accept either the original `#…` form or a previously-rewritten absolute URL. 197 // Accept either the original `#…` form or a previously-rewritten absolute URL.
198 const raw = link.getAttribute('href') ?? '' 198 const raw = link.getAttribute('href') ?? ''
@@ -206,8 +206,12 @@ const rewriteAnchorLinks = () => {
206 const qs = raw.slice((origin + pathname + '?').length).split('#')[0] 206 const qs = raw.slice((origin + pathname + '?').length).split('#')[0]
207 const tabName = new URLSearchParams(qs).get(TAB_QUERY_PARAM) 207 const tabName = new URLSearchParams(qs).get(TAB_QUERY_PARAM)
208 if (tabName) { 208 if (tabName) {
209 const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(tabName)}` 209 const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${
210 if (link.getAttribute('href') !== href) link.setAttribute('href', href) 210 encodeURIComponent(tabName)
211 }`
212 if (link.getAttribute('href') !== href) {
213 link.setAttribute('href', href)
214 }
211 } 215 }
212 return 216 return
213 } else { 217 } else {
@@ -404,8 +408,7 @@ const tryOpenAnchoredContent = async () => {
404 return 408 return
405 } 409 }
406 410
407 const isTabRelatedTarget = 411 const isTabRelatedTarget = target.classList.contains('tab-search-heading') ||
408 target.classList.contains('tab-search-heading') ||
409 Boolean(target.closest('.plugin-tabs')) 412 Boolean(target.closest('.plugin-tabs'))
410 413
411 if ( 414 if (
@@ -438,14 +441,68 @@ const tryOpenAnchoredContent = async () => {
438 } 441 }
439} 442}
440 443
441const rewriteAnchorLinksDeferred = () => 444const rewriteAnchorLinksDeferred = () => nextTick(() => rewriteAnchorLinks())
442 nextTick(() => rewriteAnchorLinks()) 445
446let outlineFollowObserver: MutationObserver | null = null
447let outlineFollowRaf = 0
448
449const followActiveOutlineLink = () => {
450 cancelAnimationFrame(outlineFollowRaf)
451
452 outlineFollowRaf = requestAnimationFrame(() => {
453 const activeLink = document.querySelector<HTMLElement>(
454 '.VPDocAsideOutline a.outline-link.active'
455 )
456 const scroller = activeLink?.closest<HTMLElement>('.aside-container')
457 if (!activeLink || !scroller) return
458
459 const linkRect = activeLink.getBoundingClientRect()
460 const scrollerRect = scroller.getBoundingClientRect()
461 const margin = 32
462
463 if (linkRect.top < scrollerRect.top + margin) {
464 scroller.scrollTop += linkRect.top - scrollerRect.top - margin
465 } else if (linkRect.bottom > scrollerRect.bottom - margin) {
466 scroller.scrollTop += linkRect.bottom - scrollerRect.bottom + margin
467 }
468 })
469}
470
471const setupOutlineFollow = () => {
472 outlineFollowObserver?.disconnect()
473 outlineFollowObserver = null
474
475 const outline = document.querySelector('.VPDocAsideOutline')
476 if (!outline) return
477
478 outlineFollowObserver = new MutationObserver((mutations) => {
479 if (
480 mutations.some((mutation) =>
481 mutation.type === 'childList' ||
482 (mutation.target instanceof HTMLElement &&
483 mutation.target.matches('a.outline-link'))
484 )
485 ) {
486 followActiveOutlineLink()
487 }
488 })
489
490 outlineFollowObserver.observe(outline, {
491 attributes: true,
492 attributeFilter: ['class'],
493 childList: true,
494 subtree: true
495 })
496 followActiveOutlineLink()
497}
443 498
444onMounted(tryOpenAnchoredContent) 499onMounted(tryOpenAnchoredContent)
445onMounted(rewriteAnchorLinksDeferred) 500onMounted(rewriteAnchorLinksDeferred)
501onMounted(() => nextTick(setupOutlineFollow))
446watch(() => route.data, () => { 502watch(() => route.data, () => {
447 void tryOpenAnchoredContent() 503 void tryOpenAnchoredContent()
448 rewriteAnchorLinksDeferred() 504 rewriteAnchorLinksDeferred()
505 nextTick(setupOutlineFollow)
449}, { flush: 'post' }) 506}, { flush: 'post' })
450watch(() => route.query, () => { 507watch(() => route.query, () => {
451 void tryOpenAnchoredContent() 508 void tryOpenAnchoredContent()
@@ -674,7 +731,9 @@ onMounted(() => {
674 731
675 // ── Plain heading link: #name or already-rewritten /path#name ───────── 732 // ── Plain heading link: #name or already-rewritten /path#name ─────────
676 // Only intercept when ?tabs= is present — otherwise let the router handle normally. 733 // Only intercept when ?tabs= is present — otherwise let the router handle normally.
677 if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) return 734 if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) {
735 return
736 }
678 737
679 let hash: string | null = null 738 let hash: string | null = null
680 if (raw.startsWith('#') && raw.length > 1) { 739 if (raw.startsWith('#') && raw.length > 1) {
@@ -793,6 +852,8 @@ onMounted(() => {
793}) 852})
794 853
795onUnmounted(() => { 854onUnmounted(() => {
855 outlineFollowObserver?.disconnect()
856 cancelAnimationFrame(outlineFollowRaf)
796 takodachiDisable.value && takodachiDisable.value() 857 takodachiDisable.value && takodachiDisable.value()
797}) 858})
798</script> 859</script>
@@ -918,4 +979,4 @@ onUnmounted(() => {
918 </span> 979 </span>
919 </template> 980 </template>
920 </Layout> 981 </Layout>
921</template>\ No newline at end of file 982</template>