| @@ -12,8 +12,8 @@ const ROW_TOKEN_TYPES = new Set([ |
| 12 | 'inline' |
12 | 'inline' |
| 13 | ]) |
13 | ]) |
| 14 | |
14 | |
| 15 | function buildToggleHtml(cols: number): string { |
15 | function 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 | |
| |
126 | function 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. |
| |
132 | function 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 | |
| 126 | function cloneCellContent(state: any, owner: any) { |
191 | function 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 | |
| 214 | function stampTableAnchor(state: any, tokens: any[], tableCloseIdx: number) { |
279 | // One global serial anchor per <more> section, in document order. |
| 215 | const openIdx = findTableOpen(tokens, tableCloseIdx) |
280 | function 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 | |
| 222 | export function tableMorePlugin(md: MarkdownIt): void { |
286 | export 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 |