Skip to content

Commit

Permalink
fix: fix table rendered as heading (#3612)
Browse files Browse the repository at this point in the history
* add tests

* add table interrupt to lheading

* fix comment

* fix redos in rule
  • Loading branch information
UziTech authored Feb 9, 2025
1 parent a1113e0 commit 9ae87de
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,24 @@ const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\
const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
const bullet = /(?:[*+-]|\d{1,9}[.)])/;
const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/;
const lheading = edit(lheadingCore)
.replace(/bull/g, bullet) // lists can interrupt
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
.replace(/\|table/g, '') // table not in commonmark
.getRegex();
const lheadingGfm = edit(lheadingCore)
.replace(/bull/g, bullet) // lists can interrupt
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
.replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
.replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
.replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
.replace(/html/g, / {0,3}<[^\n>]+>\n/) // block html can interrupt
.replace(/table/g, / {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n/) // table can interrupt
.getRegex();
const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
const blockText = /^[^\n]+/;
Expand Down Expand Up @@ -186,6 +197,7 @@ const gfmTable = edit(

const blockGfm: Record<BlockKeys, RegExp> = {
...blockNormal,
lheading: lheadingGfm,
table: gfmTable,
paragraph: edit(_paragraph)
.replace('hr', hr)
Expand Down
41 changes: 41 additions & 0 deletions test/specs/new/hr_following_nptables.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,44 @@
</tbody>
</table>
<hr>

<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>

<p>text then table</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>
<hr>
13 changes: 13 additions & 0 deletions test/specs/new/hr_following_nptables.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
bar | foo
baz | boo
___

abc | def
--- | ---
bar | foo
baz | boo
---

text then table
abc | def
--- | ---
bar | foo
baz | boo
---
41 changes: 41 additions & 0 deletions test/specs/new/hr_following_tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,44 @@
</tbody>
</table>
<hr>

<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>

<p>text then table</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>
<hr>
13 changes: 13 additions & 0 deletions test/specs/new/hr_following_tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
| bar | foo |
| baz | boo |
___

| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
---

text then table
| abc | def |
| --- | --- |
| bar | foo |
| baz | boo |
---

0 comments on commit 9ae87de

Please sign in to comment.