Commit 92ee7baf0e

92ee7baf0e016a73d55496e42d9143c119687ff8

parent: fcda9b7b83

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-30 04:41 UTC

init

Layout: unified · split

docs/.vitepress/configs/markdown/index.ts +16 −3
@@ -17,6 +17,7 @@ import type { MarkdownRenderer } from 'vitepress'
17import { 17import {
18 getTabAnchor, 18 getTabAnchor,
19 getTabHeadingAnchor, 19 getTabHeadingAnchor,
20 getUniqueAnchor,
20 parseTabLabel 21 parseTabLabel
21} from '../../utils/tabAnchors' 22} from '../../utils/tabAnchors'
22import { getTooltip } from '../../utils/tooltips' 23import { getTooltip } from '../../utils/tooltips'
@@ -148,6 +149,7 @@ function injectSearchHeadings(src: string) {
148 const headingStack: (SearchHeading | undefined)[] = [] 149 const headingStack: (SearchHeading | undefined)[] = []
149 const tabResetStack: (SearchHeading | undefined)[] = [] 150 const tabResetStack: (SearchHeading | undefined)[] = []
150 const tabAnchorCounts = new Map<string, number>() 151 const tabAnchorCounts = new Map<string, number>()
152 const collapsibleAnchorCounts = new Map<string, number>()
151 const getCurrentHeading = () => { 153 const getCurrentHeading = () => {
152 for (let i = headingStack.length - 1; i >= 0; i--) { 154 for (let i = headingStack.length - 1; i >= 0; i--) {
153 if (headingStack[i]) return headingStack[i] 155 if (headingStack[i]) return headingStack[i]
@@ -228,6 +230,20 @@ function injectSearchHeadings(src: string) {
228 ) 230 )
229 if (!match) continue 231 if (!match) continue
230 232
233 const [, indent, , rawTitle] = match
234 const parsedTitle = parseTabLabel(rawTitle)
235 const headingTitle = parsedTitle.label
236 if (!headingTitle) continue
237
238 const collapsibleAnchor = parsedTitle.anchor ||
239 getUniqueAnchor(headingTitle, collapsibleAnchorCounts)
240
241 lines[i] = lines[i].replace(
242 /(<Collapsible\b[^>]*?\btitle=)(['"]).*?\2/i,
243 (_full, pre: string, quote: string) =>
244 `${pre}${quote}${headingTitle}${quote} anchor=${quote}${collapsibleAnchor}${quote}`
245 )
246
231 let nextLine = i + 1 247 let nextLine = i + 1
232 while (nextLine < lines.length && lines[nextLine].trim() === '') { 248 while (nextLine < lines.length && lines[nextLine].trim() === '') {
233 nextLine++ 249 nextLine++
@@ -235,9 +251,6 @@ function injectSearchHeadings(src: string) {
235 251
236 if (/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) continue 252 if (/^#{1,6}\s+/.test(lines[nextLine]?.trim() ?? '')) continue
237 253
238 const [, indent, , title] = match
239 const headingTitle = title.trim()
240 if (!headingTitle) continue
241 const headingLevel = containerStack.includes('tabs') ? 4 : 3 254 const headingLevel = containerStack.includes('tabs') ? 4 : 3
242 255
243 lines.splice( 256 lines.splice(
docs/.vitepress/theme/Layout.vue +133 −12
@@ -102,35 +102,53 @@ const getScrollTargetForAnchor = (target: HTMLElement) => {
102} 102}
103 103
104const TAB_QUERY_PARAM = 'tabs' 104const TAB_QUERY_PARAM = 'tabs'
105const COLLAPSIBLE_QUERY_PARAM = 'c'
105 106
106const getRequestedTabPath = () => { 107const getRequestedAnchorPath = (param: string) => {
107 const value = new URLSearchParams(window.location.search).get( 108 const value = new URLSearchParams(window.location.search).get(param)
108 TAB_QUERY_PARAM
109 )
110 return value 109 return value
111 ?.split(',') 110 ?.split(',')
112 .map((tab) => tab.trim()) 111 .map((anchor) => anchor.trim())
113 .filter(Boolean) ?? [] 112 .filter(Boolean) ?? []
114} 113}
115 114
116const buildUrlWithTabs = (tabs: string[], hash = '') => { 115const getRequestedTabPath = () => getRequestedAnchorPath(TAB_QUERY_PARAM)
116const getRequestedCollapsiblePath = () =>
117 getRequestedAnchorPath(COLLAPSIBLE_QUERY_PARAM)
118
119const encodeAnchorList = (anchors: string[]) =>
120 anchors.map((anchor) => encodeURIComponent(anchor)).join(',')
121
122const buildUrl = (
123 options: { tabs?: string[]; collapsibles?: string[]; hash?: string } = {}
124) => {
117 const params = new URLSearchParams(window.location.search) 125 const params = new URLSearchParams(window.location.search)
126 const currentTabs = params.get(TAB_QUERY_PARAM)
127 const currentCollapsibles = params.get(COLLAPSIBLE_QUERY_PARAM)
118 params.delete(TAB_QUERY_PARAM) 128 params.delete(TAB_QUERY_PARAM)
129 params.delete(COLLAPSIBLE_QUERY_PARAM)
130
131 const tabs = options.tabs ??
132 (currentTabs ? currentTabs.split(',').filter(Boolean) : [])
133 const collapsibles = options.collapsibles ??
134 (currentCollapsibles ? currentCollapsibles.split(',').filter(Boolean) : [])
119 135
120 const queryParts = [ 136 const queryParts = [
121 params.toString(), 137 params.toString(),
122 tabs.length 138 tabs.length ? `${TAB_QUERY_PARAM}=${encodeAnchorList(tabs)}` : '',
123 ? `${TAB_QUERY_PARAM}=${ 139 collapsibles.length
124 tabs.map((tab) => encodeURIComponent(tab)).join(',') 140 ? `${COLLAPSIBLE_QUERY_PARAM}=${encodeAnchorList(collapsibles)}`
125 }`
126 : '' 141 : ''
127 ].filter(Boolean) 142 ].filter(Boolean)
128 143
144 const hash = options.hash ?? ''
129 return `${window.location.pathname}${ 145 return `${window.location.pathname}${
130 queryParts.length ? `?${queryParts.join('&')}` : '' 146 queryParts.length ? `?${queryParts.join('&')}` : ''
131 }${hash ? `#${encodeURIComponent(hash)}` : ''}` 147 }${hash ? `#${encodeURIComponent(hash)}` : ''}`
132} 148}
133 149
150const buildUrlWithTabs = (tabs: string[], hash = '') => buildUrl({ tabs, hash })
151
134const getSelectedTabAnchor = (tabs: HTMLElement) => 152const getSelectedTabAnchor = (tabs: HTMLElement) =>
135 tabs.querySelector<HTMLButtonElement>( 153 tabs.querySelector<HTMLButtonElement>(
136 '.plugin-tabs--tab[aria-selected="true"][data-tab-anchor]' 154 '.plugin-tabs--tab[aria-selected="true"][data-tab-anchor]'
@@ -165,7 +183,9 @@ const updateQueryForSelectedTab = async (tabs: HTMLElement) => {
165 const tabPath = getTabPathForGroup(tabs) 183 const tabPath = getTabPathForGroup(tabs)
166 if (!tabPath.length) return 184 if (!tabPath.length) return
167 185
168 const nextUrl = buildUrlWithTabs(tabPath) 186 const collapsibles = getOpenCollapsiblePath(tabs)
187
188 const nextUrl = buildUrl({ tabs: tabPath, collapsibles })
169 const currentUrl = 189 const currentUrl =
170 `${window.location.pathname}${window.location.search}${window.location.hash}` 190 `${window.location.pathname}${window.location.search}${window.location.hash}`
171 if (nextUrl === currentUrl) return 191 if (nextUrl === currentUrl) return
@@ -184,6 +204,87 @@ const queueTabQueryUpdateForSelection = (target: EventTarget | null) => {
184 if (tabs) void updateQueryForSelectedTab(tabs) 204 if (tabs) void updateQueryForSelectedTab(tabs)
185} 205}
186 206
207const COLLAPSIBLE_SELECTOR = 'details[data-collapsible-anchor]'
208
209const getOpenCollapsiblePath = (details: HTMLElement) => {
210 const path: string[] = []
211 let el: HTMLElement | null = details
212
213 while (el) {
214 const current: HTMLElement | null = el.closest(COLLAPSIBLE_SELECTOR)
215 if (!current) break
216
217 if ((current as HTMLDetailsElement).open) {
218 const anchor = current.dataset.collapsibleAnchor
219 if (anchor) path.unshift(anchor)
220 }
221
222 el = current.parentElement
223 }
224
225 return path
226}
227
228const getEnclosingTabPath = (el: HTMLElement) => {
229 const panel = el.closest<HTMLElement>(
230 '.plugin-tabs--content[data-tab-anchor]'
231 )
232 const tabs = panel?.closest<HTMLElement>('.plugin-tabs')
233 return tabs ? getTabPathForGroup(tabs) : []
234}
235
236const updateQueryForCollapsible = async (details: HTMLElement) => {
237 await nextTick()
238 await nextFrame()
239
240 const collapsiblePath = getOpenCollapsiblePath(details)
241 const tabPath = getEnclosingTabPath(details)
242
243 const nextUrl = buildUrl({
244 tabs: tabPath.length ? tabPath : undefined,
245 collapsibles: collapsiblePath
246 })
247 const currentUrl =
248 `${window.location.pathname}${window.location.search}${window.location.hash}`
249 if (nextUrl === currentUrl) return
250
251 window.history.pushState(null, '', nextUrl)
252 rewriteAnchorLinks()
253}
254
255const queueCollapsibleQueryUpdate = (target: EventTarget | null) => {
256 if (!(target instanceof HTMLElement)) return
257
258 const summary = target.closest<HTMLElement>('summary')
259 const details = summary?.parentElement
260 if (details instanceof HTMLElement && details.matches(COLLAPSIBLE_SELECTOR)) {
261 void updateQueryForCollapsible(details)
262 }
263}
264
265const selectCollapsiblesByPath = async (collapsiblePath: string[]) => {
266 let root: ParentNode = document
267 let opened: HTMLElement | undefined
268
269 for (const anchor of collapsiblePath) {
270 const details = root.querySelector<HTMLDetailsElement>(
271 `details[data-collapsible-anchor="${CSS.escape(anchor)}"]`
272 )
273 if (!details) return opened
274
275 opened = details
276 if (!details.open) {
277 details.open = true
278 await nextTick()
279 await nextFrame()
280 }
281
282 root = details
283 }
284
285 return opened
286}
287
187const TAB_LINK_MARKER = 'tab-' 288const TAB_LINK_MARKER = 'tab-'
188 289
189const rewriteAnchorLinks = () => { 290const rewriteAnchorLinks = () => {
@@ -375,11 +476,17 @@ const tryOpenAnchoredContent = async () => {
375 const selectedTabs = requestedTabs.length 476 const selectedTabs = requestedTabs.length
376 ? await selectTabsByPath(requestedTabs) 477 ? await selectTabsByPath(requestedTabs)
377 : undefined 478 : undefined
479 const requestedCollapsibles = getRequestedCollapsiblePath()
480 const openedCollapsible = requestedCollapsibles.length
481 ? await selectCollapsiblesByPath(requestedCollapsibles)
482 : undefined
378 const hash = window.location.hash.slice(1) 483 const hash = window.location.hash.slice(1)
379 484
380 if (!hash) { 485 if (!hash) {
381 resetSearchNavigation() 486 resetSearchNavigation()
382 if (selectedTabs) { 487 if (openedCollapsible) {
488 scrollToElement(openedCollapsible, false)
489 } else if (selectedTabs) {
383 scrollToElement(getSelectedTabScrollTarget(selectedTabs), false) 490 scrollToElement(getSelectedTabScrollTarget(selectedTabs), false)
384 } 491 }
385 return 492 return
@@ -403,6 +510,19 @@ const tryOpenAnchoredContent = async () => {
403 510
404 if (searchNavigated) { 511 if (searchNavigated) {
405 await openCollapsibles(target) 512 await openCollapsibles(target)
513 const collapsiblePath = getOpenCollapsiblePath(target)
514 if (collapsiblePath.length) {
515 const tabPath = getEnclosingTabPath(target)
516 window.history.replaceState(
517 null,
518 '',
519 buildUrl({
520 tabs: tabPath.length ? tabPath : undefined,
521 collapsibles: collapsiblePath
522 })
523 )
524 rewriteAnchorLinks()
525 }
406 resetSearchNavigation() 526 resetSearchNavigation()
407 return 527 return
408 } 528 }
@@ -972,6 +1092,7 @@ onMounted(() => {
972 useEventListener(document, 'click', (e: MouseEvent) => { 1092 useEventListener(document, 'click', (e: MouseEvent) => {
973 if (!e.isTrusted) return 1093 if (!e.isTrusted) return
974 queueTabQueryUpdateForSelection(e.target) 1094 queueTabQueryUpdateForSelection(e.target)
1095 queueCollapsibleQueryUpdate(e.target)
975 }) 1096 })
976 1097
977 useEventListener(document, 'keydown', (e: KeyboardEvent) => { 1098 useEventListener(document, 'keydown', (e: KeyboardEvent) => {
docs/.vitepress/theme/components/Collapsible.vue +2 −1
@@ -4,13 +4,14 @@ import { ref } from 'vue'
4const props = defineProps<{ 4const props = defineProps<{
5 title?: string 5 title?: string
6 type?: 'tip' | 'warning' | 'danger' 6 type?: 'tip' | 'warning' | 'danger'
7 anchor?: string
7}>() 8}>()
8 9
9const open = ref(false) 10const open = ref(false)
10</script> 11</script>
11 12
12<template> 13<template>
13 <details class="details custom-block" :class="type" :open="open || undefined" @toggle="open = ($event.target as HTMLDetailsElement).open"> 14 <details class="details custom-block" :class="type" :data-collapsible-anchor="anchor || undefined" :open="open || undefined" @toggle="open = ($event.target as HTMLDetailsElement).open">
14 <summary> 15 <summary>
15 <span v-if="open" class="i-iconoir-nav-arrow-down collapsible-icon" /> 16 <span v-if="open" class="i-iconoir-nav-arrow-down collapsible-icon" />
16 <span v-else class="i-iconoir-nav-arrow-right collapsible-icon" /> 17 <span v-else class="i-iconoir-nav-arrow-right collapsible-icon" />