Skip to content

Commit

Permalink
FIX listitem bullet without whitespace (markedjs#1980)
Browse files Browse the repository at this point in the history
This makes marked render correctly with the following text:

```markdown
- item1
- item2
-Not a list item(without space)
- item3
```

Currently:
```html
<ul>
<li>item1</li>
<li>item2</li>
</ul>
<p>-Not a list item(without space)</p>
<ul>
<li>item3</li>
</ul>
```

Which should be:
```html
<ul>
<li>item1</li>
<li>item2
-Not a list item(without space)</li>
<li>item3</li>
</ul>
```

This is the rendered result for commonmark as well.

Changes:
- `nextBulletRegex` checks for whitespace after the bullet
- separately checks for hr or lheading
  • Loading branch information
jangxyz committed Mar 28, 2022
1 parent 4c5b974 commit 24b697e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Tokenizer.js
Expand Up @@ -225,18 +225,32 @@ export class Tokenizer {
}

if (!endEarly) {
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?: [^\\n]*)?(?:\\n|$))`);

// Check if following lines should be included in List Item
let prevLine = line;
while (src) {
rawLine = src.split('\n', 1)[0];
prevLine = line;
line = rawLine;

// Re-align to follow commonmark nesting rules
if (this.options.pedantic) {
line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
}

if (this.rules.block.hr.test(line)) {
// End list if bullet was actually HR (possibly move into itemRegex?)
// make sure it is not a setext heading, which takes precedence
const twoLines = [prevLine, line].join('\n');
const matchLheadingPattern = this.rules.block.lheading.test(twoLines);
const lineIdentIsLargerThanPrev = line.search(/[^ ]/) >= indent;
const isNextLineSetextHeading = matchLheadingPattern && lineIdentIsLargerThanPrev;
if (!isNextLineSetextHeading) {
break;
}
}

// End list item if found start of new bullet
if (nextBulletRegex.test(line)) {
break;
Expand Down

0 comments on commit 24b697e

Please sign in to comment.