diff --git a/lib/parser_block.js b/lib/parser_block.js index 5875f09..2170fd2 100644 --- a/lib/parser_block.js +++ b/lib/parser_block.js @@ -94,9 +94,6 @@ ParserBlock.prototype.tokenize = function (state, startLine, endLine) { if (line < endLine && state.isEmpty(line)) { hasEmptyLines = true; line++; - - // two empty lines should stop the parser in list mode - if (line < endLine && state.parentType === 'list' && state.isEmpty(line)) { break; } state.line = line; } } diff --git a/lib/rules_block/blockquote.js b/lib/rules_block/blockquote.js index 30c3702..2b104aa 100644 --- a/lib/rules_block/blockquote.js +++ b/lib/rules_block/blockquote.js @@ -57,6 +57,9 @@ module.exports = function blockquote(state, startLine, endLine, silent) { terminatorRules = state.md.block.ruler.getRules('blockquote'); + oldParentType = state.parentType; + state.parentType = 'blockquote'; + // Search the end of the block // // Block ends with either: @@ -146,9 +149,6 @@ module.exports = function blockquote(state, startLine, endLine, silent) { state.sCount[nextLine] = -1; } - oldParentType = state.parentType; - state.parentType = 'blockquote'; - token = state.push('blockquote_open', 'blockquote', 1); token.markup = '>'; token.map = lines = [ startLine, 0 ]; diff --git a/lib/rules_block/code.js b/lib/rules_block/code.js index 470cb01..a83db11 100644 --- a/lib/rules_block/code.js +++ b/lib/rules_block/code.js @@ -4,7 +4,7 @@ module.exports = function code(state, startLine, endLine/*, silent*/) { - var nextLine, last, token, emptyLines = 0; + var nextLine, last, token; if (state.sCount[startLine] - state.blkIndent < 4) { return false; } @@ -12,20 +12,10 @@ module.exports = function code(state, startLine, endLine/*, silent*/) { while (nextLine < endLine) { if (state.isEmpty(nextLine)) { - emptyLines++; - - // workaround for lists: 2 blank lines should terminate indented - // code block, but not fenced code block - if (emptyLines >= 2 && state.parentType === 'list') { - break; - } - nextLine++; continue; } - emptyLines = 0; - if (state.sCount[nextLine] - state.blkIndent >= 4) { nextLine++; last = nextLine; diff --git a/lib/rules_block/heading.js b/lib/rules_block/heading.js index 9b8eee4..5c133df 100644 --- a/lib/rules_block/heading.js +++ b/lib/rules_block/heading.js @@ -22,7 +22,7 @@ module.exports = function heading(state, startLine, endLine, silent) { ch = state.src.charCodeAt(++pos); } - if (level > 6 || (pos < max && ch !== 0x20/* space */)) { return false; } + if (level > 6 || (pos < max && !isSpace(ch))) { return false; } if (silent) { return true; } diff --git a/lib/rules_block/lheading.js b/lib/rules_block/lheading.js index 482084c..3e15b0c 100644 --- a/lib/rules_block/lheading.js +++ b/lib/rules_block/lheading.js @@ -5,9 +5,12 @@ module.exports = function lheading(state, startLine, endLine/*, silent*/) { var content, terminate, i, l, token, pos, max, level, marker, - nextLine = startLine + 1, + nextLine = startLine + 1, oldParentType, terminatorRules = state.md.block.ruler.getRules('paragraph'); + oldParentType = state.parentType; + state.parentType = 'paragraph'; // use paragraph to match terminatorRules + // jump line-by-line until empty one or EOF for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) { // this would be a code block normally, but after paragraph @@ -71,5 +74,7 @@ module.exports = function lheading(state, startLine, endLine/*, silent*/) { token = state.push('heading_close', 'h' + String(level), -1); token.markup = String.fromCharCode(marker); + state.parentType = oldParentType; + return true; }; diff --git a/lib/rules_block/list.js b/lib/rules_block/list.js index ea554f3..6569cec 100644 --- a/lib/rules_block/list.js +++ b/lib/rules_block/list.js @@ -147,7 +147,6 @@ module.exports = function list(state, startLine, endLine, silent) { if (isOrdered) { start = state.bMarks[startLine] + state.tShift[startLine]; markerValue = Number(state.src.substr(start, posAfterMarker - start - 1)); - token = state.push('ordered_list_open', 'ol', 1); if (markerValue !== 1) { token.attrs = [ [ 'start', markerValue ] ]; @@ -168,6 +167,9 @@ module.exports = function list(state, startLine, endLine, silent) { prevEmptyEnd = false; terminatorRules = state.md.block.ruler.getRules('list'); + oldParentType = state.parentType; + state.parentType = 'list'; + while (nextLine < endLine) { pos = posAfterMarker; max = state.eMarks[nextLine]; @@ -216,10 +218,8 @@ module.exports = function list(state, startLine, endLine, silent) { oldTight = state.tight; oldTShift = state.tShift[startLine]; oldLIndent = state.sCount[startLine]; - oldParentType = state.parentType; state.blkIndent = indent; state.tight = true; - state.parentType = 'list'; state.tShift[startLine] = contentStart - state.bMarks[startLine]; state.sCount[startLine] = offset; @@ -248,7 +248,6 @@ module.exports = function list(state, startLine, endLine, silent) { state.tShift[startLine] = oldTShift; state.sCount[startLine] = oldLIndent; state.tight = oldTight; - state.parentType = oldParentType; token = state.push('list_item_close', 'li', -1); token.markup = String.fromCharCode(markerCharCode); @@ -259,10 +258,6 @@ module.exports = function list(state, startLine, endLine, silent) { if (nextLine >= endLine) { break; } - if (state.isEmpty(nextLine)) { - break; - } - // // Try to check if list is terminated or continued. // @@ -301,6 +296,8 @@ module.exports = function list(state, startLine, endLine, silent) { listLines[1] = nextLine; state.line = nextLine; + state.parentType = oldParentType; + // mark paragraphs tight if needed if (tight) { markTightParagraphs(state, listTokIdx); diff --git a/lib/rules_block/paragraph.js b/lib/rules_block/paragraph.js index 18a860d..f0c6872 100644 --- a/lib/rules_block/paragraph.js +++ b/lib/rules_block/paragraph.js @@ -4,11 +4,14 @@ module.exports = function paragraph(state, startLine/*, endLine*/) { - var content, terminate, i, l, token, + var content, terminate, i, l, token, oldParentType, nextLine = startLine + 1, terminatorRules = state.md.block.ruler.getRules('paragraph'), endLine = state.lineMax; + oldParentType = state.parentType; + state.parentType = 'paragraph'; + // jump line-by-line until empty one or EOF for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) { // this would be a code block normally, but after paragraph @@ -43,5 +46,7 @@ module.exports = function paragraph(state, startLine/*, endLine*/) { token = state.push('paragraph_close', 'p', -1); + state.parentType = oldParentType; + return true; }; diff --git a/lib/rules_block/reference.js b/lib/rules_block/reference.js index 23f662b..c84823c 100644 --- a/lib/rules_block/reference.js +++ b/lib/rules_block/reference.js @@ -17,6 +17,7 @@ module.exports = function reference(state, startLine, _endLine, silent) { l, label, labelEnd, + oldParentType, res, start, str, @@ -46,6 +47,9 @@ module.exports = function reference(state, startLine, _endLine, silent) { // jump line-by-line until empty one or EOF terminatorRules = state.md.block.ruler.getRules('reference'); + oldParentType = state.parentType; + state.parentType = 'reference'; + for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++) { // this would be a code block normally, but after paragraph // it's considered a lazy continuation regardless of what's there @@ -186,6 +190,8 @@ module.exports = function reference(state, startLine, _endLine, silent) { state.env.references[label] = { title: title, href: href }; } + state.parentType = oldParentType; + state.line = startLine + lines + 1; return true; }; diff --git a/lib/rules_block/state_block.js b/lib/rules_block/state_block.js index 744951b..fa523a6 100644 --- a/lib/rules_block/state_block.js +++ b/lib/rules_block/state_block.js @@ -33,9 +33,12 @@ function StateBlock(src, md, env, tokens) { this.line = 0; // line index in src this.lineMax = 0; // lines count this.tight = false; // loose/tight mode for lists - this.parentType = 'root'; // if `list`, block parser stops on two newlines this.ddIndent = -1; // indent of the current dd block (-1 if there isn't any) + // can be 'blockquote', 'list', 'root', 'paragraph' or 'reference' + // used in lists to determine if they interrupt a paragraph + this.parentType = 'root'; + this.level = 0; // renderer diff --git a/test/fixtures/commonmark/bad.txt b/test/fixtures/commonmark/bad.txt index 63aa4ef..21c6220 100644 --- a/test/fixtures/commonmark/bad.txt +++ b/test/fixtures/commonmark/bad.txt @@ -18,81 +18,6 @@ error: -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 458 - -. -# Foo -. -
# Foo
- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 3821 - -. -- foo - - - bar -. -foo
-bar
-bar
- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 3866 - -. -- Foo - - bar - - - baz -. -Foo
-bar
-
-
-baz
-
-Foo
-bar
-
- baz
-
-
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4214
@@ -140,89 +65,6 @@ error:
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-src line: 4890
-
-.
-- foo
-
-- bar
-
-
-- baz
-.
-foo
-bar
-baz
-foo
-bar
-baz
-bim
- bim
-
-
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6137
diff --git a/test/fixtures/commonmark/good.txt b/test/fixtures/commonmark/good.txt
index 9553737..c04ffa9 100644
--- a/test/fixtures/commonmark/good.txt
+++ b/test/fixtures/commonmark/good.txt
@@ -110,6 +110,15 @@ src line: 440
.
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+src line: 458
+
+.
+# Foo
+.
+2.two
. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 3821 + +. +- foo + + + bar +. +foo
+bar
+Foo
+bar
+
+
+baz
+
+foo
+bar
+baz
+baz
+bim
+