Skip to content

Commit

Permalink
Handle escaped pipes in table header.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapsdz committed May 19, 2023
1 parent 6bf853e commit 4ed5239
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
15 changes: 11 additions & 4 deletions document.c
Original file line number Diff line number Diff line change
Expand Up @@ -3641,8 +3641,15 @@ parse_table_header(struct lowdown_node **np,
ssize_t pipes = 0;
struct lowdown_node *n;

while (i < size && data[i] != '\n')
if (data[i++] == '|')
/*
* Parse the number of cells in the header by looking at the
* number of delimiters (pipes). Disregard those on the
* beginning and end of the line, and escaped ones.
*/

for ( ; i < size && data[i] != '\n'; i++)
if (data[i] == '|' &&
(i == 0 || data[i - 1] != '\\'))
pipes++;

if (i == size || pipes == 0)
Expand All @@ -3655,8 +3662,8 @@ parse_table_header(struct lowdown_node **np,

if (data[0] == '|')
pipes--;

if (header_end && data[header_end - 1] == '|')
if (header_end && data[header_end - 1] == '|' &&
(header_end < 2 || data[header_end - 2] != '\\'))
pipes--;

if (pipes < 0)
Expand Down
15 changes: 15 additions & 0 deletions regress/table7.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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>
5 changes: 5 additions & 0 deletions regress/table7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

| f\|oo |
| ------ |
| b `\|` az |
| b **\|** im |
15 changes: 15 additions & 0 deletions regress/table8.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<table>
<thead>
<tr>
<th>f|oo |</th>
</tr>
</thead>
<tbody>
<tr>
<td>b az</td>
</tr>
<tr>
<td>b im</td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions regress/table8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

| f\|oo \|
| ------ |
| b az |
| b im |

0 comments on commit 4ed5239

Please sign in to comment.