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

Fixes ordered list ")" delimiter #1704

Merged
merged 12 commits into from Jun 17, 2020
9 changes: 5 additions & 4 deletions lib/marked.esm.js
Expand Up @@ -492,12 +492,13 @@ var Tokenizer_1 = 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',
raw,
ordered: isordered,
start: isordered ? +bull : '',
start: isordered ? +bull.slice(0, -1) : '',
loose: false,
items: []
};
Expand All @@ -522,7 +523,7 @@ var Tokenizer_1 = class Tokenizer {
// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
item = item.replace(/^ *([*+-]|\d+\.) */, '');
item = item.replace(/^ *([*+-]|\d+[.)]) */, '');

// Outdent whatever the
// list item contains. Hacky.
Expand All @@ -537,7 +538,7 @@ var Tokenizer_1 = class Tokenizer {
// Backpedal if it does not belong in this list.
if (i !== l - 1) {
b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];
if (bull.length > 1 ? b.length === 1
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);
Expand Down Expand Up @@ -974,7 +975,7 @@ block.def = edit$1(block.def)
.replace('title', block._title)
.getRegex();

block.bullet = /(?:[*+-]|\d{1,9}\.)/;
block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
block.item = edit$1(block.item, 'gm')
.replace(/bull/g, block.bullet)
Expand Down
9 changes: 5 additions & 4 deletions lib/marked.js
Expand Up @@ -585,11 +585,12 @@
var raw = cap[0];
var bull = cap[2];
var isordered = bull.length > 1;
var isparen = bull[bull.length - 1] === ')';
var list = {
type: 'list',
raw: raw,
ordered: isordered,
start: isordered ? +bull : '',
start: isordered ? +bull.slice(0, -1) : '',
loose: false,
items: []
}; // Get each top-level item.
Expand All @@ -611,7 +612,7 @@
// so it is seen as the next token.

space = item.length;
item = item.replace(/^ *([*+-]|\d+\.) */, ''); // Outdent whatever the
item = item.replace(/^ *([*+-]|\d+[.)]) */, ''); // Outdent whatever the
// list item contains. Hacky.

if (~item.indexOf('\n ')) {
Expand All @@ -624,7 +625,7 @@
if (i !== l - 1) {
b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];

if (bull.length > 1 ? b.length === 1 : b.length > 1 || this.options.smartLists && b !== bull) {
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;
Expand Down Expand Up @@ -1076,7 +1077,7 @@
block._label = /(?!\s*\])(?:\\[\[\]]|[^\[\]])+/;
block._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
block.def = edit$1(block.def).replace('label', block._label).replace('title', block._title).getRegex();
block.bullet = /(?:[*+-]|\d{1,9}\.)/;
block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
block.item = edit$1(block.item, 'gm').replace(/bull/g, block.bullet).getRegex();
block.list = edit$1(block.list).replace(/bull/g, block.bullet).replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))').replace('def', '\\n+(?=' + block.def.source + ')').getRegex();
Expand Down
2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/Tokenizer.js
Expand Up @@ -195,12 +195,13 @@ 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',
raw,
ordered: isordered,
start: isordered ? +bull : '',
start: isordered ? +bull.slice(0, -1) : '',
loose: false,
items: []
};
Expand All @@ -225,7 +226,7 @@ module.exports = class Tokenizer {
// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
item = item.replace(/^ *([*+-]|\d+\.) */, '');
item = item.replace(/^ *([*+-]|\d+[.)]) */, '');

// Outdent whatever the
// list item contains. Hacky.
Expand All @@ -240,7 +241,7 @@ module.exports = class Tokenizer {
// Backpedal if it does not belong in this list.
if (i !== l - 1) {
b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];
if (bull.length > 1 ? b.length === 1
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);
Expand Down
2 changes: 1 addition & 1 deletion src/rules.js
Expand Up @@ -42,7 +42,7 @@ block.def = edit(block.def)
.replace('title', block._title)
.getRegex();

block.bullet = /(?:[*+-]|\d{1,9}\.)/;
block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
block.item = edit(block.item, 'gm')
.replace(/bull/g, block.bullet)
Expand Down
6 changes: 2 additions & 4 deletions test/specs/commonmark/commonmark.0.29.json
Expand Up @@ -2146,8 +2146,7 @@
"example": 266,
"start_line": 4595,
"end_line": 4606,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "10) foo\n - bar\n",
Expand Down Expand Up @@ -2197,8 +2196,7 @@
"example": 272,
"start_line": 4909,
"end_line": 4921,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "Foo\n- bar\n- baz\n",
Expand Down
6 changes: 2 additions & 4 deletions test/specs/gfm/commonmark.0.29.json
Expand Up @@ -2146,8 +2146,7 @@
"example": 266,
"start_line": 4595,
"end_line": 4606,
"section": "List items",
"shouldFail": true
"section": "List items"
},
{
"markdown": "10) foo\n - bar\n",
Expand Down Expand Up @@ -2197,8 +2196,7 @@
"example": 272,
"start_line": 4909,
"end_line": 4921,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "Foo\n- bar\n- baz\n",
Expand Down
11 changes: 11 additions & 0 deletions test/specs/new/list_paren_delimiter.html
@@ -0,0 +1,11 @@
<ol>
<li>one</li>
<li>two</li>
<li>three</li>
</ol>

<ol start="2">
<li>two</li>
<li>three</li>
<li>four</li>
</ol>
8 changes: 8 additions & 0 deletions test/specs/new/list_paren_delimiter.md
@@ -0,0 +1,8 @@
1) one
2) two
3) three


2) two
3) three
4) four
25 changes: 25 additions & 0 deletions test/unit/Lexer-spec.js
Expand Up @@ -365,6 +365,31 @@ a | b
});
});

it('ordered with parenthesis', () => {
expectTokens({
md: `
1) item 1
2) item 2
`,
tokens: jasmine.arrayContaining([
jasmine.objectContaining({
type: 'list',
raw: '1) item 1\n2) item 2\n',
ordered: true,
start: 1,
items: [
jasmine.objectContaining({
raw: '1) item 1'
}),
jasmine.objectContaining({
raw: '2) item 2\n'
})
]
})
])
});
});

it('start', () => {
expectTokens({
md: `
Expand Down