Skip to content

Escape pipe characters in table cells#267

Open
gaoflow wants to merge 1 commit into
matthewwithanm:developfrom
gaoflow:fix-table-cell-pipe-escaping
Open

Escape pipe characters in table cells#267
gaoflow wants to merge 1 commit into
matthewwithanm:developfrom
gaoflow:fix-table-cell-pipe-escaping

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Cell content containing a literal | is emitted unescaped, so the pipe is parsed as a column delimiter and the table gains spurious columns:

>>> md('<table><tr><th>A</th><th>B</th></tr><tr><td>1|2</td><td>3</td></tr></table>')
'\n\n| A | B |\n| --- | --- |\n| 1|2 | 3 |\n\n'

(three data cells under a two-column header, and everything after the stray pipe lands in the wrong column)

This escapes | as \| in convert_td/convert_th, skipping pipes that escape_misc=True has already escaped. It's done in the cell converters rather than in escape() so pipes inside code spans are covered too: GFM splits rows into cells before inline parsing, so `1|2` also breaks the row and has to be written as `1\|2` (checked against cmark-gfm via pandoc, which escapes cell pipes the same way).

A literal '|' in td/th content is parsed by GFM as a column
delimiter, adding spurious columns and corrupting the table.
Escape it as '\|' unless it is already escaped (escape_misc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant