Commit a604eb33d1
Unsigned
Layout: unified · split
docs/.vitepress/configs/markdown/tableMore.ts +119 −20
| @@ -51,30 +51,107 @@ function isDelimiterRow(line: string): boolean { | ||
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | function colspanOf(td: any): number { |
| 54 | const cols = td ? td.attrGet('colspan') : null | |
| 55 | return cols === null ? 1 : cols | |
| 54 | const cols = td ? Number(td.attrGet('colspan')) : NaN | |
| 55 | return Number.isFinite(cols) && cols > 0 ? Math.floor(cols) : 1 | |
| 56 | } | |
| 57 | ||
| 58 | function rowspanOf(td: any): number { | |
| 59 | const rs = td ? Number(td.attrGet('rowspan')) : NaN | |
| 60 | return Number.isFinite(rs) && rs > 0 ? Math.floor(rs) : 1 | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | function prevRowCells(tokens: any[]): any[] { |
| 59 | 64 | let end = tokens.length - 1 |
| 60 | while ( | |
| 61 | end >= 0 && | |
| 62 | (tokens[end].type === 'table_close' || tokens[end].type === 'tbody_close') | |
| 63 | ) end-- | |
| 64 | if (end < 0 || tokens[end].type !== 'tr_close') return [] | |
| 65 | const cells: any[] = [] | |
| 66 | let k = end - 1 | |
| 67 | while (k >= 0 && tokens[k].type !== 'tr_open') { | |
| 68 | if (tokens[k].type === 'td_open' || tokens[k].type === 'th_open') { | |
| 69 | cells.unshift(tokens[k]) | |
| 65 | while (end >= 0 && tokens[end].type === 'table_close') end-- | |
| 66 | if (end < 0 || tokens[end].type !== 'tbody_close') return [] | |
| 67 | let open = end | |
| 68 | let depth = 0 | |
| 69 | while (open >= 0) { | |
| 70 | if (tokens[open].type === 'tbody_close') depth++ | |
| 71 | else if (tokens[open].type === 'tbody_open') { | |
| 72 | depth-- | |
| 73 | if (depth === 0) break | |
| 70 | 74 | } |
| 71 | k-- | |
| 75 | open-- | |
| 72 | 76 | } |
| 73 | const cols: any[] = [] | |
| 74 | for (const td of cells) { | |
| 75 | for (let w = 0; w < colspanOf(td); w++) cols.push(td) | |
| 77 | if (open < 0) return [] | |
| 78 | const rows: any[][] = [] | |
| 79 | let cur: any[] | null = null | |
| 80 | for (let k = open + 1; k < end; k++) { | |
| 81 | const t = tokens[k] | |
| 82 | if (t.type === 'tr_open') cur = [] | |
| 83 | else if (t.type === 'tr_close') { | |
| 84 | if (cur) rows.push(cur) | |
| 85 | cur = null | |
| 86 | } else if ( | |
| 87 | cur && (t.type === 'td_open' || t.type === 'th_open') | |
| 88 | ) { | |
| 89 | cur.push(t) | |
| 90 | } | |
| 91 | } | |
| 92 | const remaining: number[] = [] | |
| 93 | const holder: any[] = [] | |
| 94 | let owners: any[] = [] | |
| 95 | for (const row of rows) { | |
| 96 | const next: any[] = [] | |
| 97 | let c = 0 | |
| 98 | for (const td of row) { | |
| 99 | while ((remaining[c] ?? 0) > 0) { | |
| 100 | next[c] = holder[c] | |
| 101 | c++ | |
| 102 | } | |
| 103 | const w = colspanOf(td) | |
| 104 | for (let k = 0; k < w; k++) next[c + k] = td | |
| 105 | const rs = rowspanOf(td) | |
| 106 | if (rs > 1) { | |
| 107 | for (let k = 0; k < w; k++) { | |
| 108 | remaining[c + k] = rs | |
| 109 | holder[c + k] = td | |
| 110 | } | |
| 111 | } | |
| 112 | c += w | |
| 113 | } | |
| 114 | while ((remaining[c] ?? 0) > 0) { | |
| 115 | next[c] = holder[c] | |
| 116 | c++ | |
| 117 | } | |
| 118 | for (let k = 0; k < remaining.length; k++) { | |
| 119 | if (remaining[k] > 0) remaining[k]-- | |
| 120 | } | |
| 121 | owners = next | |
| 122 | } | |
| 123 | return owners | |
| 124 | } | |
| 125 | ||
| 126 | function cloneCellContent(state: any, owner: any) { | |
| 127 | const tokens = state.tokens | |
| 128 | const idx = tokens.indexOf(owner) | |
| 129 | if (idx === -1) return | |
| 130 | const end = tokens.length | |
| 131 | let depth = 1 | |
| 132 | for (let k = idx + 1; k < end && depth > 0; k++) { | |
| 133 | const t = tokens[k] | |
| 134 | const isOpen = (t.type === 'td_open' || t.type === 'th_open') && | |
| 135 | t.nesting === 1 | |
| 136 | const isClose = (t.type === 'td_close' || t.type === 'th_close') && | |
| 137 | t.nesting === -1 | |
| 138 | if (isOpen) { | |
| 139 | depth++ | |
| 140 | } else if (isClose) { | |
| 141 | depth-- | |
| 142 | if (depth === 0) break | |
| 143 | } else { | |
| 144 | const cp = state.push(t.type, t.tag, t.nesting) | |
| 145 | cp.attrs = t.attrs ? t.attrs.map((a: any) => [a[0], a[1]]) : t.attrs | |
| 146 | cp.content = t.content | |
| 147 | cp.map = t.map ? t.map.slice() : t.map | |
| 148 | cp.children = [] | |
| 149 | cp.block = t.block | |
| 150 | cp.info = t.info | |
| 151 | cp.markup = t.markup | |
| 152 | cp.meta = t.meta ? { ...t.meta } : t.meta | |
| 153 | } | |
| 76 | 154 | } |
| 77 | return cols | |
| 78 | 155 | } |
| 79 | 156 | |
| 80 | 157 | function findTableClose(tokens: any[], from: number): number { |
| @@ -165,6 +242,7 @@ export function tableMorePlugin(md: MarkdownIt): void { | ||
| 165 | 242 | const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2) |
| 166 | 243 | |
| 167 | 244 | let up: any[] = prevRowCells(state.tokens) |
| 245 | const emitted = new Set(state.tokens) | |
| 168 | 246 | const marker = state.push('table_more', '', 0) |
| 169 | 247 | marker.map = [startLine, startLine + 1] |
| 170 | 248 | marker.content = '' |
| @@ -196,10 +274,31 @@ export function tableMorePlugin(md: MarkdownIt): void { | ||
| 196 | 274 | if (raw.trim() === '^^' && c < up.length && up[c]) { |
| 197 | 275 | flush() |
| 198 | 276 | const above = up[c] |
| 199 | const rs = above.attrGet('rowspan') | |
| 200 | above.attrSet('rowspan', rs === null ? 2 : rs + 1) | |
| 201 | 277 | const w = colspanOf(above) |
| 202 | for (let k = 0; k < w; k++) cur[c + k] = above | |
| 278 | if (emitted.has(above)) { | |
| 279 | const td = state.push( | |
| 280 | above.tag === 'th' ? 'th_open' : 'td_open', | |
| 281 | above.tag, | |
| 282 | 1 | |
| 283 | ) | |
| 284 | td.attrs = above.attrs | |
| 285 | ? above.attrs | |
| 286 | .filter((a: any) => a[0] !== 'rowspan') | |
| 287 | .map((a: any) => [a[0], a[1]]) | |
| 288 | : [] | |
| 289 | if (w > 1) td.attrSet('colspan', w) | |
| 290 | cloneCellContent(state, above) | |
| 291 | state.push( | |
| 292 | above.tag === 'th' ? 'th_close' : 'td_close', | |
| 293 | above.tag, | |
| 294 | -1 | |
| 295 | ) | |
| 296 | for (let k = 0; k < w; k++) cur[c + k] = td | |
| 297 | } else { | |
| 298 | const rs = rowspanOf(above) | |
| 299 | above.attrSet('rowspan', rs + 1) | |
| 300 | for (let k = 0; k < w; k++) cur[c + k] = above | |
| 301 | } | |
| 203 | 302 | c += w |
| 204 | 303 | continue |
| 205 | 304 | } |