Skip to content

Commit 5714212

Browse files
authored
fix: table after paragraph without blank line (#2298)
* 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
1 parent 6ac4d82 commit 5714212

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

src/rules.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const block = {
3030
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
3131
// regex template, placeholders will be replaced according to different paragraph
3232
// interruption rules of commonmark and the original markdown spec:
33-
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html| +\n)[^\n]+)*)/,
33+
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,
3434
text: /^[^\n]+/
3535
};
3636

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

111+
block.gfm.paragraph = edit(block._paragraph)
112+
.replace('hr', block.hr)
113+
.replace('heading', ' {0,3}#{1,6} ')
114+
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
115+
.replace('table', block.gfm.table) // interrupt paragraphs with table
116+
.replace('blockquote', ' {0,3}>')
117+
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
118+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
119+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
120+
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
121+
.getRegex();
110122
/**
111123
* Pedantic grammar (original John Gruber's loose markdown specification)
112124
*/
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>hello world</p>
2+
<table>
3+
<thead>
4+
<tr>
5+
<th>abc</th>
6+
<th>def</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<td>bar</td>
12+
<td>foo</td>
13+
</tr>
14+
<tr>
15+
<td>baz</td>
16+
<td>boo</td>
17+
</tr>
18+
</tbody>
19+
</table>
20+
<p>hello world with empty line</p>
21+
<table>
22+
<thead>
23+
<tr>
24+
<th>abc</th>
25+
<th>def</th>
26+
</tr>
27+
</thead>
28+
<tbody>
29+
<tr>
30+
<td>bar</td>
31+
<td>foo</td>
32+
</tr>
33+
<tr>
34+
<td>baz</td>
35+
<td>boo</td>
36+
</tr>
37+
</tbody>
38+
</table>
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
gfm: true
3+
---
4+
hello world
5+
| abc | def |
6+
| --- | --- |
7+
| bar | foo |
8+
| baz | boo |
9+
10+
hello world with empty line
11+
12+
| abc | def |
13+
| --- | --- |
14+
| bar | foo |
15+
| baz | boo |

test/unit/Lexer-spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,52 @@ lheading 2
204204
});
205205
});
206206

207+
it('table after para', () => {
208+
expectTokens({
209+
md: `
210+
paragraph 1
211+
| a | b |
212+
|---|---|
213+
| 1 | 2 |
214+
`,
215+
tokens: [
216+
{
217+
type: 'paragraph',
218+
raw: 'paragraph 1',
219+
text: 'paragraph 1',
220+
tokens: [{ type: 'text', raw: 'paragraph 1', text: 'paragraph 1' }]
221+
},
222+
{
223+
type: 'table',
224+
align: [null, null],
225+
raw: '| a | b |\n|---|---|\n| 1 | 2 |\n',
226+
header: [
227+
{
228+
text: 'a',
229+
tokens: [{ type: 'text', raw: 'a', text: 'a' }]
230+
},
231+
{
232+
text: 'b',
233+
tokens: [{ type: 'text', raw: 'b', text: 'b' }]
234+
}
235+
],
236+
rows: [
237+
[
238+
{
239+
text: '1',
240+
tokens: [{ type: 'text', raw: '1', text: '1' }]
241+
},
242+
{
243+
text: '2',
244+
tokens: [{ type: 'text', raw: '2', text: '2' }]
245+
}
246+
]
247+
]
248+
}
249+
]
250+
});
251+
});
252+
207253
it('align table', () => {
208254
expectTokens({
209255
md: `

0 commit comments

Comments
 (0)