Commit ad888df47b

ad888df47b7cf9fefb420f3d34757820348da852

parent: d5e55ace80

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-10 16:53 UTC

fix tab hash issue

Layout: unified · split

docs/.vitepress/theme/Layout.vue +122 −3
@@ -172,6 +172,7 @@ const updateQueryForSelectedTab = async (tabs: HTMLElement) => {
172172 if (nextUrl === currentUrl) return
173173
174174 window.history.pushState(null, '', nextUrl)
175 rewriteAnchorLinks()
175176}
176177
177178const queueTabQueryUpdateForSelection = (target: EventTarget | null) => {
@@ -184,6 +185,51 @@ const queueTabQueryUpdateForSelection = (target: EventTarget | null) => {
184185 if (tabs) void updateQueryForSelectedTab(tabs)
185186}
186187
188const TAB_LINK_MARKER = 'tab-'
189
190const rewriteAnchorLinks = () => {
191 const origin = window.location.origin
192 const pathname = window.location.pathname
193
194 document
195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]')
196 .forEach((link) => {
197 // Accept either the original `#…` form or a previously-rewritten absolute URL.
198 const raw = link.getAttribute('href') ?? ''
199 let fragment: string | null = null
200 if (raw.startsWith('#') && raw.length > 1) {
201 fragment = raw.slice(1)
202 } else if (raw.startsWith(origin + pathname + '#')) {
203 fragment = raw.slice((origin + pathname + '#').length)
204 } else if (raw.startsWith(origin + pathname + '?')) {
205 // Already rewritten as a tab link — re-derive the tab name and re-stamp.
206 const qs = raw.slice((origin + pathname + '?').length).split('#')[0]
207 const tabName = new URLSearchParams(qs).get(TAB_QUERY_PARAM)
208 if (tabName) {
209 const href = `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(tabName)}`
210 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
211 }
212 return
213 } else {
214 return
215 }
216
217 const isTabLink = fragment.startsWith(TAB_LINK_MARKER)
218 const name = (isTabLink
219 ? fragment.slice(TAB_LINK_MARKER.length)
220 : fragment).trim()
221 if (!name) return
222
223 // Use absolute URLs so the browser's Copy Link has nothing to merge with
224 // the current ?tabs= query.
225 const href = isTabLink
226 ? `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(name)}`
227 : `${origin}${pathname}#${name}`
228
229 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
230 })
231}
232
187233const selectTabsByPath = async (tabPath: string[]) => {
188234 let root: ParentNode = document
189235 let selectedTabs: HTMLElement | undefined
@@ -358,6 +404,19 @@ const tryOpenAnchoredContent = async () => {
358404 return
359405 }
360406
407 const isTabRelatedTarget =
408 target.classList.contains('tab-search-heading') ||
409 Boolean(target.closest('.plugin-tabs'))
410
411 if (
412 !isTabRelatedTarget &&
413 new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)
414 ) {
415 window.history.replaceState(null, '', buildUrlWithTabs([], hash))
416 scrollToElement(getScrollTargetForAnchor(target), false)
417 return
418 }
419
361420 if (
362421 !requestedTabs.length && target.classList.contains('tab-search-heading')
363422 ) {
@@ -379,9 +438,19 @@ const tryOpenAnchoredContent = async () => {
379438 }
380439}
381440
441const rewriteAnchorLinksDeferred = () =>
442 nextTick(() => rewriteAnchorLinks())
443
382444onMounted(tryOpenAnchoredContent)
383watch(() => route.data, tryOpenAnchoredContent, { flush: 'post' })
384watch(() => route.query, tryOpenAnchoredContent, { flush: 'post' })
445onMounted(rewriteAnchorLinksDeferred)
446watch(() => route.data, () => {
447 void tryOpenAnchoredContent()
448 rewriteAnchorLinksDeferred()
449}, { flush: 'post' })
450watch(() => route.query, () => {
451 void tryOpenAnchoredContent()
452 rewriteAnchorLinksDeferred()
453}, { flush: 'post' })
385454watch(() => route.hash, tryOpenAnchoredContent, { flush: 'post' })
386455
387456function onSidebarEnter() {
@@ -575,6 +644,55 @@ onMounted(() => {
575644 document.addEventListener('mouseout', hideTooltip)
576645 document.addEventListener('scroll', hideTooltip, true)
577646
647 const onCaptureClick = (e: MouseEvent) => {
648 if (!e.isTrusted) return
649 const link = (e.target as HTMLElement).closest<HTMLAnchorElement>('a[href]')
650 if (!link) return
651 const raw = link.getAttribute('href') ?? ''
652
653 // ── Tab link: #tab-name or already-rewritten /path?tabs=name ──────────
654 const tabFromHash = raw.startsWith('#' + TAB_LINK_MARKER)
655 ? raw.slice(1 + TAB_LINK_MARKER.length)
656 : null
657 const tabFromQuery =
658 !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) && !raw.includes('#')
659 ? new URLSearchParams(raw.split('?')[1]).get(TAB_QUERY_PARAM)
660 : null
661 const tabName = tabFromHash ?? tabFromQuery
662
663 if (tabName) {
664 e.preventDefault()
665 e.stopImmediatePropagation()
666 window.history.pushState(null, '', buildUrlWithTabs([tabName]))
667 rewriteAnchorLinks()
668 void (async () => {
669 const tabs = await selectTabsByPath([tabName])
670 if (tabs) scrollToElement(getSelectedTabScrollTarget(tabs), false)
671 })()
672 return
673 }
674
675 // ── Plain heading link: #name or already-rewritten /path#name ─────────
676 // Only intercept when ?tabs= is present — otherwise let the router handle normally.
677 if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) return
678
679 let hash: string | null = null
680 if (raw.startsWith('#') && raw.length > 1) {
681 hash = raw.slice(1)
682 } else if (raw.startsWith(window.location.pathname + '#')) {
683 hash = raw.slice(window.location.pathname.length + 1)
684 }
685 if (!hash) return
686
687 e.preventDefault()
688 e.stopImmediatePropagation()
689 window.history.pushState(null, '', `${window.location.pathname}#${hash}`)
690 rewriteAnchorLinks()
691 void tryOpenAnchoredContent()
692 }
693
694 document.addEventListener('click', onCaptureClick, { capture: true })
695
578696 useEventListener(document, 'click', (e: MouseEvent) => {
579697 if (!e.isTrusted) return
580698 queueTabQueryUpdateForSelection(e.target)
@@ -589,6 +707,7 @@ onMounted(() => {
589707 })
590708
591709 onUnmounted(() => {
710 document.removeEventListener('click', onCaptureClick, { capture: true })
592711 document.removeEventListener('mouseover', showTooltip)
593712 document.removeEventListener('mouseout', hideTooltip)
594713 document.removeEventListener('scroll', hideTooltip, true)
@@ -799,4 +918,4 @@ onUnmounted(() => {
799918 </span>
800919 </template>
801920 </Layout>
802</template>
921</template>
\ No newline at end of file