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) => {
185 if (tabs) void updateQueryForSelectedTab(tabs) 185 if (tabs) void updateQueryForSelectedTab(tabs)
186} 186}
187 187
188// (tryOpenAnchoredContent) handle tab selection / scrolling.
188const TAB_LINK_MARKER = 'tab-' 189const TAB_LINK_MARKER = 'tab-'
189 190
190const rewriteAnchorLinks = () => { 191const rewriteAnchorLinks = () => {
191 const origin = window.location.origin 192 const origin = window.location.origin
192 const pathname = window.location.pathname
193 193
194 document 194 document
195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]') 195 .querySelectorAll<HTMLAnchorElement>('.VPDoc a[href]')
196 .forEach((link) => { 196 .forEach((link) => {
197 // Accept either the original `#…` form or a previously-rewritten absolute URL.
198 const raw = link.getAttribute('href') ?? '' 197 const raw = link.getAttribute('href') ?? ''
199 let fragment: string | null = null 198
200 if (raw.startsWith('#') && raw.length > 1) { 199 // Normalise to an absolute URL for uniform processing.
201 fragment = raw.slice(1) 200 let abs: URL
202 } else if (raw.startsWith(origin + pathname + '#')) { 201 try {
203 fragment = raw.slice((origin + pathname + '#').length) 202 abs = new URL(raw, origin + window.location.pathname)
204 } else if (raw.startsWith(origin + pathname + '?')) { 203 } catch {
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 204 return
215 } 205 }
216 206
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
217 const isTabLink = fragment.startsWith(TAB_LINK_MARKER) 213 const isTabLink = fragment.startsWith(TAB_LINK_MARKER)
218 const name = (isTabLink 214 if (!isTabLink) {
219 ? fragment.slice(TAB_LINK_MARKER.length) 215 // Plain heading link — rewrite to absolute so Copy Link has no stale
220 : fragment).trim() 216 // query to merge with.
221 if (!name) return 217 const href = `${origin}${abs.pathname}#${fragment}`
218 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
219 return
220 }
222 221
223 // Use absolute URLs so the browser's Copy Link has nothing to merge with 222 // Tab link: strip the marker prefix and move the name into ?tabs=
224 // the current ?tabs= query. 223 const name = fragment.slice(TAB_LINK_MARKER.length).trim()
225 const href = isTabLink 224 if (!name) return
226 ? `${origin}${pathname}?${TAB_QUERY_PARAM}=${encodeURIComponent(name)}`
227 : `${origin}${pathname}#${name}`
228 225
226 const href = `${origin}${abs.pathname}?${TAB_QUERY_PARAM}=${
227 encodeURIComponent(name)
228 }`
229 if (link.getAttribute('href') !== href) link.setAttribute('href', href) 229 if (link.getAttribute('href') !== href) link.setAttribute('href', href)
230 }) 230 })
231} 231}
@@ -404,8 +404,7 @@ const tryOpenAnchoredContent = async () => {
404 return 404 return
405 } 405 }
406 406
407 const isTabRelatedTarget = 407 const isTabRelatedTarget = target.classList.contains('tab-search-heading') ||
408 target.classList.contains('tab-search-heading') ||
409 Boolean(target.closest('.plugin-tabs')) 408 Boolean(target.closest('.plugin-tabs'))
410 409
411 if ( 410 if (
@@ -439,7 +438,7 @@ const tryOpenAnchoredContent = async () => {
439} 438}
440 439
441const rewriteAnchorLinksDeferred = () => 440const rewriteAnchorLinksDeferred = () =>
442 nextTick(() => rewriteAnchorLinks()) 441 nextTick(() => nextFrame().then(() => rewriteAnchorLinks()))
443 442
444onMounted(tryOpenAnchoredContent) 443onMounted(tryOpenAnchoredContent)
445onMounted(rewriteAnchorLinksDeferred) 444onMounted(rewriteAnchorLinksDeferred)
@@ -650,20 +649,50 @@ onMounted(() => {
650 if (!link) return 649 if (!link) return
651 const raw = link.getAttribute('href') ?? '' 650 const raw = link.getAttribute('href') ?? ''
652 651
653 // ── Tab link: #tab-name or already-rewritten /path?tabs=name ────────── 652 let linkUrl: URL | undefined
654 const tabFromHash = raw.startsWith('#' + TAB_LINK_MARKER) 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)
655 ? raw.slice(1 + TAB_LINK_MARKER.length) 665 ? raw.slice(1 + TAB_LINK_MARKER.length)
656 : null 666 : 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
657 const tabFromQuery = 673 const tabFromQuery =
658 !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) && !raw.includes('#') 674 isSamePageLink && !tabFromHash && raw.includes(`?${TAB_QUERY_PARAM}=`) &&
659 ? new URLSearchParams(raw.split('?')[1]).get(TAB_QUERY_PARAM) 675 !raw.includes('#')
676 ? (linkUrl?.searchParams ?? new URLSearchParams(raw.split('?')[1])).get(
677 TAB_QUERY_PARAM
678 )
660 : null 679 : 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)
662 683
663 if (tabName) { 684 if (tabName) {
664 e.preventDefault() 685 e.preventDefault()
665 e.stopImmediatePropagation() 686 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 }
667 rewriteAnchorLinks() 696 rewriteAnchorLinks()
668 void (async () => { 697 void (async () => {
669 const tabs = await selectTabsByPath([tabName]) 698 const tabs = await selectTabsByPath([tabName])
@@ -674,7 +703,9 @@ onMounted(() => {
674 703
675 // ── Plain heading link: #name or already-rewritten /path#name ───────── 704 // ── Plain heading link: #name or already-rewritten /path#name ─────────
676 // Only intercept when ?tabs= is present — otherwise let the router handle normally. 705 // 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 }
678 709
679 let hash: string | null = null 710 let hash: string | null = null
680 if (raw.startsWith('#') && raw.length > 1) { 711 if (raw.startsWith('#') && raw.length > 1) {
@@ -918,4 +949,4 @@ onUnmounted(() => {
918 </span> 949 </span>
919 </template> 950 </template>
920 </Layout> 951 </Layout>
921</template>\ No newline at end of file 952</template>