Skip to content

Commit 3ea7e88

Browse files
authored
fix: trim trailing whitespace from lheading text (#3920)
1 parent d4c0fe5 commit 3ea7e88

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Tokenizer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,13 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
574574
lheading(src: string): Tokens.Heading | undefined {
575575
const cap = this.rules.block.lheading.exec(src);
576576
if (cap) {
577+
const text = cap[1].trim();
577578
return {
578579
type: 'heading',
579580
raw: cap[0],
580581
depth: cap[2].charAt(0) === '=' ? 1 : 2,
581-
text: cap[1],
582-
tokens: this.lexer.inline(cap[1]),
582+
text,
583+
tokens: this.lexer.inline(text),
583584
};
584585
}
585586
}

test/unit/Lexer.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ lheading 2
165165
});
166166
});
167167

168+
it('lheading should trim trailing whitespace from text', () => {
169+
expectTokens({
170+
md: 'heading with trailing spaces \n==============================',
171+
tokens: [{
172+
type: 'heading',
173+
raw: 'heading with trailing spaces \n==============================',
174+
depth: 1,
175+
text: 'heading with trailing spaces',
176+
tokens: [{ type: 'text', raw: 'heading with trailing spaces', text: 'heading with trailing spaces', escaped: false }],
177+
}],
178+
});
179+
});
180+
168181
it('should not be heading if depth > 6', () => {
169182
expectTokens({
170183
md: '####### heading 7',

0 commit comments

Comments
 (0)