Skip to content

Commit e76453e

Browse files
authored
fix: fix multiline list item adds extra newline to raw (#3735)
* fix: fix multiline list item adds extra newline to raw * update other raw * remove only * write test for code * add def test
1 parent 36eaa29 commit e76453e

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

src/Lexer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
142142
const lastToken = tokens.at(-1);
143143
// An indented code block cannot interrupt a paragraph.
144144
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
145-
lastToken.raw += '\n' + token.raw;
145+
lastToken.raw += (lastToken.raw.endsWith('\n') ? '' : '\n') + token.raw;
146146
lastToken.text += '\n' + token.text;
147147
this.inlineQueue.at(-1)!.src = lastToken.text;
148148
} else {
@@ -198,7 +198,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
198198
src = src.substring(token.raw.length);
199199
const lastToken = tokens.at(-1);
200200
if (lastToken?.type === 'paragraph' || lastToken?.type === 'text') {
201-
lastToken.raw += '\n' + token.raw;
201+
lastToken.raw += (lastToken.raw.endsWith('\n') ? '' : '\n') + token.raw;
202202
lastToken.text += '\n' + token.raw;
203203
this.inlineQueue.at(-1)!.src = lastToken.text;
204204
} else if (!this.tokens.links[token.tag]) {
@@ -244,7 +244,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
244244
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
245245
const lastToken = tokens.at(-1);
246246
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
247-
lastToken.raw += '\n' + token.raw;
247+
lastToken.raw += (lastToken.raw.endsWith('\n') ? '' : '\n') + token.raw;
248248
lastToken.text += '\n' + token.text;
249249
this.inlineQueue.pop();
250250
this.inlineQueue.at(-1)!.src = lastToken.text;
@@ -261,7 +261,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
261261
src = src.substring(token.raw.length);
262262
const lastToken = tokens.at(-1);
263263
if (lastToken?.type === 'text') {
264-
lastToken.raw += '\n' + token.raw;
264+
lastToken.raw += (lastToken.raw.endsWith('\n') ? '' : '\n') + token.raw;
265265
lastToken.text += '\n' + token.text;
266266
this.inlineQueue.pop();
267267
this.inlineQueue.at(-1)!.src = lastToken.text;

test/unit/Lexer.test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,123 @@ paragraph
11731173
],
11741174
});
11751175
});
1176+
1177+
it('multiline', () => {
1178+
expectTokens({
1179+
md: `
1180+
- line 1
1181+
line 2
1182+
`,
1183+
tokens: [
1184+
{
1185+
type: 'space',
1186+
raw: '\n',
1187+
}, {
1188+
type: 'list',
1189+
raw: '- line 1\n line 2\n',
1190+
ordered: false,
1191+
start: '',
1192+
loose: false,
1193+
items: [
1194+
{
1195+
type: 'list_item',
1196+
raw: '- line 1\n line 2',
1197+
task: false,
1198+
checked: undefined,
1199+
loose: false,
1200+
text: 'line 1\nline 2',
1201+
tokens: [{
1202+
type: 'text',
1203+
raw: 'line 1\nline 2',
1204+
text: 'line 1\nline 2',
1205+
tokens: [{ type: 'text', raw: 'line 1\nline 2', text: 'line 1\nline 2', escaped: false }],
1206+
}],
1207+
},
1208+
],
1209+
},
1210+
],
1211+
});
1212+
});
1213+
1214+
it('indented code after paragraph', () => {
1215+
expectTokens({
1216+
md: `
1217+
- a
1218+
- b
1219+
`,
1220+
tokens: [{
1221+
type: 'space',
1222+
raw: '\n',
1223+
},
1224+
{
1225+
type: 'list',
1226+
raw: '- a\n - b\n',
1227+
ordered: false,
1228+
start: '',
1229+
loose: false,
1230+
items: [
1231+
{
1232+
type: 'list_item',
1233+
raw: '- a\n - b',
1234+
task: false,
1235+
checked: undefined,
1236+
loose: false,
1237+
text: 'a\n - b',
1238+
tokens: [
1239+
{
1240+
type: 'text',
1241+
raw: 'a\n - b',
1242+
text: 'a\n- b',
1243+
tokens: [
1244+
{ type: 'text', raw: 'a\n- b', text: 'a\n- b', escaped: false },
1245+
],
1246+
},
1247+
],
1248+
},
1249+
],
1250+
},
1251+
],
1252+
});
1253+
});
1254+
1255+
it('def after paragraph', () => {
1256+
expectTokens({
1257+
md: `
1258+
- hello
1259+
[1]: hello
1260+
`,
1261+
tokens: [
1262+
{ type: 'space', raw: '\n' },
1263+
{
1264+
type: 'list',
1265+
raw: '- hello\n[1]: hello\n',
1266+
ordered: false,
1267+
start: '',
1268+
loose: false,
1269+
items: [
1270+
{
1271+
type: 'list_item',
1272+
raw: '- hello\n[1]: hello',
1273+
task: false,
1274+
checked: undefined,
1275+
loose: false,
1276+
text: 'hello\n[1]: hello',
1277+
tokens: [
1278+
{
1279+
type: 'text',
1280+
raw: 'hello\n[1]: hello',
1281+
text: 'hello\n[1]: hello',
1282+
tokens: [
1283+
{ type: 'text', raw: 'hello\n[1]: hello', text: 'hello\n[1]: hello', escaped: false },
1284+
],
1285+
},
1286+
],
1287+
},
1288+
],
1289+
},
1290+
],
1291+
});
1292+
});
11761293
});
11771294

11781295
describe('html', () => {

0 commit comments

Comments
 (0)