A table arranges content in rows and columns. The first row is a header row, and each row after it holds data. Use tables for information that readers compare across rows or columns.
Tables originate in GitHub Flavored Markdown. This document describes Comrak’s tables, which can differ from GitHub’s. GitLab uses Comrak’s tables, so this document is also the canonical spec for GitLab Flavored Markdown tables.
To enable tables, pass -e table (or --gfm) on the command line, or set extension.table in the options struct.
Here’s a typical table:
$ comrak -e table -t commonmark -t xml
| Fruit | Colour | Price |
|---|---|---|
| Apple | Red | 1.20 |
| Banana | Yellow | 0.50 |
Fruit
Colour
Price
Apple
Red
1.20
Banana
Yellow
0.50
Fruit
Colour
Price
Apple
Red
1.20
Banana
Yellow
0.50
Syntax
A table row is a line divided into cells by pipes (|), except pipes that are part of a backslash escape. A leading pipe and a trailing pipe are optional. A table row must contain at least one cell. A cell’s content is the text between its pipes, without leading or trailing spaces or tabs.
A delimiter row is a table row whose cells each consist of one or more hyphens (-), optionally preceded by a colon (:), and optionally followed by a colon.
A table begins when the last line of a paragraph is a table row and the next line is a delimiter row with the same number of cells. That last line becomes the table’s header row, and any earlier lines remain a paragraph. Each later line that is a table row becomes a body row, until the table ends.
The simplest table has one column and no body rows:
$ comrak -e table -t commonmark -t xml
| a |
|---|
a
a
Delimiter rows
Colons in a delimiter row set each column’s alignment. A leading colon aligns the column left, a trailing colon aligns it right, and both colons centre it. A cell with no colons leaves the column unaligned:
$ comrak -e table -t commonmark -t xml
| a | b | c | d |
|---|---|---|---|
| e | f | g | h |
a
b
c
d
e
f
g
h
a
b
c
d
e
f
g
h
None of the following are delimiter rows:
$ comrak -e table -t commonmark -t xml
| a | | |
| b | | : |
| c | | :: |
| d | | - - |
| e | | -:- |
| a |
| |
| b |
| : |
| c |
| :: |
| d |
| - - |
| e |
| -:- |
| a |
| |
| b |
| : |
| c |
| :: |
| d |
| - - |
| e |
| -:- |
A delimiter row may be indented up to three spaces. With four spaces, the line is paragraph continuation text instead:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| b | | - |
a
| b |
| - |
a
| b |
| - |
Pipes
Leading and trailing pipes are optional, and rows don’t need to agree on them:
$ comrak -e table -t commonmark -t xml
| abc | defghi |
|---|---|
| bar | baz |
abc
defghi
bar
baz
abc
defghi
bar
baz
A delimiter row for a single column needs a pipe or a colon:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| b |
|---|
a
b
a
b
Without one, a line of hyphens is a setext heading underline, even if the line above has pipes:
$ comrak -e table -t commonmark -t xml
a
| b |
a
| b |
a
| b |
A delimiter row can’t begin with a hyphen and a space, because that begins a list item:
$ comrak -e table -t commonmark -t xml
a | b
- | -
a | b
| -
a | b
| -
Header rows
The header row must have the same number of cells as the delimiter row. Otherwise, the lines don’t form a table:
$ comrak -e table -t commonmark -t xml
| a | b | | - |
| a | b |
| - |
| a | b |
| - |
Earlier lines of the paragraph remain a paragraph:
$ comrak -e table -t commonmark -t xml
123 456
| a | b |
|---|---|
| d | e |
123
456
a
b
d
e
123
456
a
b
d
e
Earlier lines remain a paragraph even if they contain pipes:
$ comrak -e table -t commonmark -t xml
| x | y |
| a | b |
|---|
| x | y |
a
b
| x | y |
a
b
The delimiter row can’t be a lazy continuation line:
$ comrak -e table -t commonmark -t xml
| a | | - |
| a |
| - |
| a |
| - |
Cell content
Leading and trailing spaces and tabs aren’t part of a cell’s content:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|
a
b
a
b
Cells contain inline content only. Text that would begin a block elsewhere is literal:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|---|
| > c | # d |
a
b
> c
# d
a
b
> c
# d
Comrak divides a row into cells before it parses inline content, so a pipe separates cells even inside a code span or a link:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|---|
| `c | d` |
| [e | f](/u) |
a
b
`c
d`
[e
f](/u)
a
b
`c
d`
[e
f](/u)
A backslash before a pipe makes the pipe part of the cell’s content. This also works inside a code span:
$ comrak -e table -t commonmark -t xml
| f|oo |
|---|
b | az |
| b | im |
f|oo
b | az
b | im
f|oo
b
|
az
b
|
im
A backslash escapes whatever character follows it. After an escaped backslash (\\), a pipe still separates cells:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|---|
| c\ | d |
| e\| f |
a
b
c\
d
e\| f
a
b
c\
d
e\| f
Body rows
A body row with fewer cells than the header row gets an empty added cell at the end for each missing cell. A body row with more cells loses the extra ones:
$ comrak -e table -t commonmark -t xml
| abc | def |
|---|---|
| bar | |
| bar | baz |
abc
def
bar
bar
baz
abc
def
bar
bar
baz
A line without pipes is a body row with one cell:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|---|
| bar | |
| baz |
a
b
bar
baz
a
b
bar
baz
A line of two pipes is a body row with two empty cells. A single pipe has no cells, so it isn’t a table row and ends the table:
$ comrak -e table -t commonmark -t xml
| a | b |
|---|---|
|
a
b
|
a
b
|
A second header row and delimiter row become ordinary body rows:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| b |
| - |
a
b
-
a
b
-
A blank line ends the table:
$ comrak -e table -t commonmark -t xml
| a |
|---|
ok
a
ok
a
ok
So does the beginning of any other block, such as a block quote, an ATX heading, a list item, a fenced code block, a thematic break, or an HTML block:
$ comrak -e table -t commonmark -t xml
| abc | def |
|---|---|
| bar | baz |
bar
abc
def
bar
baz
bar
abc
def
bar
baz
bar
$ comrak -e table -t commonmark -t xml
| a |
|---|
b
a
b
a
b
$ comrak -e table -t commonmark -t xml
| a |
|---|
- b
a
b
-
a
b
$ comrak -e table -t commonmark -t xml
| a |
|---|
b
a
b
a
b
$ comrak -e table -t commonmark -t xml
| a |
|---|
a
a
$ comrak -e table --unsafe -t commonmark -t xml
| a |
|---|
a
b
a
<div>
b
</div>
A line of hyphens after a body row is a thematic break, not a body row:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| b |
a
b
a
b
A line indented four or more spaces begins an indented code block:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| b |
c
a
b
c
a
b
c
A line of = characters is a body row, because only a paragraph can have a setext heading underline:
$ comrak -e table -t commonmark -t xml
| a |
|---|
| === |
a
===
a
===
Tables in containers
Tables may appear in any container, such as a list item or a block quote:
$ comrak -e table -t commonmark -t xml
-
a b c d
a
b
c
d
-
a
b
c
d
$ comrak -e table -t commonmark -t xml
a b c d
a
b
c
d
a
b
c
d
A table can interrupt a paragraph in a container:
$ comrak -e table -t commonmark -t xml
-
- foo
a
foo
a
-
foo
a
A line indented less than its list item’s content ends both the table and the list item:
$ comrak -e table -t commonmark -t xml
-
a
| b |
a
| b |
-
a
| b |
Body rows can’t be lazy continuation lines:
$ comrak -e table -t commonmark -t xml
a
| b |
a
| b |
a
| b |
Output
HTML output puts the header row in a <thead> element and the body rows in a <tbody> element. Header cells are <th> elements, and body cells are <td> elements. The cells of an aligned column have an align attribute. Added cells take their column’s alignment:
$ comrak -e table -t commonmark -t xml
| x | b | x | x | e |
|---|---|---|---|---|
| f | x | x | i |
x
b
x
x
e
f
x
x
i
x
b
x
x
e
f
x
x
i
A table with no body rows has no <tbody>:
$ comrak -e table -t commonmark -t xml
| abc | def |
|---|
abc
def
abc
def
CommonMark output writes each delimiter cell as ---, :--, --:, or :-:, and doesn’t pad cells to line up. It writes added cells out as empty cells. --width doesn’t wrap table rows:
$ comrak -e table --width 10 -t commonmark -t xml
| a | b |
|---|---|
| one two three four |
a
b
one two three four
a
b
one two three four
In XML output, a table is a table element. It contains a table_header element for the header row and a table_row element for each body row. Each cell is a table_cell element. Header cells in aligned columns have an align attribute of left, right, or center.
In the AST, a table is a NodeValue::Table. Its NodeTable holds each column’s alignment, and the numbers of columns, rows, and cells parsed from the source. The cell count excludes added cells. Each row is a NodeValue::TableRow, whose NodeTableRow’s header field is true for the header row. Each cell is a NodeValue::TableCell.
The source position of an added cell is an empty range, just after the row’s last cell in the source:
$ comrak -e table --sourcepos -t commonmark -t xml
| a | b |
|---|---|
| c |
a
b
c
a
b
c
Interactions
--gfm enables tables, along with the other GitHub Flavored Markdown extensions:
$ comrak --gfm -t commonmark -t xml
| a |
|---|
a
a
With task lists and --tasklist-in-table, a cell whose only content is a task list item marker becomes a checkbox. For details, see Task lists in tables.
With spoilers, the || delimiter doesn’t separate cells:
$ comrak -e table -e spoiler -t commonmark -t xml
| a | b |
|---|---|
| c | d |
a
b
c
d
a
b
c
d
With wikilinks, escape the pipe in a wikilink. Otherwise, it separates cells:
$ comrak -e table -e wikilinks-title-after-pipe -t commonmark -t xml
The same applies to a pipe in dollar math:
$ comrak -e table -e math-dollars -t commonmark -t xml
| a | b |
|---|---|
| $c | d$ |
| c|d |
a
b
$c
d$
c|d
a
b
$c
d$
c|d
Footnote references work in cells:
$ comrak -e table -e footnotes -t commonmark -t xml
With grid tables also enabled, both kinds of table work in the same document:
$ comrak -e table -e grid-table -t commonmark -t xml
| a |
| b |
|---|
a
b
a
b
Differences from cmark-gfm
cmark-gfm, the reference implementation of GitHub Flavored Markdown, uses the same table design as Comrak. It doesn’t support the other extensions in Interactions, so those examples aren’t comparable. On every other example above, the two implementations agree on HTML and XML output, except that cmark-gfm:
- treats a pipe after an escaped backslash as escaped, so
| c\\| d |is one cell that containsc| d(example 18); - treats a vertical tab (U+000B) or a form feed (U+000C) as whitespace around cells, including in delimiter rows;
- gives an added cell a source position in column 0 (example 41); and,
- when a table interrupts a paragraph, gives the paragraph the source position
0:0-0:0, places the header row on the paragraph’s first line, and ignores the indentation of body rows (example 48, example 49).
With --width, cmark-gfm’s CommonMark output wraps cell content across lines, which breaks the table (example 40). cmark-gfm also has a --table-prefer-style-attributes option, which aligns cells with style attributes instead of align attributes; Comrak has no equivalent.
Regressions
The following examples guard against regressions.
Source positions of rows indented differently from the header row (#862):
$ comrak -e table --sourcepos -t commonmark -t xml
p
| a | b |
|---|---|
| c | d |
| e |
p
a
b
c
d
e
p
a
b
c
d
e
Source positions of a preceding paragraph whose lines begin with spaces (#651):
$ comrak -e table --sourcepos -t commonmark -t xml
123
456
123 456
| a | b |
|---|---|
| c | d |
123
456
a
b
c
d
123
456
a
b
c
d