Commit ed490d90bc
Unsigned
Layout: unified · split
docs/.vitepress/configs/markdown/tableMore.ts +51 −3
| @@ -50,6 +50,33 @@ function isDelimiterRow(line: string): boolean { | |||
| 50 | cells.every((c) => /^:?-+:?$/.test(c.trim())) | 50 | cells.every((c) => /^:?-+:?$/.test(c.trim())) |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | function colspanOf(td: any): number { | ||
| 54 | const cols = td ? td.attrGet('colspan') : null | ||
| 55 | return cols === null ? 1 : cols | ||
| 56 | } | ||
| 57 | |||
| 58 | function prevRowCells(tokens: any[]): any[] { | ||
| 59 | 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]) | ||
| 70 | } | ||
| 71 | k-- | ||
| 72 | } | ||
| 73 | const cols: any[] = [] | ||
| 74 | for (const td of cells) { | ||
| 75 | for (let w = 0; w < colspanOf(td); w++) cols.push(td) | ||
| 76 | } | ||
| 77 | return cols | ||
| 78 | } | ||
| 79 | |||
| 53 | function findTableClose(tokens: any[], from: number): number { | 80 | function findTableClose(tokens: any[], from: number): number { |
| 54 | let depth = 0 | 81 | let depth = 0 |
| 55 | for (let j = from; j < tokens.length; j++) { | 82 | for (let j = from; j < tokens.length; j++) { |
| @@ -137,6 +164,7 @@ export function tableMorePlugin(md: MarkdownIt): void { | |||
| 137 | const next2 = line + 1 < endLine ? getLine(state, line + 1) : '' | 164 | const next2 = line + 1 < endLine ? getLine(state, line + 1) : '' |
| 138 | const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2) | 165 | const fullTableFollows = isPipeLine(next1) && isDelimiterRow(next2) |
| 139 | 166 | ||
| 167 | let up: any[] = prevRowCells(state.tokens) | ||
| 140 | const marker = state.push('table_more', '', 0) | 168 | const marker = state.push('table_more', '', 0) |
| 141 | marker.map = [startLine, startLine + 1] | 169 | marker.map = [startLine, startLine + 1] |
| 142 | marker.content = '' | 170 | marker.content = '' |
| @@ -151,16 +179,33 @@ export function tableMorePlugin(md: MarkdownIt): void { | |||
| 151 | const cells = splitCells(getLine(state, line)) | 179 | const cells = splitCells(getLine(state, line)) |
| 152 | const trOpen = state.push('tr_open', 'tr', 1) | 180 | const trOpen = state.push('tr_open', 'tr', 1) |
| 153 | trOpen.map = [line, line + 1] | 181 | trOpen.map = [line, line + 1] |
| 182 | const cur: any[] = [] | ||
| 183 | let c = 0 | ||
| 154 | let openTd: any = null | 184 | let openTd: any = null |
| 185 | let openStart = 0 | ||
| 155 | let span = 1 | 186 | let span = 1 |
| 156 | const flush = () => { | 187 | const flush = () => { |
| 157 | if (openTd && span > 1) openTd.attrJoin('colspan', String(span)) | 188 | if (openTd) { |
| 158 | openTd = null | 189 | if (span > 1) openTd.attrSet('colspan', span) |
| 159 | span = 1 | 190 | for (let k = 0; k < span; k++) cur[openStart + k] = openTd |
| 191 | openTd = null | ||
| 192 | span = 1 | ||
| 193 | } | ||
| 160 | } | 194 | } |
| 161 | for (const raw of cells) { | 195 | for (const raw of cells) { |
| 196 | if (raw.trim() === '^^' && c < up.length && up[c]) { | ||
| 197 | flush() | ||
| 198 | const above = up[c] | ||
| 199 | const rs = above.attrGet('rowspan') | ||
| 200 | above.attrSet('rowspan', rs === null ? 2 : rs + 1) | ||
| 201 | const w = colspanOf(above) | ||
| 202 | for (let k = 0; k < w; k++) cur[c + k] = above | ||
| 203 | c += w | ||
| 204 | continue | ||
| 205 | } | ||
| 162 | if (raw === '' && openTd) { | 206 | if (raw === '' && openTd) { |
| 163 | span++ | 207 | span++ |
| 208 | c++ | ||
| 164 | continue | 209 | continue |
| 165 | } | 210 | } |
| 166 | flush() | 211 | flush() |
| @@ -172,9 +217,12 @@ export function tableMorePlugin(md: MarkdownIt): void { | |||
| 172 | inline.children = [] | 217 | inline.children = [] |
| 173 | state.push('td_close', 'td', -1) | 218 | state.push('td_close', 'td', -1) |
| 174 | openTd = td | 219 | openTd = td |
| 220 | openStart = c | ||
| 221 | c++ | ||
| 175 | } | 222 | } |
| 176 | flush() | 223 | flush() |
| 177 | state.push('tr_close', 'tr', -1) | 224 | state.push('tr_close', 'tr', -1) |
| 225 | up = cur | ||
| 178 | rows++ | 226 | rows++ |
| 179 | line++ | 227 | line++ |
| 180 | } | 228 | } |