Skip to content

Commit

Permalink
Merge pull request #1598 from calculuschild/table-block-breaks
Browse files Browse the repository at this point in the history
Fix GFM tables not breaking on block-level structures
  • Loading branch information
UziTech authored Mar 6, 2020
2 parents 6ae3651 + 5972e73 commit 8f1917e
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ block.html = edit(block.html, 'i')

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

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

block.gfm.table = edit(block.gfm.table)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('lheading', '([^\\n]+)\\n {0,3}(=+|-+) *(?:\\n+|$)')
.replace('blockquote', ' {0,3}>')
.replace('code', ' {4}[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
.getRegex();

/**
* Pedantic grammar (original John Gruber's loose markdown specification)
*/
Expand Down
21 changes: 21 additions & 0 deletions test/specs/new/code_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<pre><code>a simple
*indented* code block
</code></pre>
6 changes: 6 additions & 0 deletions test/specs/new/code_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
a simple
*indented* code block
19 changes: 19 additions & 0 deletions test/specs/new/fences_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<pre><code>foobar()</code></pre>
7 changes: 7 additions & 0 deletions test/specs/new/fences_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
```
foobar()
```
19 changes: 19 additions & 0 deletions test/specs/new/heading_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<h1 id="title">title</h1>
5 changes: 5 additions & 0 deletions test/specs/new/heading_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
# title
19 changes: 19 additions & 0 deletions test/specs/new/hr_following_tables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<hr>
5 changes: 5 additions & 0 deletions test/specs/new/hr_following_tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
___
19 changes: 19 additions & 0 deletions test/specs/new/html_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<div>Some HTML</div>
5 changes: 5 additions & 0 deletions test/specs/new/html_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
<div>Some HTML</div>
22 changes: 22 additions & 0 deletions test/specs/new/inlinecode_following_tables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
<tr>
<td><code>hello</code></td>
<td></td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions test/specs/new/inlinecode_following_tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
`hello`
19 changes: 19 additions & 0 deletions test/specs/new/lheading_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<h1 id="title">title</h1>
6 changes: 6 additions & 0 deletions test/specs/new/lheading_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
title
=====
23 changes: 23 additions & 0 deletions test/specs/new/list_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
</tbody>
</table>
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
7 changes: 7 additions & 0 deletions test/specs/new/list_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
- foo
- bar
- baz
22 changes: 22 additions & 0 deletions test/specs/new/strong_following_tables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
<tr>
<td><strong>strong</strong></td>
<td></td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions test/specs/new/strong_following_tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
**strong**
22 changes: 22 additions & 0 deletions test/specs/new/text_following_tables.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>foo</td>
</tr>
<tr>
<td>baz</td>
<td>boo</td>
</tr>
<tr>
<td>hello</td>
<td></td>
</tr>
</tbody>
</table>
5 changes: 5 additions & 0 deletions test/specs/new/text_following_tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
hello

1 comment on commit 8f1917e

@vercel
Copy link

@vercel vercel bot commented on 8f1917e Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.