Skip to content

Commit 420251e

Browse files
committed
fix: #644 parsing issue inside table rows
1 parent b732c6f commit 420251e

File tree

3 files changed

+86
-16
lines changed

3 files changed

+86
-16
lines changed

__snapshots__/index.compiler.spec.tsx.snap

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,15 @@ Markdown's email-style
542542
Setext-style headers are "underlined" using equal signs (for first-level
543543
headers) and dashes (for second-level headers). For example:
544544
</p>
545-
<h1>
546-
This is an H1
547-
</h1>
548-
<h2>
549-
This is an H2
550-
</h2>
545+
<pre>
546+
<code>
547+
This is an H1
548+
=============
549+
550+
This is an H2
551+
-------------
552+
</code>
553+
</pre>
551554
<p>
552555
Any number of underlining
553556
<code>

index.compiler.spec.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,50 @@ describe('GFM tables', () => {
25302530
</table>
25312531
`)
25322532
})
2533+
2534+
it('#644 handles nested inlines within table cells', () => {
2535+
render(
2536+
compiler(theredoc`
2537+
| Nested HTML | Link |
2538+
| ---------------------------------- | ---------------------------- |
2539+
| <div><strong>Nested</strong></div> | [I'm a \`link\`](www.google.com) |
2540+
`)
2541+
)
2542+
2543+
expect(root.innerHTML).toMatchInlineSnapshot(`
2544+
<table>
2545+
<thead>
2546+
<tr>
2547+
<th>
2548+
Nested HTML
2549+
</th>
2550+
<th>
2551+
Link
2552+
</th>
2553+
</tr>
2554+
</thead>
2555+
<tbody>
2556+
<tr>
2557+
<td>
2558+
<div>
2559+
<strong>
2560+
Nested
2561+
</strong>
2562+
</div>
2563+
</td>
2564+
<td>
2565+
<a href="www.google.com">
2566+
I'm a
2567+
<code>
2568+
link
2569+
</code>
2570+
</a>
2571+
</td>
2572+
</tr>
2573+
</tbody>
2574+
</table>
2575+
`)
2576+
})
25332577
})
25342578

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

index.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as React from 'react'
1212
* Analogous to `node.type`. Please note that the values here may change at any time,
1313
* so do not hard code against the value directly.
1414
*/
15-
export const RuleType = {
15+
export var RuleType = {
1616
blockQuote: '0',
1717
breakLine: '1',
1818
breakThematic: '2',
@@ -54,6 +54,10 @@ export const RuleType = {
5454
unorderedList: '33',
5555
} as const
5656

57+
if (process.env.NODE_ENV !== 'production') {
58+
Object.keys(RuleType).forEach(key => (RuleType[key] = key))
59+
}
60+
5761
export type RuleType = (typeof RuleType)[keyof typeof RuleType]
5862

5963
const enum Priority {
@@ -622,24 +626,40 @@ function parseTableRow(
622626
tableOutput: boolean
623627
): MarkdownToJSX.ParserResult[][] {
624628
const prevInTable = state.inTable
629+
625630
state.inTable = true
626-
let tableRow = source
631+
632+
const tableRow = [] as MarkdownToJSX.ParserResult[]
633+
let cells = [[]]
634+
let acc = ''
635+
636+
function flush() {
637+
if (acc) {
638+
tableRow.push.apply(tableRow, parse(acc, state))
639+
acc = ''
640+
}
641+
}
642+
643+
source
627644
.trim()
628645
// isolate situations where a pipe should be ignored (inline code, escaped, etc)
629646
.split(/( *(?:`[^`]*`|\\\||\|) *)/)
630-
.reduce((nodes, fragment) => {
631-
if (fragment.trim() === '|')
632-
nodes.push(
647+
.forEach(fragment => {
648+
if (fragment.trim() === '|') {
649+
flush()
650+
651+
tableRow.push(
633652
tableOutput
634653
? { type: RuleType.tableSeparator }
635654
: { type: RuleType.text, text: fragment }
636655
)
637-
else if (fragment !== '') nodes.push.apply(nodes, parse(fragment, state))
638-
return nodes
639-
}, [] as MarkdownToJSX.ParserResult[])
640-
state.inTable = prevInTable
656+
} else if (fragment !== '') {
657+
acc += fragment
658+
}
659+
})
660+
661+
flush()
641662

642-
let cells = [[]]
643663
tableRow.forEach(function (node, i) {
644664
if (node.type === RuleType.tableSeparator) {
645665
// Filter out empty table separators at the start/end:
@@ -658,6 +678,9 @@ function parseTableRow(
658678
cells[cells.length - 1].push(node)
659679
}
660680
})
681+
682+
state.inTable = prevInTable
683+
661684
return cells
662685
}
663686

0 commit comments

Comments
 (0)