Sashmark / Blog

Markdown table merge cells: grid spans in pandoc tests

Bobby Huang ·

The section Three small tables in the test folder prints the three input files and the six commands run on them. Under A grid cell across two columns, pandoc's HTML for a row holding one wide cell is quoted in full. Output for a cell with an open row line beneath it sits in A grid cell down two rows. An added test file, with a closing row drawn as a single cell, is covered in One cell across a whole row. Two more runs of the wide-cell file, and a look at commonmark's extension list, fill The same grid file without grid_tables. For a pipe row that ends in a doubled pipe, HTML from both readers appears in Two pipes in a row in a pipe table. Numbered pandoc and grep lines you can point at another file make up Steps to look for merged cells with pandoc.

Key takeaways

  • Cat output for span-col.md, span-row.md and span-pipe.md is printed in Three small tables in the test folder.
  • Look in A grid cell across two columns for the full HTML pandoc wrote from the wide-cell file.
  • Pandoc's HTML for the file with an open row line sits under A grid cell down two rows.
  • Counts taken on commonmark's list of extensions appear in The same grid file without grid_tables.
  • The two readers' HTML for a doubled pipe at a row's end is in Two pipes in a row in a pipe table.

This post tests merged cells in Markdown tables: one cell stretched across two columns, or down two rows. Pandoc's grid_tables extension did that in this test, and its HTML carried colspan and rowspan attributes. A pipe table given the same idea did something else, quoted under Two pipes in a row in a pipe table.

Each command here was typed on a Mac whose pandoc build calls itself 3.8.3. Three tiny files were fed to it through the markdown reader, and two of them through commonmark as well. Nothing quoted here reaches past that setup, and this post's own extra test repeated the runs a second time. For how to draw a grid table at all, see grid tables with multiline cells; the files here are already drawn.

Three small tables in the test folder

All three inputs are plain ASCII. Here is cat span-col.md:

+-------+-------+-------+
| Name  | Q1    | Q2    |
+=======+=======+=======+
| Jane  | Both quarters |
+-------+-------+-------+
| John  | 3     | 4     |
+-------+-------+-------+

The Jane row has no | between the Q1 and Q2 columns. One cell runs from the second column's left border to the third column's right border.

Here is cat span-row.md:

+-------+-------+
| Team  | Owner |
+=======+=======+
| Docs  | Jane  |
+       +-------+
|       | John  |
+-------+-------+

The row line under Docs has dashes in the Owner column and only spaces in the Team column. So the Team cell is left open at the bottom.

Here is cat span-pipe.md:

| Name | Q1 | Q2 |
|------|----|----|
| Jane | Both quarters ||
| John | 3  | 4  |

This one tries the same wide cell in a pipe table, by typing two pipes with nothing between them after Both quarters on the Jane row. Pipe table rules in general live in Markdown tables.

Six commands were run inside the test folder:

pandoc -f markdown -t html span-col.md
pandoc -f markdown -t html span-row.md
pandoc -f markdown-grid_tables -t html span-col.md
pandoc -f commonmark -t html span-col.md
pandoc -f markdown -t html span-pipe.md
pandoc -f commonmark+pipe_tables -t html span-pipe.md

Each one returned exit code 0 in the lead log and again in the extra test.

A grid cell across two columns

The markdown reader lists +grid_tables, so the extension was on for this command with no flag added:

pandoc -f markdown -t html span-col.md

Pandoc gave this HTML:

<table style="width:33%;">
<colgroup>
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
</colgroup>
<thead>
<tr>
<th>Name</th>
<th>Q1</th>
<th>Q2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jane</td>
<td colspan="2">Both quarters</td>
</tr>
<tr>
<td>John</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>

The Jane row holds two td elements where the John row holds three. The second one, <td colspan="2">Both quarters</td>, is the wide cell. The header row kept all three th elements, so the table still has three columns.

Above the rows sit a colgroup and a style on the table tag; column width in Markdown tables is where those numbers are looked at.

A grid cell down two rows

With the open row line in place, span-row.md was handed to the markdown reader as well:

pandoc -f markdown -t html span-row.md

The output:

<table style="width:22%;">
<colgroup>
<col style="width: 11%" />
<col style="width: 11%" />
</colgroup>
<thead>
<tr>
<th>Team</th>
<th>Owner</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Docs</td>
<td>Jane</td>
</tr>
<tr>
<td>John</td>
</tr>
</tbody>
</table>

Docs went into <td rowspan="2">, and the next tr holds a single td for John. The spaces in the Team column of the source never became a cell of their own. The body holds two rows, one per Owner name.

One cell across a whole row

The lead's files stop at a cell two slots wide, so this post's extra test brings in merge-wide.md, with a closing row that has no inner | at all:

+-------+-------+-------+
| Name  | Q1    | Q2    |
+=======+=======+=======+
| John  | 3     | 4     |
+-------+-------+-------+
| Doe Holdings total    |
+-------+-------+-------+

It ran through the same reader:

pandoc -f markdown -t html merge-wide.md

The top of the output matched the span-col.md run: the same table tag, colgroup and header row. Here is the tbody part, as pandoc wrote it:

<tbody>
<tr>
<td>John</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td colspan="3">Doe Holdings total</td>
</tr>
</tbody>

The last row became one td with colspan="3", the full width of the three columns. The row above it kept three separate cells. Across the two grid files, a wide cell took two slots in span-col.md and three in merge-wide.md.

The same grid file without grid_tables

Typing -grid_tables after the reader name turned the extension off for one run. That run, pandoc -f markdown-grid_tables -t html span-col.md, gave one paragraph of text and no table. Its text holds characters outside ASCII, so it isn't quoted here; the commonmark run below shows the plain-text result in ASCII.

The commonmark reader has no grid_tables entry in its extension list. In the saved list, grep -c grid counted 0 lines; the control, grep -c pipe_tables, counted 1. The fourth command was:

pandoc -f commonmark -t html span-col.md

It printed:

<p>+-------+-------+-------+ | Name | Q1 | Q2 |
+=======+=======+=======+ | Jane | Both quarters |
+-------+-------+-------+ | John | 3 | 4 | +-------+-------+-------+</p>

All seven source lines ended up in a single p element. The runs of spaces inside each cell shrank to one, and the output wraps across three lines. No colspan survived, since no table was built.

Two pipes in a row in a pipe table

The pipe_tables extension is on in the markdown list. The fifth command ran it on the pipe file:

pandoc -f markdown -t html span-pipe.md
<table>
<thead>
<tr>
<th>Name</th>
<th>Q1</th>
<th>Q2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jane</td>
<td>Both quarters</td>
<td></td>
</tr>
<tr>
<td>John</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>

The Jane row still has three td elements. Both quarters sits in the Q1 slot, and the third cell, <td></td>, is empty. There's no colspan anywhere in the output.

The commonmark list shows -pipe_tables, so the sixth command added it with a plus sign:

pandoc -f commonmark+pipe_tables -t html span-pipe.md

Its HTML was the same, line for line; the extra test ran cmp on the two saved files and got exit code 0. Two pipes typed together made an empty cell under both readers here. That agrees with the limits list in Markdown tables, which gives pipe tables no merged cells.

Steps to look for merged cells with pandoc

Every step below was run on the files above by this post's extra test. Swap in your file's name wherever a span- name appears.

  1. Save the HTML for a grid table: pandoc -f markdown -t html span-col.md -o merge-col.html.
  2. Count the wide cells with grep -c 'colspan' merge-col.html. The count came back as 1.
  3. Save the HTML for the tall cell: pandoc -f markdown -t html span-row.md -o merge-row.html.
  4. Find the tall cell with grep -n 'rowspan' merge-row.html. Its one match was 14:<td rowspan="2">Docs</td>.
  5. Try the other reader with pandoc -f commonmark -t html span-col.md -o merge-cm.html. A grep -c '<table' merge-cm.html on that file gave 0, against 1 from the same grep on merge-col.html.
  6. Check a pipe row with pandoc -f markdown -t html span-pipe.md -o merge-pipe.html. Looking for the empty cell, grep -c '<td></td>' merge-pipe.html found 1. With grep -c 'colspan' merge-pipe.html the count was 0.

Where Sashmark fits

Should Sashmark reach a first release on plan, you could fix a phrase directly where it is displayed. The Round-Trip target for that release: lines you never touched keep every byte they had. Changes View, also on the list for that version, is meant to flag what shifted since your last read, with no git setup.

It is in development and has not been released. The app itself has no AI features. See the planned features, the help pages and the roadmap for where each part stands.

More at sashmark.com.

Questions

Can a Markdown table merge cells?
Compare the grid runs in A grid cell across two columns with the pipe runs in Two pipes in a row in a pipe table.
Can one cell span two rows?
See A grid cell down two rows for pandoc's output on a tall cell.
What does commonmark do with a grid table?
That run is quoted, with an extension list check, in The same grid file without grid_tables.
Is there a quick way to find merged cells in a file?
Follow the numbered commands in Steps to look for merged cells with pandoc.

What Sashmark plans for v1: Features · Roadmap

View as Markdown