Skip to content

Commit 9ae87de

Browse files
authored
fix: fix table rendered as heading (#3612)
* add tests * add table interrupt to lheading * fix comment * fix redos in rule
1 parent a1113e0 commit 9ae87de

5 files changed

+121
-1
lines changed

src/rules.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,24 @@ const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\
8585
const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
8686
const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
8787
const bullet = /(?:[*+-]|\d{1,9}[.)])/;
88-
const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
88+
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/;
89+
const lheading = edit(lheadingCore)
8990
.replace(/bull/g, bullet) // lists can interrupt
9091
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
9192
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
9293
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
9394
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
9495
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
96+
.replace(/\|table/g, '') // table not in commonmark
97+
.getRegex();
98+
const lheadingGfm = edit(lheadingCore)
99+
.replace(/bull/g, bullet) // lists can interrupt
100+
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
101+
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
102+
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
103+
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
104+
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
105+
.replace(/table/g, / {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n/) // table can interrupt
95106
.getRegex();
96107
const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
97108
const blockText = /^[^\n]+/;
@@ -186,6 +197,7 @@ const gfmTable = edit(
186197

187198
const blockGfm: Record<BlockKeys, RegExp> = {
188199
...blockNormal,
200+
lheading: lheadingGfm,
189201
table: gfmTable,
190202
paragraph: edit(_paragraph)
191203
.replace('hr', hr)

test/specs/new/hr_following_nptables.html

+41
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,44 @@
1717
</tbody>
1818
</table>
1919
<hr>
20+
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>
39+
<hr>
40+
41+
<p>text then table</p>
42+
<table>
43+
<thead>
44+
<tr>
45+
<th>abc</th>
46+
<th>def</th>
47+
</tr>
48+
</thead>
49+
<tbody>
50+
<tr>
51+
<td>bar</td>
52+
<td>foo</td>
53+
</tr>
54+
<tr>
55+
<td>baz</td>
56+
<td>boo</td>
57+
</tr>
58+
</tbody>
59+
</table>
60+
<hr>

test/specs/new/hr_following_nptables.md

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@
33
bar | foo
44
baz | boo
55
___
6+
7+
abc | def
8+
--- | ---
9+
bar | foo
10+
baz | boo
11+
---
12+
13+
text then table
14+
abc | def
15+
--- | ---
16+
bar | foo
17+
baz | boo
18+
---

test/specs/new/hr_following_tables.html

+41
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,44 @@
1717
</tbody>
1818
</table>
1919
<hr>
20+
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>
39+
<hr>
40+
41+
<p>text then table</p>
42+
<table>
43+
<thead>
44+
<tr>
45+
<th>abc</th>
46+
<th>def</th>
47+
</tr>
48+
</thead>
49+
<tbody>
50+
<tr>
51+
<td>bar</td>
52+
<td>foo</td>
53+
</tr>
54+
<tr>
55+
<td>baz</td>
56+
<td>boo</td>
57+
</tr>
58+
</tbody>
59+
</table>
60+
<hr>

test/specs/new/hr_following_tables.md

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@
33
| bar | foo |
44
| baz | boo |
55
___
6+
7+
| abc | def |
8+
| --- | --- |
9+
| bar | foo |
10+
| baz | boo |
11+
---
12+
13+
text then table
14+
| abc | def |
15+
| --- | --- |
16+
| bar | foo |
17+
| baz | boo |
18+
---

0 commit comments

Comments
 (0)