Skip to content

Commit 8f1917e

Browse files
authored
Merge pull request #1598 from calculuschild/table-block-breaks
Fix GFM tables not breaking on block-level structures
2 parents 6ae3651 + 5972e73 commit 8f1917e

21 files changed

+277
-2
lines changed

src/rules.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ block.html = edit(block.html, 'i')
6969

7070
block.paragraph = edit(block._paragraph)
7171
.replace('hr', block.hr)
72-
.replace('heading', ' {0,3}#{1,6} +')
72+
.replace('heading', ' {0,3}#{1,6} ')
7373
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
7474
.replace('blockquote', ' {0,3}>')
7575
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
@@ -94,9 +94,23 @@ block.normal = merge({}, block);
9494

9595
block.gfm = merge({}, block.normal, {
9696
nptable: /^ *([^|\n ].*\|.*)\n *([-:]+ *\|[-| :]*)(?:\n((?:.*[^>\n ].*(?:\n|$))*)\n*|$)/,
97-
table: /^ *\|(.+)\n *\|?( *[-:]+[-| :]*)(?:\n((?: *[^>\n ].*(?:\n|$))*)\n*|$)/
97+
table: '^ *\\|(.+)\\n' // Header
98+
+ ' *\\|?( *[-:]+[-| :]*)' // Align
99+
+ '(?:\\n((?:(?!^|>|\\n| |hr|heading|lheading|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
98100
});
99101

102+
block.gfm.table = edit(block.gfm.table)
103+
.replace('hr', block.hr)
104+
.replace('heading', ' {0,3}#{1,6} ')
105+
.replace('lheading', '([^\\n]+)\\n {0,3}(=+|-+) *(?:\\n+|$)')
106+
.replace('blockquote', ' {0,3}>')
107+
.replace('code', ' {4}[^\\n]')
108+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
109+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
110+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
111+
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
112+
.getRegex();
113+
100114
/**
101115
* Pedantic grammar (original John Gruber's loose markdown specification)
102116
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>abc</th>
5+
<th>def</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td>bar</td>
11+
<td>foo</td>
12+
</tr>
13+
<tr>
14+
<td>baz</td>
15+
<td>boo</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<pre><code>a simple
20+
*indented* code block
21+
</code></pre>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| abc | def |
2+
| --- | --- |
3+
| bar | foo |
4+
| baz | boo |
5+
a simple
6+
*indented* code block
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>abc</th>
5+
<th>def</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td>bar</td>
11+
<td>foo</td>
12+
</tr>
13+
<tr>
14+
<td>baz</td>
15+
<td>boo</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<pre><code>foobar()</code></pre>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| abc | def |
2+
| --- | --- |
3+
| bar | foo |
4+
| baz | boo |
5+
```
6+
foobar()
7+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>abc</th>
5+
<th>def</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td>bar</td>
11+
<td>foo</td>
12+
</tr>
13+
<tr>
14+
<td>baz</td>
15+
<td>boo</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<h1 id="title">title</h1>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| abc | def |
2+
| --- | --- |
3+
| bar | foo |
4+
| baz | boo |
5+
# title
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>abc</th>
5+
<th>def</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td>bar</td>
11+
<td>foo</td>
12+
</tr>
13+
<tr>
14+
<td>baz</td>
15+
<td>boo</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<hr>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| abc | def |
2+
| --- | --- |
3+
| bar | foo |
4+
| baz | boo |
5+
___
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>abc</th>
5+
<th>def</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<td>bar</td>
11+
<td>foo</td>
12+
</tr>
13+
<tr>
14+
<td>baz</td>
15+
<td>boo</td>
16+
</tr>
17+
</tbody>
18+
</table>
19+
<div>Some HTML</div>

0 commit comments

Comments
 (0)