Skip to content

Commit b379e3e

Browse files
authored
fix: prevent heading and def tokens from greedily capturing multiple newlines (#3925)
1 parent e8efc51 commit b379e3e

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/Tokenizer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
126126

127127
return {
128128
type: 'heading',
129-
raw: cap[0],
129+
raw: rtrim(cap[0], '\n'),
130130
depth: cap[1].length,
131131
text,
132132
tokens: this.lexer.inline(text),
@@ -501,7 +501,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
501501
return {
502502
type: 'def',
503503
tag,
504-
raw: cap[0],
504+
raw: rtrim(cap[0], '\n'),
505505
href,
506506
title,
507507
};
@@ -577,7 +577,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
577577
const text = cap[1].trim();
578578
return {
579579
type: 'heading',
580-
raw: cap[0],
580+
raw: rtrim(cap[0], '\n'),
581581
depth: cap[2].charAt(0) === '=' ? 1 : 2,
582582
text,
583583
tokens: this.lexer.inline(text),

test/unit/Lexer.test.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,53 +107,81 @@ lheading 2
107107
},
108108
{
109109
type: 'heading',
110-
raw: '# heading 1\n\n',
110+
raw: '# heading 1',
111111
depth: 1,
112112
text: 'heading 1',
113113
tokens: [{ type: 'text', raw: 'heading 1', text: 'heading 1', escaped: false }],
114114
},
115+
{
116+
type: 'space',
117+
raw: '\n\n',
118+
},
115119
{
116120
type: 'heading',
117-
raw: '## heading 2\n\n',
121+
raw: '## heading 2',
118122
depth: 2,
119123
text: 'heading 2',
120124
tokens: [{ type: 'text', raw: 'heading 2', text: 'heading 2', escaped: false }],
121125
},
126+
{
127+
type: 'space',
128+
raw: '\n\n',
129+
},
122130
{
123131
type: 'heading',
124-
raw: '### heading 3\n\n',
132+
raw: '### heading 3',
125133
depth: 3,
126134
text: 'heading 3',
127135
tokens: [{ type: 'text', raw: 'heading 3', text: 'heading 3', escaped: false }],
128136
},
137+
{
138+
type: 'space',
139+
raw: '\n\n',
140+
},
129141
{
130142
type: 'heading',
131-
raw: '#### heading 4\n\n',
143+
raw: '#### heading 4',
132144
depth: 4,
133145
text: 'heading 4',
134146
tokens: [{ type: 'text', raw: 'heading 4', text: 'heading 4', escaped: false }],
135147
},
148+
{
149+
type: 'space',
150+
raw: '\n\n',
151+
},
136152
{
137153
type: 'heading',
138-
raw: '##### heading 5\n\n',
154+
raw: '##### heading 5',
139155
depth: 5,
140156
text: 'heading 5',
141157
tokens: [{ type: 'text', raw: 'heading 5', text: 'heading 5', escaped: false }],
142158
},
159+
{
160+
type: 'space',
161+
raw: '\n\n',
162+
},
143163
{
144164
type: 'heading',
145-
raw: '###### heading 6\n\n',
165+
raw: '###### heading 6',
146166
depth: 6,
147167
text: 'heading 6',
148168
tokens: [{ type: 'text', raw: 'heading 6', text: 'heading 6', escaped: false }],
149169
},
170+
{
171+
type: 'space',
172+
raw: '\n\n',
173+
},
150174
{
151175
type: 'heading',
152-
raw: 'lheading 1\n==========\n\n',
176+
raw: 'lheading 1\n==========',
153177
depth: 1,
154178
text: 'lheading 1',
155179
tokens: [{ type: 'text', raw: 'lheading 1', text: 'lheading 1', escaped: false }],
156180
},
181+
{
182+
type: 'space',
183+
raw: '\n\n',
184+
},
157185
{
158186
type: 'heading',
159187
raw: 'lheading 2\n----------\n',

test/unit/marked.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ br
10061006
['space', ''],
10071007
['heading', '# heading'],
10081008
['text', 'heading'],
1009+
['space', ''],
10091010
['code', '```code```'],
10101011
['space', ''],
10111012
['table', '| a | b ||---|---|| 1 | 2 || 3 | 4 |'],

0 commit comments

Comments
 (0)