Skip to content

Commit

Permalink
fix: loosen table row parsing regex
Browse files Browse the repository at this point in the history
Fixes #625
  • Loading branch information
quantizor committed Nov 20, 2024
1 parent 713ce59 commit e52c19f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-cheetahs-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Arbitrary HTML no longer punches out pipes when parsing rows. If you absolutely need a pipe character that isn't a table separator, either escape it or enclose it in backticks to trigger inline code handling.
49 changes: 49 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,55 @@ describe('GFM tables', () => {
`)
})

it('regression #625 - processes self-closing HTML inside of a table row', () => {
render(
compiler(theredoc`
| col1 | col2 | col3 |
|------|-----------------|------------------|
| col1 | <custom-element>col2</custom-element><br> col2 | <custom-element>col3</custom-element><br>col3 |
`)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<table>
<thead>
<tr>
<th>
col1
</th>
<th>
col2
</th>
<th>
col3
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
col1
</td>
<td>
<custom-element>
col2
</custom-element>
<br>
col2
</td>
<td>
<custom-element>
col3
</custom-element>
<br>
col3
</td>
</tr>
</tbody>
</table>
`)
})

it('processes markdown inside of a table row when a preceeding column contains HTML', () => {
render(
compiler(theredoc`
Expand Down
4 changes: 2 additions & 2 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ function parseTableRow(
state.inTable = true
let tableRow = source
.trim()
// isolate situations where a pipe should be ignored (inline code, HTML)
.split(/( *(?:`[^`]*`|<.*?>.*?<\/.*?>(?!<\/.*?>)|\\\||\|) *)/)
// isolate situations where a pipe should be ignored (inline code, escaped, etc)
.split(/( *(?:`[^`]*`|\\\||\|) *)/)
.reduce((nodes, fragment) => {
if (fragment.trim() === '|')
nodes.push(
Expand Down

0 comments on commit e52c19f

Please sign in to comment.