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 <cursoragent@cursor.com>
Co-authored-by: Rahul Kashyap <itisrmk@users.noreply.github.com>
This commit is contained in:
Rahul Kashyap
2026-11-02 07:52:01 -07:00
committed by GitHub
co-authored by Cursor Agent Rahul Kashyap
parent 9cb84e07d1
commit 1f1b480a29
3 changed files with 68 additions and 2 deletions
+8 -2
View File
@@ -57,7 +57,10 @@ export const other = {
blankLine: /^[ \t]*$/,
doubleBlankLine: /\n[ \t]*\n[ \t]*$/,
blockquoteStart: /^ {0,3}>/,
blockquoteSetextReplace: /\n {0,3}((?:=+|-+) *)(?=\n|$)/g,
// Neutralize a lazy setext underline, including one with trailing tabs.
// CommonMark §4.3 allows those tabs on a real underline, so the same class
// has to be recognized here or a lazy `===\t` becomes a heading.
blockquoteSetextReplace: /\n {0,3}((?:=+|-+)[ \t]*)(?=\n|$)/g,
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
blockquoteMarkerWhitespace: /^( {0,3}>)([ \t]+)/gm,
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
@@ -119,7 +122,10 @@ 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 = / {0,3}(?:[*+-]|\d{1,9}[.)])/;
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |fences|blockquote|heading|hr|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/;
// The underline may end with spaces or tabs (CommonMark §4.3). Internal
// spaces or tabs still fail to match, and more than three spaces of indent
// still do not.
const lheadingCore = /^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |fences|blockquote|heading|hr|html|table))+?)\n {0,3}(=+|-+)[ \t]*(?:\n+|$)/;
const lheading = edit(lheadingCore)
.replace(/bull/g, bullet) // lists can interrupt
.replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code can start a block but cannot interrupt a paragraph
@@ -0,0 +1,25 @@
<h1>Foo</h1>
<h2>Foo</h2>
<h2>Foo</h2>
<h1>Foo
Bar</h1>
<h2>Foo <em>bar</em></h2>
<blockquote>
<h1>Bar</h1>
</blockquote>
<blockquote>
<h2>Baz</h2>
</blockquote>
<blockquote>
<p>Foo
===</p>
</blockquote>
<p>Foo
---</p>
<p>Foo
= =</p>
<hr>
<ul>
<li><h2>Foo</h2>
</li>
</ul>
@@ -0,0 +1,35 @@
Foo
===
Foo
---
Foo
----
Foo
Bar
===
Foo *bar*
---
> Bar
> ===
> Baz
> ---
> Foo
===
Foo
---
Foo
= =
---
- Foo
---