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:

Example 1
$ comrak -e table
| 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:

Example 2
$ comrak -e table
| 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:

Example 3
$ comrak -e table
| a | b  | c  | d   |
| - | :- | -: | :-: |
| e | f  | g  | h   |

None of the following are delimiter rows:

Example 4
$ comrak -e table
| 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:

Example 5
$ comrak -e table
| a |
   | - |

| b |
    | - |

Pipes

Leading and trailing pipes are optional, and rows don’t need to agree on them:

Example 6
$ comrak -e table
| abc | defghi |
:-: | -----------:
bar | baz

A delimiter row for a single column needs a pipe or a colon:

Example 7
$ comrak -e table
a
|-

b
:-

Without one, a line of hyphens is a setext heading underline, even if the line above has pipes:

Example 8
$ comrak -e table
a
---

| b |
---

A delimiter row can’t begin with a hyphen and a space, because that begins a list item:

Example 9
$ comrak -e table
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:

Example 10
$ comrak -e table
| a | b |
| - |

Earlier lines of the paragraph remain a paragraph:

Example 11
$ comrak -e table
123
456
| a | b |
| ---| --- |
d | e

Earlier lines remain a paragraph even if they contain pipes:

Example 12
$ comrak -e table
| x | y |
| a | b |
| - | - |

The delimiter row can’t be a lazy continuation line:

Example 13
$ comrak -e table
> | a |
| - |

Cell content

Leading and trailing spaces and tabs aren’t part of a cell’s content:

Example 14
$ comrak -e table
|→a→|  b  |
|-|-|

Cells contain inline content only. Text that would begin a block elsewhere is literal:

Example 15
$ comrak -e table
| *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:

Example 16
$ comrak -e table
| 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:

Example 17
$ comrak -e table
| f\|oo  |
| ------ |
| b `\|` az |
| b **\|** im |

A backslash escapes whatever character follows it. After an escaped backslash (\\), a pipe still separates cells:

Example 18
$ comrak -e table
| 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:

Example 19
$ comrak -e table
| abc | def |
| --- | --- |
| bar |
| bar | baz | boo |

A line without pipes is a body row with one cell:

Example 20
$ comrak -e table
| 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:

Example 21
$ comrak -e table
| a | b |
| - | - |
||
|

A second header row and delimiter row become ordinary body rows:

Example 22
$ comrak -e table
| a |
| - |
| b | c |
| - | - |

A blank line ends the table:

Example 23
$ comrak -e table
| 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:

Example 24
$ comrak -e table
| abc | def |
| --- | --- |
| bar | baz |
> bar
Example 25
$ comrak -e table
| a |
| - |
# b
Example 26
$ comrak -e table
| a |
| - |
- b
Example 27
$ comrak -e table
| a |
| - |
```
b
```
Example 28
$ comrak -e table
| a |
| - |
***
Example 29
$ comrak -e table --unsafe
| a |
| - |
<div>
b
</div>

A line of hyphens after a body row is a thematic break, not a body row:

Example 30
$ comrak -e table
| a |
| - |
| b |
---

A line indented four or more spaces begins an indented code block:

Example 31
$ comrak -e table
| a |
| - |
| b |
    c

A line of = characters is a body row, because only a paragraph can have a setext heading underline:

Example 32
$ comrak -e table
| a |
| - |
===

Tables in containers

Tables may appear in any container, such as a list item or a block quote:

Example 33
$ comrak -e table
- |a|b|
  |-|-|
  |c|d|
Example 34
$ comrak -e table
> |a|b|
> |-|-|
> |c|d|

A table can interrupt a paragraph in a container:

Example 35
$ comrak -e table
- foo
  | a |
  | - |

A line indented less than its list item’s content ends both the table and the list item:

Example 36
$ comrak -e table
- | a |
  | - |
| b |

Body rows can’t be lazy continuation lines:

Example 37
$ comrak -e table
> | 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:

Example 38
$ comrak -e table
|x|b|x|x|e|
|:-|:-:|-|-:|:-:|
|f|x|x|i|

A table with no body rows has no <tbody>:

Example 39
$ comrak -e table
| 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:

Example 40
$ comrak -e table --width 10
| 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:

Example 41
$ comrak -e table --sourcepos
| a | b |
| - | - |
| c |

Interactions

--gfm enables tables, along with the other GitHub Flavored Markdown extensions:

Example 42
$ comrak --gfm
| 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:

Example 43
$ comrak -e table -e spoiler
| a | b |
| - | - |
| ||c|| | d |

With wikilinks, escape the pipe in a wikilink. Otherwise, it separates cells:

Example 44
$ comrak -e table -e wikilinks-title-after-pipe
| a | b |
| - | - |
| [[c|d]] |
| [[c\|d]] |

The same applies to a pipe in dollar math:

Example 45
$ comrak -e table -e math-dollars
| a | b |
| - | - |
| $c|d$ |
| $c\|d$ |

Footnote references work in cells:

Example 46
$ comrak -e table -e footnotes
| a |
| - |
| b[^1] |

[^1]: c

With grid tables also enabled, both kinds of table work in the same document:

Example 47
$ comrak -e table -e grid-table
+---+
| 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:

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):

Example 48
$ comrak -e table --sourcepos
p
      |a|b|
|-|-|
|c|d|
  |e|
#

Source positions of a preceding paragraph whose lines begin with spaces (#651):

Example 49
$ comrak -e table --sourcepos
 123
  456
| a | b |
| - | - |
| c | d |