Skip to content

Commit 1f1b480

Browse files
itisrmkcursoragent
andauthored
fix: allow trailing tabs after a setext heading underline (#4152)
fix: allow trailing tabs on setext heading underlines CommonMark treats spaces or tabs after a setext underline as part of the underline. Marked only allowed spaces, so `Foo` followed by `===\t` stayed a paragraph and `---\t` became a thematic break. Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Rahul Kashyap <[email protected]>
1 parent 9cb84e0 commit 1f1b480

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎src/rules.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ export const other = {
5757
blankLine: /^[ \t]*$/,
5858
doubleBlankLine: /\n[ \t]*\n[ \t]*$/,
5959
blockquoteStart: /^ {0,3}>/,
60-
blockquoteSetextReplace: /\n {0,3}((?:=+|-+) *)(?=\n|$)/g,
60+
// Neutralize a lazy setext underline, including one with trailing tabs.
61+
// CommonMark §4.3 allows those tabs on a real underline, so the same class
62+
// has to be recognized here or a lazy `===\t` becomes a heading.
63+
blockquoteSetextReplace: /\n {0,3}((?:=+|-+)[ \t]*)(?=\n|$)/g,
6164
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
6265
blockquoteMarkerWhitespace: /^( {0,3}>)([ \t]+)/gm,
6366
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
@@ -119,7 +122,10 @@ const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\
119122
const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
120123
const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
121124
const bullet = / {0,3}(?:[*+-]|\d{1,9}[.)])/;
122-
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |fences|blockquote|heading|hr|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/;
125+
// The underline may end with spaces or tabs (CommonMark §4.3). Internal
126+
// spaces or tabs still fail to match, and more than three spaces of indent
127+
// still do not.
128+
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |fences|blockquote|heading|hr|html|table))+?)\n {0,3}(=+|-+)[ \t]*(?:\n+|$)/;
123129
const lheading = edit(lheadingCore)
124130
.replace(/bull/g, bullet) // lists can interrupt
125131
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code can start a block but cannot interrupt a paragraph
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h1>Foo</h1>
2+
<h2>Foo</h2>
3+
<h2>Foo</h2>
4+
<h1>Foo
5+
Bar</h1>
6+
<h2>Foo <em>bar</em></h2>
7+
<blockquote>
8+
<h1>Bar</h1>
9+
</blockquote>
10+
<blockquote>
11+
<h2>Baz</h2>
12+
</blockquote>
13+
<blockquote>
14+
<p>Foo
15+
===</p>
16+
</blockquote>
17+
<p>Foo
18+
---</p>
19+
<p>Foo
20+
= =</p>
21+
<hr>
22+
<ul>
23+
<li><h2>Foo</h2>
24+
</li>
25+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Foo
2+
===
3+
4+
Foo
5+
---
6+
7+
Foo
8+
----
9+
10+
Foo
11+
Bar
12+
===
13+
14+
Foo *bar*
15+
---
16+
17+
> Bar
18+
> ===
19+
20+
> Baz
21+
> ---
22+
23+
> Foo
24+
===
25+
26+
Foo
27+
---
28+
29+
Foo
30+
= =
31+
32+
---
33+
34+
- Foo
35+
---

0 commit comments

Comments
 (0)