Commit 1ae3a2342b

1ae3a2342b5833ded85654733499e08a757fa86e

parent: abc0d6f4a4

Unsigned

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

Revert "Update Layout.vue"

This reverts commit 4877bf60e4d5dda5eb4d56ce726bf0b7da6874a9.

Layout: unified · split

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