# 表格(拓展)

GFM 启用了 表格 扩展,其中可以使用其他类型的叶子块。

表格 (opens new window)是具有行和列的数据排列,由单个标题行、分隔符、将标题与数据分隔开的分隔符行 (opens new window),以及零个或多个数据行组成。

每行由包含任意文本的单元格组成,其中的内容会被当做内联 (opens new window)解析,由管道(|)分隔。为了清晰阅读,还建议使用前置和后置管道,否则会让解析产生歧义。管道和单元格内容之间的空间会被修剪。块级元素不能插入表中。

分隔符行 (opens new window)由单元格组成,这些单元格的唯一内容是连字符(-),可选的前置或后置冒号(:)或两者兼有,分别表示左,右或中心对齐。

示例 198

Markdown HTML 效果
| 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>

一列中的单元格不需要长度一致,虽然那样它们更容易阅读。同样,使用前置管道和后置管道也可能不一致:

示例 199

Markdown HTML 效果
| abc | defghi |
:-: | -----------:
bar | baz

<table>
<thead>
<tr>
<th align="center">abc</th>
<th align="right">defghi</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">bar</td>
<td align="right">baz</td>
</tr>
</tbody>
</table>

通过转义,在单元格的内容中可以包含管道,可以包括内部的其他内联 span 标签:

示例 200

Markdown HTML 效果
| 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>

表格在第一个空行或另一个块级结构的开头处会被破坏:

示例 201

Markdown HTML 效果
| 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>

示例 202

Markdown HTML 效果
| abc | def |
| --- | --- |
| bar | baz |
bar

bar

<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
<tr>
<td>bar</td>
<td></td>
</tr>
</tbody>
</table>
<p>bar</p>

标题行必须与单元格数中的分隔符行 (opens new window)匹配。如果没有,表格将无法被识别:

示例 203

Markdown HTML 效果
| abc | def |
| --- |
| bar |

<p>| abc | def |
| --- |
| bar |</p>

表格行的其余部分可以在单元格数量上有所不同。如果有多个单元格小于标题行中的单元格数,则插入空单元格。如果有多的,则忽略多余的:

示例 204

Markdown HTML 效果
| 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>

如果正文中没有行,则 HTML 输出中不会生成 <tbody>

示例 205

Markdown HTML 效果
| abc | def |
| --- | --- |

<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
</table>