| @@ -12,8 +12,8 @@ const ROW_TOKEN_TYPES = new Set([ |
| 12 | 12 | 'inline' |
| 13 | 13 | ]) |
| 14 | 14 | |
| 15 | | function buildToggleHtml(cols: number): string { |
| 16 | | return `<tbody class="table-more-toggle">\n` + |
| 15 | function buildToggleHtml(cols: number, anchor: string): string { |
| 16 | return `<tbody class="table-more-toggle" data-table-more="${anchor}">\n` + |
| 17 | 17 | `<tr class="table-more-toggle-row">\n` + |
| 18 | 18 | `<td colspan="${cols}">\n` + |
| 19 | 19 | `<label class="table-more-label">\n` + |
| @@ -123,6 +123,71 @@ function prevRowCells(tokens: any[]): any[] { |
| 123 | 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 | 191 | function cloneCellContent(state: any, owner: any) { |
| 127 | 192 | const tokens = state.tokens |
| 128 | 193 | const idx = tokens.indexOf(owner) |
| @@ -211,12 +276,11 @@ function countTableCols(tokens: any[], tableCloseIdx: number): number { |
| 211 | 276 | return 0 |
| 212 | 277 | } |
| 213 | 278 | |
| 214 | | function 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. |
| 280 | function nextSectionAnchor(state: any): string { |
| 217 | 281 | const n = state.env.__tableMoreCount ?? 0 |
| 218 | 282 | state.env.__tableMoreCount = n + 1 |
| 219 | | tokens[openIdx].attrJoin('data-table-more', String(n)) |
| 283 | return String(n) |
| 220 | 284 | } |
| 221 | 285 | |
| 222 | 286 | export function tableMorePlugin(md: MarkdownIt): void { |
| @@ -229,19 +293,45 @@ export function tableMorePlugin(md: MarkdownIt): void { |
| 229 | 293 | if (!MARKER_RE.test(state.src.slice(pos, max).trim())) { |
| 230 | 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 | 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 | 321 | if (silent) return true |
| 236 | 322 | |
| 237 | 323 | // Full table follows: let the table parser handle it, merge later. |
| 324 | // Chained sections support bare rows only. |
| 238 | 325 | let line = startLine + 1 |
| 239 | 326 | while (line < endLine && getLine(state, line).trim() === '') line++ |
| 240 | 327 | const next1 = line < endLine ? getLine(state, line) : '' |
| 241 | 328 | const next2 = line + 1 < endLine ? getLine(state, line + 1) : '' |
| 242 | 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 | 335 | const emitted = new Set(state.tokens) |
| 246 | 336 | const marker = state.push('table_more', '', 0) |
| 247 | 337 | marker.map = [startLine, startLine + 1] |
| @@ -390,8 +480,6 @@ export function tableMorePlugin(md: MarkdownIt): void { |
| 390 | 480 | if (t.type === 'tbody_open') t.attrJoin('class', 'table-more-hidden') |
| 391 | 481 | } |
| 392 | 482 | |
| 393 | | stampTableAnchor(state, tokens, i) |
| 394 | | |
| 395 | 483 | // Toggle + hidden rows go inside the first table, before its close. |
| 396 | 484 | const marker = tokens[i + 1] |
| 397 | 485 | marker.type = 'html_block' |
| @@ -401,7 +489,7 @@ export function tableMorePlugin(md: MarkdownIt): void { |
| 401 | 489 | marker.children = null |
| 402 | 490 | marker.info = '' |
| 403 | 491 | marker.markup = '' |
| 404 | | marker.content = buildToggleHtml(cols) |
| 492 | marker.content = buildToggleHtml(cols, nextSectionAnchor(state)) |
| 405 | 493 | |
| 406 | 494 | const merged = tokens.slice(0, i) |
| 407 | 495 | merged.push(marker) |
| @@ -413,59 +501,66 @@ export function tableMorePlugin(md: MarkdownIt): void { |
| 413 | 501 | continue |
| 414 | 502 | } |
| 415 | 503 | |
| 416 | | // bare continuation rows follow the marker. |
| 504 | // Chained bare rows: one toggle per <more> section. |
| 417 | 505 | 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 |
| 427 | 523 | } |
| 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++ |
| 432 | 526 | continue |
| 433 | 527 | } |
| 434 | 528 | |
| 435 | 529 | let cols = countTableCols(tokens, i) |
| 436 | 530 | 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( |
| 439 | 534 | (t, idx) => idx > firstTr && t.type === 'tr_close' |
| 440 | 535 | ) |
| 441 | 536 | cols = countCells( |
| 442 | | kept, |
| 537 | firstKept, |
| 443 | 538 | firstTr, |
| 444 | | end === -1 ? kept.length : end, |
| 539 | end === -1 ? firstKept.length : end, |
| 445 | 540 | 'td_open' |
| 446 | 541 | ) |
| 447 | 542 | } |
| 448 | 543 | if (cols <= 0) cols = 1 |
| 449 | 544 | |
| 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 | } |
| 462 | 559 | |
| 463 | | // Move the toggle + bare rows inside the first table. |
| 464 | | const segment = tokens.slice(i + 1, j + 1) |
| 465 | 560 | const merged = tokens.slice(0, i) |
| 466 | 561 | merged.push(...segment) |
| 467 | 562 | merged.push(tokens[i]) |
| 468 | | merged.push(...tokens.slice(j + 1)) |
| 563 | merged.push(...tokens.slice(j)) |
| 469 | 564 | tokens.splice(0, tokens.length, ...merged) |
| 470 | 565 | i += segment.length + 1 |
| 471 | 566 | continue |