fix: allow a tab before the closing sequence of an ATX heading (#4084)

CommonMark 0.31.2 says the optional closing sequence of #s must be
preceded by spaces or tabs, but the heading tokenizer tested for a
space only, so `# foo<tab>#` kept the trailing hashes as content.

The shared endingSpaceChar rule stays space only because code spans
need exactly that, so the heading path gets its own rule.
This commit is contained in:
Bao Nguyen
2026-09-05 07:08:46 -06:00
committed by GitHub
parent 23b1706a0e
commit 4417582f35
4 changed files with 16 additions and 2 deletions
+2 -2
View File
@@ -138,8 +138,8 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
const trimmed = rtrim(text, '#');
if (this.options.pedantic) {
text = trimmed.trim();
} else if (!trimmed || this.rules.other.endingSpaceChar.test(trimmed)) {
// CommonMark requires space before trailing #s
} else if (!trimmed || this.rules.other.endingSpaceTabChar.test(trimmed)) {
// CommonMark requires a space or tab before trailing #s
text = trimmed.trim();
}
}
+1
View File
@@ -48,6 +48,7 @@ export const other = {
endingHash: /#$/,
startingSpaceChar: /^ /,
endingSpaceChar: / $/,
endingSpaceTabChar: /[ \t]$/,
nonSpaceChar: /[^ ]/,
newLineCharGlobal: /\n/g,
tabCharGlobal: /\t/g,
@@ -0,0 +1,5 @@
<h1>foo</h1>
<h2>bar</h2>
<h3>baz #</h3>
<h4>qux</h4>
<h5>quux#</h5>
@@ -0,0 +1,8 @@
---
gfm: false
---
# foo #
## bar ###
### baz # #
#### qux #
##### quux#