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 = () => {
192192 const pathname = window.location.pathname
193193
194194 document
195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]')
195 .querySelectorAll<HTMLAnchorElement>('.VPDoc .vp-doc a[href]')
196196 .forEach((link) => {
197197 // Accept either the original `#…` form or a previously-rewritten absolute URL.
198198 const raw = link.getAttribute('href') ?? ''
@@ -206,8 +206,12 @@ const rewriteAnchorLinks = () => {
206206 const qs = raw.slice((origin + pathname + '?').length).split('#')[0]
207207 const tabName = new URLSearchParams(qs).get(TAB_QUERY_PARAM)
208208 if (tabName) {
209 const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(tabName)}`
210 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
209 const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${
210 encodeURIComponent(tabName)
211 }`
212 if (link.getAttribute('href') !== href) {
213 link.setAttribute('href', href)
214 }
211215 }
212216 return
213217 } else {
@@ -404,8 +408,7 @@ const tryOpenAnchoredContent = async () => {
404408 return
405409 }
406410
407 const isTabRelatedTarget =
408 target.classList.contains('tab-search-heading') ||
411 const isTabRelatedTarget = target.classList.contains('tab-search-heading') ||
409412 Boolean(target.closest('.plugin-tabs'))
410413
411414 if (
@@ -438,14 +441,68 @@ const tryOpenAnchoredContent = async () => {
438441 }
439442}
440443
441const rewriteAnchorLinksDeferred = () =>
442 nextTick(() => rewriteAnchorLinks())
444const rewriteAnchorLinksDeferred = () => 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}
443498
444499onMounted(tryOpenAnchoredContent)
445500onMounted(rewriteAnchorLinksDeferred)
501onMounted(() => nextTick(setupOutlineFollow))
446502watch(() => route.data, () => {
447503 void tryOpenAnchoredContent()
448504 rewriteAnchorLinksDeferred()
505 nextTick(setupOutlineFollow)
449506}, { flush: 'post' })
450507watch(() => route.query, () => {
451508 void tryOpenAnchoredContent()
@@ -674,7 +731,9 @@ onMounted(() => {
674731
675732 // ── Plain heading link: #name or already-rewritten /path#name ─────────
676733 // 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 }
678737
679738 let hash: string | null = null
680739 if (raw.startsWith('#') && raw.length > 1) {
@@ -793,6 +852,8 @@ onMounted(() => {
793852})
794853
795854onUnmounted(() => {
855 outlineFollowObserver?.disconnect()
856 cancelAnimationFrame(outlineFollowRaf)
796857 takodachiDisable.value && takodachiDisable.value()
797858})
798859</script>
@@ -918,4 +979,4 @@ onUnmounted(() => {
918979 </span>
919980 </template>
920981 </Layout>
921</template>
\ No newline at end of file
982</template>