Commit 6cc977e6c5

6cc977e6c5b39f011f686ec2f27d9778025f2884

parent: ab30556fe6

Unsigned

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

table

Layout: unified · split

docs/.vitepress/configs/markdown/tableMore.ts +140 −45
@@ -12,8 +12,8 @@ const ROW_TOKEN_TYPES = new Set([
12 'inline' 12 'inline'
13]) 13])
14 14
15function buildToggleHtml(cols: number): string { 15function buildToggleHtml(cols: number, anchor: string): string {
16 return `<tbody class="table-more-toggle">\n` + 16 return `<tbody class="table-more-toggle" data-table-more="${anchor}">\n` +
17 `<tr class="table-more-toggle-row">\n` + 17 `<tr class="table-more-toggle-row">\n` +
18 `<td colspan="${cols}">\n` + 18 `<td colspan="${cols}">\n` +
19 `<label class="table-more-label">\n` + 19 `<label class="table-more-label">\n` +
@@ -123,6 +123,71 @@ function prevRowCells(tokens: any[]): any[] {
123 return owners 123 return owners
124} 124}
125 125
126function isTableMoreCloser(t: any): boolean {
127 return t?.type === 'html_block' && typeof t.content === 'string' &&
128 t.content.includes('</tbody>')
129}
130
131// Owners for chained <more>; cross-tbody ^^ clones via emitted check.
132function chainedRowOwners(tokens: any[]): any[] {
133 let closeIdx = -1
134 for (let k = tokens.length - 1; k >= 0; k--) {
135 if (tokens[k].type === 'table_close') {
136 closeIdx = k
137 break
138 }
139 }
140 if (closeIdx === -1) return []
141 const openIdx = findTableOpen(tokens, closeIdx)
142 if (openIdx === -1) return []
143 const rows: any[][] = []
144 let cur: any[] | null = null
145 for (let k = openIdx; k < tokens.length; k++) {
146 const t = tokens[k]
147 if (t.type === 'tr_open') cur = []
148 else if (t.type === 'tr_close') {
149 if (cur) rows.push(cur)
150 cur = null
151 } else if (
152 cur && (t.type === 'td_open' || t.type === 'th_open')
153 ) {
154 cur.push(t)
155 }
156 }
157 const remaining: number[] = []
158 const holder: any[] = []
159 let owners: any[] = []
160 for (const row of rows) {
161 const next: any[] = []
162 let c = 0
163 for (const td of row) {
164 while ((remaining[c] ?? 0) > 0) {
165 next[c] = holder[c]
166 c++
167 }
168 const w = colspanOf(td)
169 for (let k = 0; k < w; k++) next[c + k] = td
170 const rs = rowspanOf(td)
171 if (rs > 1) {
172 for (let k = 0; k < w; k++) {
173 remaining[c + k] = rs
174 holder[c + k] = td
175 }
176 }
177 c += w
178 }
179 while ((remaining[c] ?? 0) > 0) {
180 next[c] = holder[c]
181 c++
182 }
183 for (let k = 0; k < remaining.length; k++) {
184 if (remaining[k] > 0) remaining[k]--
185 }
186 owners = next
187 }
188 return owners
189}
190
126function cloneCellContent(state: any, owner: any) { 191function cloneCellContent(state: any, owner: any) {
127 const tokens = state.tokens 192 const tokens = state.tokens
128 const idx = tokens.indexOf(owner) 193 const idx = tokens.indexOf(owner)
@@ -211,12 +276,11 @@ function countTableCols(tokens: any[], tableCloseIdx: number): number {
211 return 0 276 return 0
212} 277}
213 278
214function stampTableAnchor(state: any, tokens: any[], tableCloseIdx: number) { 279// One global serial anchor per <more> section, in document order.
215 const openIdx = findTableOpen(tokens, tableCloseIdx) 280function nextSectionAnchor(state: any): string {
216 if (openIdx === -1) return
217 const n = state.env.__tableMoreCount ?? 0 281 const n = state.env.__tableMoreCount ?? 0
218 state.env.__tableMoreCount = n + 1 282 state.env.__tableMoreCount = n + 1
219 tokens[openIdx].attrJoin('data-table-more', String(n)) 283 return String(n)
220} 284}
221 285
222export function tableMorePlugin(md: MarkdownIt): void { 286export function tableMorePlugin(md: MarkdownIt): void {
@@ -229,19 +293,45 @@ export function tableMorePlugin(md: MarkdownIt): void {
229 if (!MARKER_RE.test(state.src.slice(pos, max).trim())) { 293 if (!MARKER_RE.test(state.src.slice(pos, max).trim())) {
230 return false 294 return false
231 } 295 }
232 // Only a marker directly after a table; otherwise literal text. 296 // Marker after table or previous <more> section; else literal text.
233 const prev = state.tokens[state.tokens.length - 1] 297 const prev = state.tokens[state.tokens.length - 1]
234 if (!prev || prev.type !== 'table_close') return false 298 const afterTable = !!prev && prev.type === 'table_close'
299 let afterChained = !!prev && isTableMoreCloser(prev)
300 if (afterChained) {
301 // Guard against arbitrary </tbody> html.
302 let lastClose = -1
303 for (let k = state.tokens.length - 1; k >= 0; k--) {
304 if (state.tokens[k].type === 'table_close') {
305 lastClose = k
306 break
307 }
308 }
309 let hasMarker = false
310 if (lastClose !== -1) {
311 for (let k = lastClose + 1; k < state.tokens.length; k++) {
312 if (state.tokens[k].type === 'table_more') {
313 hasMarker = true
314 break
315 }
316 }
317 }
318 if (!hasMarker) afterChained = false
319 }
320 if (!afterTable && !afterChained) return false
235 if (silent) return true 321 if (silent) return true
236 322
237 // Full table follows: let the table parser handle it, merge later. 323 // Full table follows: let the table parser handle it, merge later.
324 // Chained sections support bare rows only.
238 let line = startLine + 1 325 let line = startLine + 1
239 while (line < endLine && getLine(state, line).trim() === '') line++ 326 while (line < endLine && getLine(state, line).trim() === '') line++
240 const next1 = line < endLine ? getLine(state, line) : '' 327 const next1 = line < endLine ? getLine(state, line) : ''
241 const next2 = line + 1 < endLine ? getLine(state, line + 1) : '' 328 const next2 = line + 1 < endLine ? getLine(state, line + 1) : ''
242 const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2) 329 const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2)
330 if (afterChained && fullTableFollows) return false
243 331
244 let up: any[] = prevRowCells(state.tokens) 332 let up: any[] = afterTable
333 ? prevRowCells(state.tokens)
334 : chainedRowOwners(state.tokens)
245 const emitted = new Set(state.tokens) 335 const emitted = new Set(state.tokens)
246 const marker = state.push('table_more', '', 0) 336 const marker = state.push('table_more', '', 0)
247 marker.map = [startLine, startLine + 1] 337 marker.map = [startLine, startLine + 1]
@@ -390,8 +480,6 @@ export function tableMorePlugin(md: MarkdownIt): void {
390 if (t.type === 'tbody_open') t.attrJoin('class', 'table-more-hidden') 480 if (t.type === 'tbody_open') t.attrJoin('class', 'table-more-hidden')
391 } 481 }
392 482
393 stampTableAnchor(state, tokens, i)
394
395 // Toggle + hidden rows go inside the first table, before its close. 483 // Toggle + hidden rows go inside the first table, before its close.
396 const marker = tokens[i + 1] 484 const marker = tokens[i + 1]
397 marker.type = 'html_block' 485 marker.type = 'html_block'
@@ -401,7 +489,7 @@ export function tableMorePlugin(md: MarkdownIt): void {
401 marker.children = null 489 marker.children = null
402 marker.info = '' 490 marker.info = ''
403 marker.markup = '' 491 marker.markup = ''
404 marker.content = buildToggleHtml(cols) 492 marker.content = buildToggleHtml(cols, nextSectionAnchor(state))
405 493
406 const merged = tokens.slice(0, i) 494 const merged = tokens.slice(0, i)
407 merged.push(marker) 495 merged.push(marker)
@@ -413,59 +501,66 @@ export function tableMorePlugin(md: MarkdownIt): void {
413 continue 501 continue
414 } 502 }
415 503
416 // bare continuation rows follow the marker. 504 // Chained bare rows: one toggle per <more> section.
417 if (tokens[i + 2]?.type === 'tr_open') { 505 if (tokens[i + 2]?.type === 'tr_open') {
418 let j = i + 2 506 let j = i + 1
419 while (j < tokens.length && ROW_TOKEN_TYPES.has(tokens[j].type)) j++ 507 const sections: { marker: any; kept: any[]; closer: any }[] = []
420 const closer = tokens[j] 508 while (tokens[j]?.type === 'table_more') {
421 if ( 509 const rStart = j + 1
422 !closer || closer.type !== 'html_block' || 510 if (tokens[rStart]?.type !== 'tr_open') break
423 !closer.content.includes('</tbody>') 511 let k = rStart
424 ) { 512 while (k < tokens.length && ROW_TOKEN_TYPES.has(tokens[k].type)) k++
425 i++ 513 const closer = tokens[k]
426 continue 514 if (!isTableMoreCloser(closer)) break
515 const kept = tokens.slice(rStart, k)
516 const hidden = kept.filter((t) => t.type === 'tr_open').length
517 if (hidden === 0) {
518 j = k + 1
519 continue
520 }
521 sections.push({ marker: tokens[j], kept, closer })
522 j = k + 1
427 } 523 }
428 const kept = tokens.slice(i + 2, j) 524 if (sections.length === 0) {
429 const hidden = kept.filter((t) => t.type === 'tr_open').length 525 i++
430 if (hidden === 0) {
431 tokens.splice(i + 1, j - i)
432 continue 526 continue
433 } 527 }
434 528
435 let cols = countTableCols(tokens, i) 529 let cols = countTableCols(tokens, i)
436 if (cols <= 0) { 530 if (cols <= 0) {
437 const firstTr = kept.findIndex((t) => t.type === 'tr_open') 531 const firstKept = sections[0].kept
438 const end = kept.findIndex( 532 const firstTr = firstKept.findIndex((t) => t.type === 'tr_open')
533 const end = firstKept.findIndex(
439 (t, idx) => idx > firstTr && t.type === 'tr_close' 534 (t, idx) => idx > firstTr && t.type === 'tr_close'
440 ) 535 )
441 cols = countCells( 536 cols = countCells(
442 kept, 537 firstKept,
443 firstTr, 538 firstTr,
444 end === -1 ? kept.length : end, 539 end === -1 ? firstKept.length : end,
445 'td_open' 540 'td_open'
446 ) 541 )
447 } 542 }
448 if (cols <= 0) cols = 1 543 if (cols <= 0) cols = 1
449 544
450 stampTableAnchor(state, tokens, i) 545 const segment: any[] = []
451 546 for (const s of sections) {
452 const marker = tokens[i + 1] 547 const m = s.marker
453 marker.type = 'html_block' 548 m.type = 'html_block'
454 marker.tag = '' 549 m.tag = ''
455 marker.nesting = 0 550 m.nesting = 0
456 marker.attrs = null 551 m.attrs = null
457 marker.children = null 552 m.children = null
458 marker.info = '' 553 m.info = ''
459 marker.markup = '' 554 m.markup = ''
460 marker.content = buildToggleHtml(cols) + 555 m.content = buildToggleHtml(cols, nextSectionAnchor(state)) +
461 '<tbody class="table-more-hidden">\n' 556 '<tbody class="table-more-hidden">\n'
557 segment.push(m, ...s.kept, s.closer)
558 }
462 559
463 // Move the toggle + bare rows inside the first table.
464 const segment = tokens.slice(i + 1, j + 1)
465 const merged = tokens.slice(0, i) 560 const merged = tokens.slice(0, i)
466 merged.push(...segment) 561 merged.push(...segment)
467 merged.push(tokens[i]) 562 merged.push(tokens[i])
468 merged.push(...tokens.slice(j + 1)) 563 merged.push(...tokens.slice(j))
469 tokens.splice(0, tokens.length, ...merged) 564 tokens.splice(0, tokens.length, ...merged)
470 i += segment.length + 1 565 i += segment.length + 1
471 continue 566 continue
docs/.vitepress/plugins/urlSearchPlugin.ts +36 −2
@@ -93,6 +93,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
93 let h = i + 1 93 let h = i + 1
94 while (h < n && lines[h].trim() === '') h++ 94 while (h < n && lines[h].trim() === '') h++
95 let hiddenEnd = -1 95 let hiddenEnd = -1
96 let firstIsFullTable = false
96 if (h < n && isPipeLine(lines[h])) { 97 if (h < n && isPipeLine(lines[h])) {
97 if ( 98 if (
98 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1]) 99 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1])
@@ -102,6 +103,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
102 let e = h + 2 103 let e = h + 2
103 while (e + 1 < n && isPipeLine(lines[e + 1])) e++ 104 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
104 hiddenEnd = e 105 hiddenEnd = e
106 firstIsFullTable = true
105 } 107 }
106 } else { 108 } else {
107 let e = h 109 let e = h
@@ -113,8 +115,40 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
113 i++ 115 i++
114 continue 116 continue
115 } 117 }
116 blocks.push({ anchor: String(count++), startLine: runStart, endLine: hiddenEnd }) 118 // One block per <more> section; anchors mirror the renderer counter.
117 i = hiddenEnd + 1 119 if (firstIsFullTable) {
120 blocks.push({
121 anchor: String(count++),
122 startLine: runStart,
123 endLine: hiddenEnd
124 })
125 i = hiddenEnd + 1
126 continue
127 }
128 blocks.push({
129 anchor: String(count++),
130 startLine: runStart,
131 endLine: hiddenEnd
132 })
133 let end = hiddenEnd
134 while (true) {
135 let m = end + 1
136 while (m < n && lines[m].trim() === '') m++
137 if (m >= n || !isMoreMarker(lines[m])) break
138 let h2 = m + 1
139 while (h2 < n && lines[h2].trim() === '') h2++
140 if (h2 >= n || !isPipeLine(lines[h2])) break
141 // Bare rows only; stop at repeated header.
142 if (
143 h2 + 1 < n && isPipeLine(lines[h2 + 1]) &&
144 isDelimiterRow(lines[h2 + 1])
145 ) break
146 let e = h2
147 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
148 blocks.push({ anchor: String(count++), startLine: h2, endLine: e })
149 end = e
150 }
151 i = end + 1
118 } 152 }
119 153
120 return blocks 154 return blocks
docs/.vitepress/theme/Layout.vue +51 −19
@@ -431,14 +431,18 @@ const detailsContainsTerms = (d: HTMLElement, terms: string[]) => {
431// Expand tables whose hidden rows hold all searched terms. 431// Expand tables whose hidden rows hold all searched terms.
432const tableContainsTerms = (t: HTMLElement, terms: string[]) => { 432const tableContainsTerms = (t: HTMLElement, terms: string[]) => {
433 if (!terms.length) return false 433 if (!terms.length) return false
434 const hidden = t.querySelector('tbody.table-more-hidden') 434 const hiddens = t.querySelectorAll('tbody.table-more-hidden')
435 const text = ((hidden ?? t).textContent ?? '').toLowerCase() 435 const text = (hiddens.length
436 ? Array.from(hiddens).map((h) => h.textContent ?? '').join(' ')
437 : t.textContent ?? ''
438 ).toLowerCase()
436 return terms.every((term) => text.includes(term)) 439 return terms.every((term) => text.includes(term))
437} 440}
438 441
439const expandTableMoreEl = (t: HTMLElement) => { 442const expandTableMoreEl = (t: HTMLElement) => {
440 const box = getTableMoreCheckbox(t) 443 for (const box of getTableMoreCheckboxes(t)) {
441 if (box && !box.checked) box.checked = true 444 if (!box.checked) box.checked = true
445 }
442} 446}
443 447
444const expandTablesForTerms = (terms: string[]) => { 448const expandTablesForTerms = (terms: string[]) => {
@@ -580,16 +584,27 @@ const queueTableMoreQueryUpdate = (target: EventTarget | null) => {
580 if (!box) return 584 if (!box) return
581 const table = box.closest<HTMLElement>('table') 585 const table = box.closest<HTMLElement>('table')
582 if (!table) return 586 if (!table) return
587 const boxes = getTableMoreCheckboxes(table)
588 const idx = boxes.indexOf(box)
589 // Keep each table's open set a prefix so the URL stays serial.
583 if (box.checked) { 590 if (box.checked) {
591 boxes.forEach((b, j) => {
592 if (j < idx && !b.checked) b.checked = true
593 })
584 // Accordion: opening one closes the others so only it stays in the URL. 594 // Accordion: opening one closes the others so only it stays in the URL.
585 document.querySelectorAll<HTMLTableElement>(TABLE_MORE_SELECTOR).forEach( 595 document.querySelectorAll<HTMLTableElement>('table').forEach(
586 (other) => { 596 (other) => {
587 if (other !== table) { 597 if (other !== table) {
588 const otherBox = getTableMoreCheckbox(other) 598 for (const otherBox of getTableMoreCheckboxes(other)) {
589 if (otherBox) otherBox.checked = false 599 if (otherBox.checked) otherBox.checked = false
600 }
590 } 601 }
591 } 602 }
592 ) 603 )
604 } else {
605 boxes.forEach((b, j) => {
606 if (j > idx && b.checked) b.checked = false
607 })
593 } 608 }
594 void updateQueryForTableMore(table) 609 void updateQueryForTableMore(table)
595} 610}
@@ -617,23 +632,40 @@ const selectCollapsiblesByPath = async (collapsiblePath: string[]) => {
617 return opened 632 return opened
618} 633}
619 634
620const TABLE_MORE_SELECTOR = 'table[data-table-more]' 635const TABLE_MORE_SELECTOR = '.table-more-toggle[data-table-more]'
621 636
622const getTableMoreCheckbox = (table: HTMLElement) => 637const getTableMoreCheckboxes = (table: HTMLElement) =>
623 table.querySelector<HTMLInputElement>('.table-more-checkbox') 638 Array.from(
639 table.querySelectorAll<HTMLInputElement>('.table-more-checkbox')
640 )
624 641
642// Open requested sections plus earlier ones in the same table.
625const selectTablesByPath = async (anchors: string[]) => { 643const selectTablesByPath = async (anchors: string[]) => {
626 let opened: HTMLTableElement | undefined 644 let opened: HTMLTableElement | undefined
627 645
628 for (const anchor of anchors) { 646 for (const anchor of anchors) {
629 const table = document.querySelector<HTMLTableElement>( 647 const toggle = document.querySelector<HTMLElement>(
630 `table[data-table-more="${CSS.escape(anchor)}"]` 648 `.table-more-toggle[data-table-more="${CSS.escape(anchor)}"]`
631 ) 649 )
650 if (!toggle) continue
651 const table = toggle.closest<HTMLTableElement>('table')
632 if (!table) continue 652 if (!table) continue
633 653
634 const box = getTableMoreCheckbox(table) 654 const boxes = getTableMoreCheckboxes(table)
635 if (box && !box.checked) { 655 const target = toggle.querySelector<HTMLInputElement>(
636 box.checked = true 656 '.table-more-checkbox'
657 )
658 const targetIdx = target ? boxes.indexOf(target) : -1
659 let changed = false
660 boxes.forEach((box, idx) => {
661 if (targetIdx === -1 || idx <= targetIdx) {
662 if (!box.checked) {
663 box.checked = true
664 changed = true
665 }
666 }
667 })
668 if (changed) {
637 await nextTick() 669 await nextTick()
638 await nextFrame() 670 await nextFrame()
639 } 671 }
@@ -645,10 +677,10 @@ const selectTablesByPath = async (anchors: string[]) => {
645 677
646const getOpenTableMorePath = (root: ParentNode = document) => { 678const getOpenTableMorePath = (root: ParentNode = document) => {
647 const anchors: string[] = [] 679 const anchors: string[] = []
648 root.querySelectorAll<HTMLTableElement>(TABLE_MORE_SELECTOR).forEach( 680 root.querySelectorAll<HTMLElement>(TABLE_MORE_SELECTOR).forEach(
649 (table) => { 681 (toggle) => {
650 if (getTableMoreCheckbox(table)?.checked) { 682 if (toggle.querySelector('.table-more-checkbox:checked')) {
651 const anchor = table.dataset.tableMore 683 const anchor = toggle.dataset.tableMore
652 if (anchor) anchors.push(anchor) 684 if (anchor) anchors.push(anchor)
653 } 685 }
654 } 686 }
docs/.vitepress/theme/styles/table-more.css +21 −5
@@ -5,10 +5,23 @@
5 display: none; 5 display: none;
6} 6}
7 7
8.vp-doc table:has(.table-more-checkbox:checked) tbody.table-more-hidden { 8/* Show hidden only when its own toggle is checked. */
9.vp-doc
10 table tbody.table-more-toggle:has(.table-more-checkbox:checked)
11 + tbody.table-more-hidden {
9 display: table-row-group; 12 display: table-row-group;
10} 13}
11 14
15/* Hide later sections after first unchecked toggle. */
16.vp-doc
17 table tbody.table-more-toggle:has(.table-more-checkbox:not(:checked))
18 ~ tbody.table-more-toggle,
19.vp-doc
20 table tbody.table-more-toggle:has(.table-more-checkbox:not(:checked))
21 ~ tbody.table-more-hidden {
22 display: none !important;
23}
24
12.table-more-toggle-row > td { 25.table-more-toggle-row > td {
13 padding: 0 !important; 26 padding: 0 !important;
14 text-align: center; 27 text-align: center;
@@ -47,7 +60,7 @@
47 transition: transform 0.2s; 60 transition: transform 0.2s;
48} 61}
49 62
50.vp-doc table:has(.table-more-checkbox:checked) .table-more-icon { 63.table-more-toggle:has(.table-more-checkbox:checked) .table-more-icon {
51 transform: rotate(180deg); 64 transform: rotate(180deg);
52} 65}
53 66
@@ -55,11 +68,11 @@
55 display: none; 68 display: none;
56} 69}
57 70
58.vp-doc table:has(.table-more-checkbox:checked) .table-more-text--more { 71.table-more-toggle:has(.table-more-checkbox:checked) .table-more-text--more {
59 display: none; 72 display: none;
60} 73}
61 74
62.vp-doc table:has(.table-more-checkbox:checked) .table-more-text--less { 75.table-more-toggle:has(.table-more-checkbox:checked) .table-more-text--less {
63 display: inline; 76 display: inline;
64} 77}
65 78
@@ -75,7 +88,10 @@ html.effects-disabled .table-more-icon {
75} 88}
76 89
77@media print { 90@media print {
78 .vp-doc table tbody.table-more-hidden { 91 .vp-doc table tbody.table-more-hidden,
92 .vp-doc
93 table tbody.table-more-toggle:has(.table-more-checkbox:not(:checked))
94 ~ tbody.table-more-hidden {
79 display: table-row-group !important; 95 display: table-row-group !important;
80 } 96 }
81 97