diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 50a7879d..ac9c7469 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -304,6 +304,10 @@ export class _Tokenizer { list(src: string): Tokens.List | undefined { let cap = this.rules.block.list.exec(src); if (cap) { + // src is only consumed from the front, so every raw below is a slice of + // listSrc rather than a string built up line by line + const listSrc = src; + const taken = () => listSrc.length - src.length; let bull = cap[1].trim(); const isordered = bull.length > 1; @@ -328,7 +332,6 @@ export class _Tokenizer { // Check if current bullet point can start a new List Item while (src) { let endEarly = false; - let raw = ''; let itemContents = ''; if (!(cap = itemRegex.exec(src))) { break; @@ -338,8 +341,8 @@ export class _Tokenizer { break; } - raw = cap[0]; - src = src.substring(raw.length); + const itemStart = taken(); + src = src.substring(cap[0].length); const firstLine = cap[2].split('\n', 1)[0]; const bulletIndent = cap[1].length; @@ -363,7 +366,6 @@ export class _Tokenizer { } if (blankLine && this.rules.other.blankLine.test(nextLine)) { // Items begin with at most one blank line - raw += nextLine + '\n'; src = src.substring(nextLine.length + 1); endEarly = true; } @@ -447,12 +449,16 @@ export class _Tokenizer { blankLine = !nextLine.trim(); - raw += rawLine + '\n'; src = src.substring(rawLine.length + 1); line = nextLineWithoutTabs.slice(indent); } } + // The loops above appended a '\n' the source may not have had. It could + // only land on the item that exhausts src, whose raw is trimmed below, + // and after which nothing reads endsWithBlankLine + const raw = listSrc.slice(itemStart, taken()); + if (!list.loose) { // If the previous item ended with a blank line, the list is loose if (endsWithBlankLine) { @@ -470,8 +476,6 @@ export class _Tokenizer { text: itemContents, tokens: [], }); - - list.raw += raw; } // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic @@ -483,7 +487,7 @@ export class _Tokenizer { // not a list since there were no items return; } - list.raw = list.raw.trimEnd(); + list.raw = listSrc.slice(0, taken()).trimEnd(); // Item child tokens handled here at end because we needed to have the final item to trim it first // First pass: tokenize items and finalize list.loose from spacers before placing checkboxes