1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
## Tables (extension)
GFM enables the `table` extension, where an additional leaf block type is
available.
A [table](@) is an arrangement of data with rows and columns, consisting of a
single header row, a [delimiter row] separating the header from the data, and
zero or more data rows.
Each row consists of cells containing arbitrary text, in which [inlines] are
parsed, separated by pipes (`|`). A leading and trailing pipe is also
recommended for clarity of reading, and if there's otherwise parsing ambiguity.
Spaces between pipes and cell content are trimmed. Block-level elements cannot
be inserted in a table.
The [delimiter row](@) consists of cells whose only content are hyphens (`-`),
and optionally, a leading or trailing colon (`:`), or both, to indicate left,
right, or center alignment respectively.
```````````````````````````````` example
| foo | bar |
| --- | --- |
| baz | bim |
.
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>baz</td>
<td>bim</td>
</tr>
</tbody>
</table>
````````````````````````````````
Cells in one column don't need to match length, though it's easier to read if
they are. Likewise, use of leading and trailing pipes may be inconsistent:
```````````````````````````````` example
| abc | defghi |
:-: | -----------:
bar | baz
.
<table>
<thead>
<tr>
<th style="text-align: center;">abc</th>
<th style="text-align: right;">defghi</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">bar</td>
<td style="text-align: right;">baz</td>
</tr>
</tbody>
</table>
````````````````````````````````
Include a pipe in a cell's content by escaping it, including inside other
inline spans:
```````````````````````````````` example
| f\|oo |
| ------ |
| b `\|` az |
| b **\|** im |
.
<table>
<thead>
<tr>
<th>f|oo</th>
</tr>
</thead>
<tbody>
<tr>
<td>b <code>|</code> az</td>
</tr>
<tr>
<td>b <strong>|</strong> im</td>
</tr>
</tbody>
</table>
````````````````````````````````
The table is broken at the first line not containing an
unescaped `|`:
```````````````````````````````` example
| abc | def |
| --- | --- |
| bar | baz |
> bar
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>
<blockquote>
<p>bar</p>
</blockquote>
````````````````````````````````
```````````````````````````````` example
| abc | def |
| --- | --- |
| bar | baz |
bar
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>
<p>bar</p>
````````````````````````````````
```````````````````````````````` example
| abc | def |
| --- | --- |
| bar | baz |
bar
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>
<p>bar</p>
````````````````````````````````
The header row must match the [delimiter row] in the number of cells. If not,
a table will not be recognized:
```````````````````````````````` example
| abc | def |
| --- |
| bar |
.
<p>| abc | def |
| --- |
| bar |</p>
````````````````````````````````
The remainder of the table's rows may vary in the number of cells. If there
are a number of cells fewer than the number of cells in the header row, empty
cells are inserted. If there are greater, the excess is ignored:
```````````````````````````````` example
| abc | def |
| --- | --- |
| bar |
| bar | baz | boo |
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td></td>
</tr>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>
````````````````````````````````
If there are no rows in the body, no `<tbody>` is generated in HTML output:
```````````````````````````````` example
| abc | def |
| --- | --- |
.
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
</table>
````````````````````````````````
Here are some non-tables:
```````````````````````````````` example
| Not enough table | to be considered table |
| Not enough table | to be considered table |
| Not enough table | to be considered table |
| ---- | --- |
.
<p>| Not enough table | to be considered table |</p>
<p>| Not enough table | to be considered table |
| Not enough table | to be considered table |</p>
<p>| ---- | --- |</p>
````````````````````````````````
A table may be indented up to three spaces:
```````````````````````````````` example
a | b | c
- | - | -
.
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
</table>
````````````````````````````````
```````````````````````````````` example
a | b | c
- | - | -
.
<pre><code>a | b | c
- | - | -
</code></pre>
````````````````````````````````
Pipe tables have exactly one header row, and do not interrupt paragraphs.
```````````````````````````````` example
| Too much table | to be considered table |
| Too much table | to be considered table |
|----------------|------------------------|
| Too much table | to be considered table |
.
<p>| Too much table | to be considered table |
| Too much table | to be considered table |
|----------------|------------------------|
| Too much table | to be considered table |</p>
````````````````````````````````
Other block structures, like headers, have higher priority than tables.
Tables can be nested in other elements, but don't benefit from laziness.
```````````````````````````````` example
# abc | def
------|-----
> abc | def
> ----|-----
> abc | def
----|-----
.
<h1>abc | def</h1>
<p>------|-----</p>
<blockquote>
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
</table>
</blockquote>
<blockquote>
<p>abc | def
----|-----</p>
</blockquote>
````````````````````````````````
As a special case, pipes in inline code in tables are escaped
with backslashes.
The parsing rule for CommonMark is that block structures are parsed before
inline structures are. Normally, this means backslashes aren't allowed to have
any effect on block structures at all. Tables do consider backslashes, but
not the same way inline syntax does.
The table parser, which runs as part of the block structure parsing stage, splits rows into cells on unescaped `|` symbols, and it replaces `\|` with a literal `|`. Then it passes the contents of each cell separately to the inline parser. This means backslash escaping pipes works even in code spans (where backslashes are usually treated as literal text), but it can also result in some counterintuitive results.
For example, consider these two rows, excerpted from the below test case:
| Wait, what? | \|
| Wait, what? | \\|
The table parser only pays attention to the backslash that is immediately followed by a pipe. This means it sees the second row as this:
| Wait, what? | \\|
-^^ escaped pipe found here
|
literal backslash with no special block-level semantics
And the inline parser is given the first backslash, followed by the pipe:
\|
The inline parser then sees it as an backslash-escaped pipe, and writes the `|` on its own. Both rows result in exactly the same HTML.
<tr>
<td>Wait, what?</td>
<td>|</td>
</tr>
<tr>
<td>Wait, what?</td>
<td>|</td>
</tr>
You need to write `\\\|` to actually get `\|` to show up in the output.
This rule should be identical to GitHub's. See
<https://gist.github.com/notriddle/c027512ee849f12098fec3a3256c89d3>
for what they do.
This changes the behavior from what older versions of commonmark-extensions do,
but it fixes some expressiveness holes that hit older versions.
```````````````````````````````` example
| Description | Test case |
|-------------|-----------|
| Single | `\` |
| Double | `\\` |
| Basic test | `\|` |
| Basic test 2| `\|\|\` |
| Basic test 3| `x\|y\|z\`|
| Not pipe | `\.` |
| Combo | `\.\|\` |
| Extra | `\\\.` |
| Wait, what? | `\\|` |
| Wait, what? | `\\\|` |
| Wait, what? | `\\\\|` |
| Wait, what? | `\\\\\|` |
| Wait, what? | \|
| Wait, what? | \\|
| Wait, what? | \\\|
| Wait, what?x| \|x
| Wait, what?x| \\|x
| Wait, what?x| \\\|x
.
<table>
<thead>
<tr>
<th>Description</th>
<th>Test case</th>
</tr>
</thead>
<tbody>
<tr>
<td>Single</td>
<td><code>\</code></td>
</tr>
<tr>
<td>Double</td>
<td><code>\\</code></td>
</tr>
<tr>
<td>Basic test</td>
<td><code>|</code></td>
</tr>
<tr>
<td>Basic test 2</td>
<td><code>||\</code></td>
</tr>
<tr>
<td>Basic test 3</td>
<td><code>x|y|z\</code></td>
</tr>
<tr>
<td>Not pipe</td>
<td><code>\.</code></td>
</tr>
<tr>
<td>Combo</td>
<td><code>\.|\</code></td>
</tr>
<tr>
<td>Extra</td>
<td><code>\\\.</code></td>
</tr>
<tr>
<td>Wait, what?</td>
<td><code>\|</code></td>
</tr>
<tr>
<td>Wait, what?</td>
<td><code>\\|</code></td>
</tr>
<tr>
<td>Wait, what?</td>
<td><code>\\\|</code></td>
</tr>
<tr>
<td>Wait, what?</td>
<td><code>\\\\|</code></td>
</tr>
<tr>
<td>Wait, what?</td>
<td>|</td>
</tr>
<tr>
<td>Wait, what?</td>
<td>|</td>
</tr>
<tr>
<td>Wait, what?</td>
<td>\|</td>
</tr>
<tr>
<td>Wait, what?x</td>
<td>|x</td>
</tr>
<tr>
<td>Wait, what?x</td>
<td>|x</td>
</tr>
<tr>
<td>Wait, what?x</td>
<td>\|x</td>
</tr>
</tbody>
</table>
````````````````````````````````
|