diff --git a/lib/marked.js b/lib/marked.js index 731b513a4f..ba9abac82d 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -631,31 +631,44 @@ addBack, loose, istask, - ischecked; + ischecked, + endMatch; var l = itemMatch.length; bcurr = this.rules.block.listItemStart.exec(itemMatch[0]); for (var i = 0; i < l; i++) { item = itemMatch[i]; - raw = item; // Determine whether the next list item belongs here. + raw = item; + + if (!this.options.pedantic) { + // Determine if current item contains the end of the list + endMatch = item.match(new RegExp('\\n\\s*\\n {0,' + (bcurr[0].length - 1) + '}\\S')); + + if (endMatch) { + addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length; + list.raw = list.raw.substring(0, list.raw.length - addBack); + item = item.substring(0, endMatch.index + 1); + raw = item; + l = i + 1; + } + } // 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 (!this.options.pedantic ? bnext[1].length > bcurr[0].length || bnext[1].length > 3 : bnext[1].length > bcurr[1].length) { - // nested list - itemMatch.splice(i, 2, itemMatch[i] + '\n' + itemMatch[i + 1]); + if (!this.options.pedantic ? bnext[1].length >= bcurr[0].length || bnext[1].length > 3 : bnext[1].length > bcurr[1].length) { + // nested list or continuation + itemMatch.splice(i, 2, itemMatch[i] + (!this.options.pedantic && bnext[1].length < bcurr[0].length && !itemMatch[i].match(/\n$/) ? '' : '\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; - } + } 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').length; + list.raw = list.raw.substring(0, list.raw.length - addBack); + i = l - 1; } bcurr = bnext; @@ -1179,7 +1192,7 @@ block.bullet = /(?:[*+-]|\d{1,9}[.)])/; block.item = /^( *)(bull) ?[^\n]*(?:\n(?! *bull ?)[^\n]*)*/; block.item = edit$1(block.item, 'gm').replace(/bull/g, block.bullet).getRegex(); - block.listItemStart = edit$1(/^( *)(bull)/).replace('bull', block.bullet).getRegex(); + block.listItemStart = edit$1(/^( *)(bull) */).replace('bull', 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(); block._tag = 'address|article|aside|base|basefont|blockquote|body|caption' + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption' + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe' + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option' + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr' + '|track|ul'; block._comment = /|$)/;