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([
1212 'inline'
1313])
1414
15function buildToggleHtml(cols: number): string {
16 return `<tbody class="table-more-toggle">\n` +
15function buildToggleHtml(cols: number, anchor: string): string {
16 return `<tbody class="table-more-toggle" data-table-more="${anchor}">\n` +
1717 `<tr class="table-more-toggle-row">\n` +
1818 `<td colspan="${cols}">\n` +
1919 `<label class="table-more-label">\n` +
@@ -123,6 +123,71 @@ function prevRowCells(tokens: any[]): any[] {
123123 return owners
124124}
125125
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
126191function cloneCellContent(state: any, owner: any) {
127192 const tokens = state.tokens
128193 const idx = tokens.indexOf(owner)
@@ -211,12 +276,11 @@ function countTableCols(tokens: any[], tableCloseIdx: number): number {
211276 return 0
212277}
213278
214function stampTableAnchor(state: any, tokens: any[], tableCloseIdx: number) {
215 const openIdx = findTableOpen(tokens, tableCloseIdx)
216 if (openIdx === -1) return
279// One global serial anchor per <more> section, in document order.
280function nextSectionAnchor(state: any): string {
217281 const n = state.env.__tableMoreCount ?? 0
218282 state.env.__tableMoreCount = n + 1
219 tokens[openIdx].attrJoin('data-table-more', String(n))
283 return String(n)
220284}
221285
222286export function tableMorePlugin(md: MarkdownIt): void {
@@ -229,19 +293,45 @@ export function tableMorePlugin(md: MarkdownIt): void {
229293 if (!MARKER_RE.test(state.src.slice(pos, max).trim())) {
230294 return false
231295 }
232 // Only a marker directly after a table; otherwise literal text.
296 // Marker after table or previous <more> section; else literal text.
233297 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
235321 if (silent) return true
236322
237323 // Full table follows: let the table parser handle it, merge later.
324 // Chained sections support bare rows only.
238325 let line = startLine + 1
239326 while (line < endLine && getLine(state, line).trim() === '') line++
240327 const next1 = line < endLine ? getLine(state, line) : ''
241328 const next2 = line + 1 < endLine ? getLine(state, line + 1) : ''
242329 const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2)
330 if (afterChained && fullTableFollows) return false
243331
244 let up: any[] = prevRowCells(state.tokens)
332 let up: any[] = afterTable
333 ? prevRowCells(state.tokens)
334 : chainedRowOwners(state.tokens)
245335 const emitted = new Set(state.tokens)
246336 const marker = state.push('table_more', '', 0)
247337 marker.map = [startLine, startLine + 1]
@@ -390,8 +480,6 @@ export function tableMorePlugin(md: MarkdownIt): void {
390480 if (t.type === 'tbody_open') t.attrJoin('class', 'table-more-hidden')
391481 }
392482
393 stampTableAnchor(state, tokens, i)
394
395483 // Toggle + hidden rows go inside the first table, before its close.
396484 const marker = tokens[i + 1]
397485 marker.type = 'html_block'
@@ -401,7 +489,7 @@ export function tableMorePlugin(md: MarkdownIt): void {
401489 marker.children = null
402490 marker.info = ''
403491 marker.markup = ''
404 marker.content = buildToggleHtml(cols)
492 marker.content = buildToggleHtml(cols, nextSectionAnchor(state))
405493
406494 const merged = tokens.slice(0, i)
407495 merged.push(marker)
@@ -413,59 +501,66 @@ export function tableMorePlugin(md: MarkdownIt): void {
413501 continue
414502 }
415503
416 // bare continuation rows follow the marker.
504 // Chained bare rows: one toggle per <more> section.
417505 if (tokens[i + 2]?.type === 'tr_open') {
418 let j = i + 2
419 while (j < tokens.length && ROW_TOKEN_TYPES.has(tokens[j].type)) j++
420 const closer = tokens[j]
421 if (
422 !closer || closer.type !== 'html_block' ||
423 !closer.content.includes('</tbody>')
424 ) {
425 i++
426 continue
506 let j = i + 1
507 const sections: { marker: any; kept: any[]; closer: any }[] = []
508 while (tokens[j]?.type === 'table_more') {
509 const rStart = j + 1
510 if (tokens[rStart]?.type !== 'tr_open') break
511 let k = rStart
512 while (k < tokens.length && ROW_TOKEN_TYPES.has(tokens[k].type)) k++
513 const closer = tokens[k]
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
427523 }
428 const kept = tokens.slice(i + 2, j)
429 const hidden = kept.filter((t) => t.type === 'tr_open').length
430 if (hidden === 0) {
431 tokens.splice(i + 1, j - i)
524 if (sections.length === 0) {
525 i++
432526 continue
433527 }
434528
435529 let cols = countTableCols(tokens, i)
436530 if (cols <= 0) {
437 const firstTr = kept.findIndex((t) => t.type === 'tr_open')
438 const end = kept.findIndex(
531 const firstKept = sections[0].kept
532 const firstTr = firstKept.findIndex((t) => t.type === 'tr_open')
533 const end = firstKept.findIndex(
439534 (t, idx) => idx > firstTr && t.type === 'tr_close'
440535 )
441536 cols = countCells(
442 kept,
537 firstKept,
443538 firstTr,
444 end === -1 ? kept.length : end,
539 end === -1 ? firstKept.length : end,
445540 'td_open'
446541 )
447542 }
448543 if (cols <= 0) cols = 1
449544
450 stampTableAnchor(state, tokens, i)
451
452 const marker = tokens[i + 1]
453 marker.type = 'html_block'
454 marker.tag = ''
455 marker.nesting = 0
456 marker.attrs = null
457 marker.children = null
458 marker.info = ''
459 marker.markup = ''
460 marker.content = buildToggleHtml(cols) +
461 '<tbody class="table-more-hidden">\n'
545 const segment: any[] = []
546 for (const s of sections) {
547 const m = s.marker
548 m.type = 'html_block'
549 m.tag = ''
550 m.nesting = 0
551 m.attrs = null
552 m.children = null
553 m.info = ''
554 m.markup = ''
555 m.content = buildToggleHtml(cols, nextSectionAnchor(state)) +
556 '<tbody class="table-more-hidden">\n'
557 segment.push(m, ...s.kept, s.closer)
558 }
462559
463 // Move the toggle + bare rows inside the first table.
464 const segment = tokens.slice(i + 1, j + 1)
465560 const merged = tokens.slice(0, i)
466561 merged.push(...segment)
467562 merged.push(tokens[i])
468 merged.push(...tokens.slice(j + 1))
563 merged.push(...tokens.slice(j))
469564 tokens.splice(0, tokens.length, ...merged)
470565 i += segment.length + 1
471566 continue
docs/.vitepress/plugins/urlSearchPlugin.ts +36 −2
@@ -93,6 +93,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
9393 let h = i + 1
9494 while (h < n && lines[h].trim() === '') h++
9595 let hiddenEnd = -1
96 let firstIsFullTable = false
9697 if (h < n && isPipeLine(lines[h])) {
9798 if (
9899 h + 1 < n && isPipeLine(lines[h + 1]) && isDelimiterRow(lines[h + 1])
@@ -102,6 +103,7 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
102103 let e = h + 2
103104 while (e + 1 < n && isPipeLine(lines[e + 1])) e++
104105 hiddenEnd = e
106 firstIsFullTable = true
105107 }
106108 } else {
107109 let e = h
@@ -113,8 +115,40 @@ function findTableMoreBlocks(lines: string[]): TableMoreBlock[] {
113115 i++
114116 continue
115117 }
116 blocks.push({ anchor: String(count++), startLine: runStart, endLine: hiddenEnd })
117 i = hiddenEnd + 1
118 // One block per <more> section; anchors mirror the renderer counter.
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
118152 }
119153
120154 return blocks
docs/.vitepress/theme/Layout.vue +51 −19
@@ -431,14 +431,18 @@ const detailsContainsTerms = (d: HTMLElement, terms: string[]) => {
431431// Expand tables whose hidden rows hold all searched terms.
432432const tableContainsTerms = (t: HTMLElement, terms: string[]) => {
433433 if (!terms.length) return false
434 const hidden = t.querySelector('tbody.table-more-hidden')
435 const text = ((hidden ?? t).textContent ?? '').toLowerCase()
434 const hiddens = t.querySelectorAll('tbody.table-more-hidden')
435 const text = (hiddens.length
436 ? Array.from(hiddens).map((h) => h.textContent ?? '').join(' ')
437 : t.textContent ?? ''
438 ).toLowerCase()
436439 return terms.every((term) => text.includes(term))
437440}
438441
439442const expandTableMoreEl = (t: HTMLElement) => {
440 const box = getTableMoreCheckbox(t)
441 if (box && !box.checked) box.checked = true
443 for (const box of getTableMoreCheckboxes(t)) {
444 if (!box.checked) box.checked = true
445 }
442446}
443447
444448const expandTablesForTerms = (terms: string[]) => {
@@ -580,16 +584,27 @@ const queueTableMoreQueryUpdate = (target: EventTarget | null) => {
580584 if (!box) return
581585 const table = box.closest<HTMLElement>('table')
582586 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.
583590 if (box.checked) {
591 boxes.forEach((b, j) => {
592 if (j < idx && !b.checked) b.checked = true
593 })
584594 // 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(
586596 (other) => {
587597 if (other !== table) {
588 const otherBox = getTableMoreCheckbox(other)
589 if (otherBox) otherBox.checked = false
598 for (const otherBox of getTableMoreCheckboxes(other)) {
599 if (otherBox.checked) otherBox.checked = false
600 }
590601 }
591602 }
592603 )
604 } else {
605 boxes.forEach((b, j) => {
606 if (j > idx && b.checked) b.checked = false
607 })
593608 }
594609 void updateQueryForTableMore(table)
595610}
@@ -617,23 +632,40 @@ const selectCollapsiblesByPath = async (collapsiblePath: string[]) => {
617632 return opened
618633}
619634
620const TABLE_MORE_SELECTOR = 'table[data-table-more]'
635const TABLE_MORE_SELECTOR = '.table-more-toggle[data-table-more]'
621636
622const getTableMoreCheckbox = (table: HTMLElement) =>
623 table.querySelector<HTMLInputElement>('.table-more-checkbox')
637const getTableMoreCheckboxes = (table: HTMLElement) =>
638 Array.from(
639 table.querySelectorAll<HTMLInputElement>('.table-more-checkbox')
640 )
624641
642// Open requested sections plus earlier ones in the same table.
625643const selectTablesByPath = async (anchors: string[]) => {
626644 let opened: HTMLTableElement | undefined
627645
628646 for (const anchor of anchors) {
629 const table = document.querySelector<HTMLTableElement>(
630 `table[data-table-more="${CSS.escape(anchor)}"]`
647 const toggle = document.querySelector<HTMLElement>(
648 `.table-more-toggle[data-table-more="${CSS.escape(anchor)}"]`
631649 )
650 if (!toggle) continue
651 const table = toggle.closest<HTMLTableElement>('table')
632652 if (!table) continue
633653
634 const box = getTableMoreCheckbox(table)
635 if (box && !box.checked) {
636 box.checked = true
654 const boxes = getTableMoreCheckboxes(table)
655 const target = toggle.querySelector<HTMLInputElement>(
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) {
637669 await nextTick()
638670 await nextFrame()
639671 }
@@ -645,10 +677,10 @@ const selectTablesByPath = async (anchors: string[]) => {
645677
646678const getOpenTableMorePath = (root: ParentNode = document) => {
647679 const anchors: string[] = []
648 root.querySelectorAll<HTMLTableElement>(TABLE_MORE_SELECTOR).forEach(
649 (table) => {
650 if (getTableMoreCheckbox(table)?.checked) {
651 const anchor = table.dataset.tableMore
680 root.querySelectorAll<HTMLElement>(TABLE_MORE_SELECTOR).forEach(
681 (toggle) => {
682 if (toggle.querySelector('.table-more-checkbox:checked')) {
683 const anchor = toggle.dataset.tableMore
652684 if (anchor) anchors.push(anchor)
653685 }
654686 }
docs/.vitepress/theme/styles/table-more.css +21 −5
@@ -5,10 +5,23 @@
55 display: none;
66}
77
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 {
912 display: table-row-group;
1013}
1114
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
1225.table-more-toggle-row > td {
1326 padding: 0 !important;
1427 text-align: center;
@@ -47,7 +60,7 @@
4760 transition: transform 0.2s;
4861}
4962
50.vp-doc table:has(.table-more-checkbox:checked) .table-more-icon {
63.table-more-toggle:has(.table-more-checkbox:checked) .table-more-icon {
5164 transform: rotate(180deg);
5265}
5366
@@ -55,11 +68,11 @@
5568 display: none;
5669}
5770
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 {
5972 display: none;
6073}
6174
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 {
6376 display: inline;
6477}
6578
@@ -75,7 +88,10 @@ html.effects-disabled .table-more-icon {
7588}
7689
7790@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 {
7995 display: table-row-group !important;
8096 }
8197