Skip to content

Commit

Permalink
fix: fix empty heading breaks paragraph and table (#3050)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Oct 28, 2023
1 parent 39cee77 commit a1034e6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ block.lheading = edit(block.lheading)

block.paragraph = edit(block._paragraph)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.replace('|table', '')
.replace('blockquote', ' {0,3}>')
Expand Down Expand Up @@ -164,7 +164,7 @@ block.gfm = {

block.gfm.table = edit(block.gfm.table as Rule)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
.replace('blockquote', ' {0,3}>')
.replace('code', ' {4}[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
Expand All @@ -175,7 +175,7 @@ block.gfm.table = edit(block.gfm.table as Rule)

block.gfm.paragraph = edit(block._paragraph)
.replace('hr', block.hr)
.replace('heading', ' {0,3}#{1,6} ')
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
.replace('table', block.gfm.table as RegExp) // interrupt paragraphs with table
.replace('blockquote', ' {0,3}>')
Expand Down
5 changes: 5 additions & 0 deletions test/specs/new/empty_heading_following_paragraph.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Newline after heading</p>
<h2></h2>

<p>No newline at the end</p>
<h2></h2>
5 changes: 5 additions & 0 deletions test/specs/new/empty_heading_following_paragraph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Newline after heading
##

No newline at the end
##
5 changes: 5 additions & 0 deletions test/specs/new/empty_heading_following_paragraph_nogfm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Newline after heading</p>
<h2></h2>

<p>No newline at the end</p>
<h2></h2>
8 changes: 8 additions & 0 deletions test/specs/new/empty_heading_following_paragraph_nogfm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
gfm: false
---
Newline after heading
##

No newline at the end
##
41 changes: 41 additions & 0 deletions test/specs/new/empty_heading_following_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
<h2></h2>

<p>No newline at the end</p>

<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
<h2></h2>
13 changes: 13 additions & 0 deletions test/specs/new/empty_heading_following_table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
| a | b |
| - | - |
| 1 | 2 |
| 3 | 4 |
##

No newline at the end

| a | b |
| - | - |
| 1 | 2 |
| 3 | 4 |
##

1 comment on commit a1034e6

@vercel
Copy link

@vercel vercel bot commented on a1034e6 Oct 28, 2023

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.