Commit 4877bf60e4

4877bf60e4d5dda5eb4d56ce726bf0b7da6874a9

parent: 33a4e4c6fa

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-11 06:33 UTC

Update Layout.vue

fix for cross page tab header

Layout: unified · split

docs/.vitepress/theme/Layout.vue +68 −37
@@ -185,47 +185,47 @@ const queueTabQueryUpdateForSelection = (target: EventTarget | null) => {
185185 if (tabs) void updateQueryForSelectedTab(tabs)
186186}
187187
188// (tryOpenAnchoredContent) handle tab selection / scrolling.
188189const TAB_LINK_MARKER = 'tab-'
189190
190191const rewriteAnchorLinks = () => {
191192 const origin = window.location.origin
192 const pathname = window.location.pathname
193193
194194 document
195195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]')
196196 .forEach((link) => {
197 // Accept either the original `#…` form or a previously-rewritten absolute URL.
198197 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 {
198
199 // Normalise to an absolute URL for uniform processing.
200 let abs: URL
201 try {
202 abs = new URL(raw, origin + window.location.pathname)
203 } catch {
214204 return
215205 }
216206
207 // Only rewrite same-origin links.
208 if (abs.origin !== origin) return
209
210 const fragment = abs.hash.slice(1) // strip leading #
211 if (!fragment) return
212
217213 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
214 if (!isTabLink) {
215 // Plain heading link — rewrite to absolute so Copy Link has no stale
216 // query to merge with.
217 const href = `${origin}${abs.pathname}#${fragment}`
218 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
219 return
220 }
222221
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}`
222 // Tab link: strip the marker prefix and move the name into ?tabs=
223 const name = fragment.slice(TAB_LINK_MARKER.length).trim()
224 if (!name) return
228225
226 const href = `${origin}${abs.pathname}?${TAB_QUERY_PARAM}=${
227 encodeURIComponent(name)
228 }`
229229 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
230230 })
231231}
@@ -404,8 +404,7 @@ const tryOpenAnchoredContent = async () => {
404404 return
405405 }
406406
407 const isTabRelatedTarget =
408 target.classList.contains('tab-search-heading') ||
407 const isTabRelatedTarget = target.classList.contains('tab-search-heading') ||
409408 Boolean(target.closest('.plugin-tabs'))
410409
411410 if (
@@ -439,7 +438,7 @@ const tryOpenAnchoredContent = async () => {
439438}
440439
441440const rewriteAnchorLinksDeferred = () =>
442 nextTick(() => rewriteAnchorLinks())
441 nextTick(() => nextFrame().then(() => rewriteAnchorLinks()))
443442
444443onMounted(tryOpenAnchoredContent)
445444onMounted(rewriteAnchorLinksDeferred)
@@ -650,20 +649,50 @@ onMounted(() => {
650649 if (!link) return
651650 const raw = link.getAttribute('href') ?? ''
652651
653 // ── Tab link: #tab-name or already-rewritten /path?tabs=name ──────────
654 const tabFromHash = raw.startsWith('#' + TAB_LINK_MARKER)
652 let linkUrl: URL | undefined
653 try {
654 linkUrl = new URL(raw, window.location.href)
655 } catch {
656 /* keep handling simple hash-only links below */
657 }
658 const isSamePageLink = linkUrl
659 ? linkUrl.origin === window.location.origin &&
660 linkUrl.pathname === route.path
661 : raw.startsWith('#')
662
663 // ── Tab link: #tab-name, /path#tab-name, or already-rewritten ?tabs=name ──
664 const tabFromHash = isSamePageLink && raw.startsWith('#' + TAB_LINK_MARKER)
655665 ? raw.slice(1 + TAB_LINK_MARKER.length)
656666 : null
667 const tabFromCrossPage =
668 !isSamePageLink && raw.includes('#' + TAB_LINK_MARKER)
669 ? raw.slice(
670 raw.indexOf('#' + TAB_LINK_MARKER) + 1 + TAB_LINK_MARKER.length
671 )
672 : null
657673 const tabFromQuery =
658 !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) && !raw.includes('#')
659 ? new URLSearchParams(raw.split('?')[1]).get(TAB_QUERY_PARAM)
674 isSamePageLink && !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) &&
675 !raw.includes('#')
676 ? (linkUrl?.searchParams ?? new URLSearchParams(raw.split('?')[1])).get(
677 TAB_QUERY_PARAM
678 )
660679 : null
661 const tabName = tabFromHash ?? tabFromQuery
680 // Cross-page tab links are handled by VitePress's router after rewriteAnchorLinks
681 // sets the correct ?tabs= href — don't intercept them here.
682 const tabName = tabFromCrossPage ? null : (tabFromHash ?? tabFromQuery)
662683
663684 if (tabName) {
664685 e.preventDefault()
665686 e.stopImmediatePropagation()
666 window.history.pushState(null, '', buildUrlWithTabs([tabName]))
687
688 // Same-page tab link only — cross-page tab links are handled by VitePress's
689 // own router after rewriteAnchorLinks has already set the correct ?tabs= href.
690 const nextUrl = buildUrlWithTabs([tabName])
691 const currentUrl =
692 `${window.location.pathname}${window.location.search}${window.location.hash}`
693 if (nextUrl !== currentUrl) {
694 window.history.replaceState(null, '', nextUrl)
695 }
667696 rewriteAnchorLinks()
668697 void (async () => {
669698 const tabs = await selectTabsByPath([tabName])
@@ -674,7 +703,9 @@ onMounted(() => {
674703
675704 // ── Plain heading link: #name or already-rewritten /path#name ─────────
676705 // Only intercept when ?tabs= is present — otherwise let the router handle normally.
677 if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) return
706 if (!new URLSearchParams(window.location.search).has(TAB_QUERY_PARAM)) {
707 return
708 }
678709
679710 let hash: string | null = null
680711 if (raw.startsWith('#') && raw.length > 1) {
@@ -918,4 +949,4 @@ onUnmounted(() => {
918949 </span>
919950 </template>
920951 </Layout>
921</template>
\ No newline at end of file
952</template>