Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: list alignment #1810

Merged
merged 6 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ module.exports = class Tokenizer {
let raw = cap[0];
const bull = cap[2];
const isordered = bull.length > 1;
const isparen = bull[bull.length - 1] === ')';

const list = {
type: 'list',
Expand All @@ -212,17 +211,45 @@ module.exports = class Tokenizer {
let next = false,
item,
space,
b,
bcurr,
bnext,
addBack,
loose,
istask,
ischecked;

const l = itemMatch.length;
let l = itemMatch.length;
bcurr = this.rules.block.listItemStart.exec(itemMatch[0]);
for (let i = 0; i < l; i++) {
item = itemMatch[i];
raw = item;

// Determine whether the next list item belongs here.
// Backpedal if it does not belong in this list.
if (i !== l - 1) {
bnext = this.rules.block.listItemStart.exec(itemMatch[i + 1]);

if (bnext[1].length > bcurr[0].length || bnext[1].length > 3) {
// nested list
itemMatch.splice(i, 2, itemMatch[i] + '\n' + itemMatch[i + 1]);
i--;
l--;
continue;
} else {
if (
// different bullet style
!this.options.pedantic || this.options.smartLists
? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
: isordered === (bnext[2].length === 1)
) {
addBack = itemMatch.slice(i + 1).join('\n');
list.raw = list.raw.substring(0, list.raw.length - addBack.length);
i = l - 1;
}
}
bcurr = bnext;
}

// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
Expand All @@ -237,18 +264,6 @@ module.exports = class Tokenizer {
: item.replace(/^ {1,4}/gm, '');
}

// Determine whether the next list item belongs here.
// Backpedal if it does not belong in this list.
if (i !== l - 1) {
b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];
if (isordered ? b.length === 1 || (!isparen && b[b.length - 1] === ')')
: (b.length > 1 || (this.options.smartLists && b !== bull))) {
addBack = itemMatch.slice(i + 1).join('\n');
list.raw = list.raw.substring(0, list.raw.length - addBack.length);
i = l - 1;
}
}

// Determine whether item is loose or not.
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
// for discount behavior.
Expand Down
8 changes: 6 additions & 2 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const block = {
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
heading: /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/,
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?! {0,3}bull )\n*|\s*$)/,
html: '^ {0,3}(?:' // optional indentation
+ '<(script|pre|style)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
+ '|comment[^\\n]*(\\n+|$)' // (2)
Expand Down Expand Up @@ -43,11 +43,15 @@ block.def = edit(block.def)
.getRegex();

block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
block.item = /^( *)(bull) ?[^\n]*(?:\n(?! *bull ?)[^\n]*)*/;
block.item = edit(block.item, 'gm')
.replace(/bull/g, block.bullet)
.getRegex();

block.listItemStart = edit(/^( *)(bull)/)
.replace('bull', block.bullet)
.getRegex();

block.list = edit(block.list)
.replace(/bull/g, block.bullet)
.replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
Expand Down
15 changes: 5 additions & 10 deletions test/specs/commonmark/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -2136,8 +2136,7 @@
"example": 265,
"start_line": 4578,
"end_line": 4590,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "10) foo\n - bar\n",
Expand All @@ -2153,8 +2152,7 @@
"example": 267,
"start_line": 4611,
"end_line": 4621,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "- - foo\n",
Expand Down Expand Up @@ -2186,8 +2184,7 @@
"example": 271,
"start_line": 4894,
"end_line": 4906,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "1. foo\n2. bar\n3) baz\n",
Expand Down Expand Up @@ -2261,17 +2258,15 @@
"example": 280,
"start_line": 5132,
"end_line": 5150,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "1. a\n\n 2. b\n\n 3. c\n",
"html": "<ol>\n<li>\n<p>a</p>\n</li>\n<li>\n<p>b</p>\n</li>\n<li>\n<p>c</p>\n</li>\n</ol>\n",
"example": 281,
"start_line": 5153,
"end_line": 5171,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "- a\n - b\n - c\n - d\n - e\n",
Expand Down
15 changes: 5 additions & 10 deletions test/specs/gfm/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -2136,8 +2136,7 @@
"example": 265,
"start_line": 4578,
"end_line": 4590,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "10) foo\n - bar\n",
Expand All @@ -2153,8 +2152,7 @@
"example": 267,
"start_line": 4611,
"end_line": 4621,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "- - foo\n",
Expand Down Expand Up @@ -2186,8 +2184,7 @@
"example": 271,
"start_line": 4894,
"end_line": 4906,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "1. foo\n2. bar\n3) baz\n",
Expand Down Expand Up @@ -2261,17 +2258,15 @@
"example": 280,
"start_line": 5132,
"end_line": 5150,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "1. a\n\n 2. b\n\n 3. c\n",
"html": "<ol>\n<li>\n<p>a</p>\n</li>\n<li>\n<p>b</p>\n</li>\n<li>\n<p>c</p>\n</li>\n</ol>\n",
"example": 281,
"start_line": 5153,
"end_line": 5171,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "- a\n - b\n - c\n - d\n - e\n",
Expand Down
8 changes: 8 additions & 0 deletions test/specs/new/list_align_number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pre><code>1. Item 1
</code></pre>
<ol start="10">
<li>Item 10</li>
<li>Item 100</li>
<li>Item 1000</li>
<li>Item 10000</li>
</ol>
5 changes: 5 additions & 0 deletions test/specs/new/list_align_number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. Item 1
10. Item 10
100. Item 100
1000. Item 1000
10000. Item 10000
3 changes: 3 additions & 0 deletions test/specs/new/same_bullet.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
pedantic: true
---
* test
+ test
- test