Skip to content

Commit

Permalink
fix: table after paragraph without blank line (#2298)
Browse files Browse the repository at this point in the history
* fix: gfm table requires leading empty line

* test(gfm/table): add some test cases

* revert changes to `gfm.0.29.json`

* test(gfm/table): add `table_following_text` testcase
  • Loading branch information
EmmetZC committed Nov 25, 2021
1 parent 6ac4d82 commit 5714212
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const block = {
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
// regex template, placeholders will be replaced according to different paragraph
// interruption rules of commonmark and the original markdown spec:
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html| +\n)[^\n]+)*)/,
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,
text: /^[^\n]+/
};

Expand Down Expand Up @@ -69,6 +69,7 @@ block.paragraph = edit(block._paragraph)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.replace('|table', '')
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
Expand Down Expand Up @@ -107,6 +108,17 @@ block.gfm.table = edit(block.gfm.table)
.replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
.getRegex();

block.gfm.paragraph = edit(block._paragraph)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.replace('table', block.gfm.table) // interrupt paragraphs with table
.replace('blockquote', ' {0,3}>')
.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|textarea|!--)')
.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
38 changes: 38 additions & 0 deletions test/specs/new/table_following_text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<p>hello world</p>
<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>
<p>hello world with empty line</p>
<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>
15 changes: 15 additions & 0 deletions test/specs/new/table_following_text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
gfm: true
---
hello world
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |

hello world with empty line

| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
46 changes: 46 additions & 0 deletions test/unit/Lexer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,52 @@ lheading 2
});
});

it('table after para', () => {
expectTokens({
md: `
paragraph 1
| a | b |
|---|---|
| 1 | 2 |
`,
tokens: [
{
type: 'paragraph',
raw: 'paragraph 1',
text: 'paragraph 1',
tokens: [{ type: 'text', raw: 'paragraph 1', text: 'paragraph 1' }]
},
{
type: 'table',
align: [null, null],
raw: '| a | b |\n|---|---|\n| 1 | 2 |\n',
header: [
{
text: 'a',
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
},
{
text: 'b',
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
}
],
rows: [
[
{
text: '1',
tokens: [{ type: 'text', raw: '1', text: '1' }]
},
{
text: '2',
tokens: [{ type: 'text', raw: '2', text: '2' }]
}
]
]
}
]
});
});

it('align table', () => {
expectTokens({
md: `
Expand Down

1 comment on commit 5714212

@vercel
Copy link

@vercel vercel bot commented on 5714212 Nov 25, 2021

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.