Skip to content

Commit

Permalink
Add workarounds to lists to conform with CM spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Jan 25, 2016
1 parent 0661eea commit 07cfbc9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 60 deletions.
15 changes: 13 additions & 2 deletions lib/rules_block/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@


module.exports = function code(state, startLine, endLine/*, silent*/) {
var nextLine, last, token;
var nextLine, last, token, emptyLines = 0;

if (state.sCount[startLine] - state.blkIndent < 4) { return false; }

last = nextLine = startLine + 1;

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;
Expand All @@ -23,7 +34,7 @@ module.exports = function code(state, startLine, endLine/*, silent*/) {
break;
}

state.line = nextLine;
state.line = last;

token = state.push('code_block', 'code', 0);
token.content = state.getLines(startLine, last, 4 + state.blkIndent, true);
Expand Down
13 changes: 12 additions & 1 deletion lib/rules_block/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,18 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tShift[startLine] = contentStart - state.bMarks[startLine];
state.sCount[startLine] = offset;

state.md.block.tokenize(state, startLine, endLine, true);
if (contentStart >= max && state.isEmpty(startLine + 1)) {
// workaround for this case
// (list item is empty, list terminates before "foo"):
// ~~~~~~~~
// -
//
// foo
// ~~~~~~~~
state.line = Math.min(state.line + 2, endLine);
} else {
state.md.block.tokenize(state, startLine, endLine, true);
}

// If any of list item is tight, mark list as tight
if (!state.tight || prevEmptyEnd) {
Expand Down
57 changes: 0 additions & 57 deletions test/fixtures/commonmark/bad.txt
Original file line number Diff line number Diff line change
@@ -1,57 +0,0 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3777

.
- Foo

bar


baz
.
<ul>
<li>
<p>Foo</p>
<pre><code>bar
</code></pre>
</li>
</ul>
<pre><code> baz
</code></pre>
.

error:

<ul>
<li>
<p>Foo</p>
<pre><code>bar


baz
</code></pre>
</li>
</ul>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4044

.
-

foo
.
<ul>
<li></li>
</ul>
<p>foo</p>
.

error:

<ul>
<li>foo</li>
</ul>


36 changes: 36 additions & 0 deletions test/fixtures/commonmark/good.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3060,6 +3060,28 @@ baz
</ul>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3777

.
- Foo

bar


baz
.
<ul>
<li>
<p>Foo</p>
<pre><code>bar
</code></pre>
</li>
</ul>
<pre><code> baz
</code></pre>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3799

Expand Down Expand Up @@ -3272,6 +3294,20 @@ src line: 4016
</ul>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4044

.
-

foo
.
<ul>
<li></li>
</ul>
<p>foo</p>
.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4058

Expand Down

0 comments on commit 07cfbc9

Please sign in to comment.