Skip to content

Commit

Permalink
Merge pull request markedjs#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 committed Mar 6, 2020
2 parents 4d6092d + aeef4b1 commit aa5d7e8
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
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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,7 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
```
foobar()
```
19 changes: 19 additions & 0 deletions test/specs/new/heading_following_table.html
@@ -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
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
# title
19 changes: 19 additions & 0 deletions test/specs/new/hr_following_tables.html
@@ -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
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
___
19 changes: 19 additions & 0 deletions test/specs/new/html_following_table.html
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
`hello`
19 changes: 19 additions & 0 deletions test/specs/new/lheading_following_table.html
@@ -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
@@ -0,0 +1,6 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
title
=====
23 changes: 23 additions & 0 deletions test/specs/new/list_following_table.html
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
**strong**
22 changes: 22 additions & 0 deletions test/specs/new/text_following_tables.html
@@ -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
@@ -0,0 +1,5 @@
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
hello

0 comments on commit aa5d7e8

Please sign in to comment.