Skip to content

Commit 7603248

Browse files
committedApr 13, 2024
fix: table cell parsing isolation
1 parent f9328cc commit 7603248

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed
 

‎.changeset/few-kiwis-retire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'markdown-to-jsx': patch
3+
---
4+
5+
Fix parsing isolation of individual table cells.

‎index.compiler.spec.tsx

+60
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,66 @@ describe('GFM tables', () => {
23192319
</table>
23202320
`)
23212321
})
2322+
2323+
it('#568 handle inline syntax around table separators', () => {
2324+
render(compiler(`|_foo|bar_|\n|-|-|\n|1|2|`))
2325+
2326+
expect(root.innerHTML).toMatchInlineSnapshot(`
2327+
<table>
2328+
<thead>
2329+
<tr>
2330+
<th>
2331+
_foo
2332+
</th>
2333+
<th>
2334+
bar_
2335+
</th>
2336+
</tr>
2337+
</thead>
2338+
<tbody>
2339+
<tr>
2340+
<td>
2341+
1
2342+
</td>
2343+
<td>
2344+
2
2345+
</td>
2346+
</tr>
2347+
</tbody>
2348+
</table>
2349+
`)
2350+
})
2351+
2352+
it('#568 handle inline code syntax around table separators', () => {
2353+
render(compiler(`|\`foo|bar\`|baz|\n|-|-|\n|1|2|`))
2354+
2355+
expect(root.innerHTML).toMatchInlineSnapshot(`
2356+
<table>
2357+
<thead>
2358+
<tr>
2359+
<th>
2360+
<code>
2361+
foo|bar
2362+
</code>
2363+
</th>
2364+
<th>
2365+
baz
2366+
</th>
2367+
</tr>
2368+
</thead>
2369+
<tbody>
2370+
<tr>
2371+
<td>
2372+
1
2373+
</td>
2374+
<td>
2375+
2
2376+
</td>
2377+
</tr>
2378+
</tbody>
2379+
</table>
2380+
`)
2381+
})
23222382
})
23232383

23242384
describe('arbitrary HTML', () => {

‎index.tsx

+13-20
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,15 @@ function parseTableRow(
623623
): MarkdownToJSX.ParserResult[][] {
624624
const prevInTable = state.inTable
625625
state.inTable = true
626-
const tableRow = parse(source.trim(), state)
626+
let tableRow = source
627+
.trim()
628+
// isolate situations where a pipe should be ignored (inline code, HTML)
629+
.split(/( *(?:`[^`]*`|<.*?>.*?<\/.*?>(?!<\/.*?>)|\\\||\|) *)/)
630+
.reduce((nodes, fragment, index, arr) => {
631+
if (fragment.trim() === '|') nodes.push({ type: RuleType.tableSeparator })
632+
else if (fragment !== '') nodes.push.apply(nodes, parse(fragment, state))
633+
return nodes
634+
}, [] as MarkdownToJSX.ParserResult[])
627635
state.inTable = prevInTable
628636

629637
let cells = [[]]
@@ -1003,8 +1011,11 @@ function parseBlock(
10031011
children,
10041012
state: MarkdownToJSX.State
10051013
): MarkdownToJSX.ParserResult[] {
1014+
const isCurrentlyInline = state.inline || false
10061015
state.inline = false
1007-
return parse(children, state)
1016+
const result = parse(children, state)
1017+
state.inline = isCurrentlyInline
1018+
return result
10081019
}
10091020

10101021
const parseCaptureInline: MarkdownToJSX.Parser<{
@@ -1772,24 +1783,6 @@ export function compiler(
17721783
},
17731784
},
17741785

1775-
[RuleType.tableSeparator]: {
1776-
match: function (source, state) {
1777-
if (!state.inTable) {
1778-
return null
1779-
}
1780-
state.inline = true
1781-
return TABLE_SEPARATOR_R.exec(source)
1782-
},
1783-
order: Priority.HIGH,
1784-
parse: function () {
1785-
return { type: RuleType.tableSeparator }
1786-
},
1787-
// These shouldn't be reached, but in case they are, be reasonable:
1788-
render() {
1789-
return ' | '
1790-
},
1791-
},
1792-
17931786
[RuleType.text]: {
17941787
// Here we look for anything followed by non-symbols,
17951788
// double newlines, or double-space-newlines

0 commit comments

Comments
 (0)
Please sign in to comment.