Browse Source

fences, heading, list fixes for stmd cases

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
e0a153fe09
  1. 15
      bin/remarkable.js
  2. 13
      lib/lexer_block/fences.js
  3. 36
      lib/lexer_block/heading.js
  4. 3
      lib/lexer_block/lheading.js
  5. 7
      lib/lexer_block/list.js
  6. 10
      lib/renderer.js
  7. 348
      test/fixtures/stmd/bad.txt
  8. 151
      test/fixtures/stmd/good.txt

15
bin/remarkable.js

@ -23,11 +23,6 @@ cli.addArgument([ 'file' ], {
defaultValue: '-'
});
cli.addArgument([ '-t', '--trace' ], {
help: 'Show stack trace on error',
action: 'storeTrue'
});
var options = cli.parseArgs();
@ -61,10 +56,7 @@ readFile(options.file, 'utf8', function (error, input) {
process.exit(2);
}
console.error(
options.trace && error.stack ||
error.message ||
String(error));
console.error(error.stack || error.message || String(error));
process.exit(1);
}
@ -75,10 +67,7 @@ readFile(options.file, 'utf8', function (error, input) {
output = md.render(input);
} catch (error) {
console.error(
options.trace && error.stack ||
error.message ||
String(error));
console.error(error.stack || error.message || String(error));
process.exit(1);
}

13
lib/lexer_block/fences.js

@ -31,6 +31,11 @@ module.exports = function fences(state, startLine, endLine, silent) {
params = state.src.slice(pos, max).trim();
if (params.indexOf('`') >= 0) { return false; }
// Since start is found, we can report success here in validation mode
if (silent) { return true; }
// search end of block
nextLine = startLine;
@ -39,10 +44,12 @@ module.exports = function fences(state, startLine, endLine, silent) {
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
if (state.blkLevel === 0) {
// also block seems to be autoclosed by end of parent
/*if (state.blkLevel === 0) {
break;
}
return false;
return false;*/
break;
}
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine];
@ -64,8 +71,6 @@ module.exports = function fences(state, startLine, endLine, silent) {
break;
}
if (silent) { return true; }
// If fense has heading spases, those should be removed from inner block
len = state.tShift[startLine];

36
lib/lexer_block/heading.js

@ -5,15 +5,18 @@
var isWhiteSpace = require('../helpers').isWhiteSpace;
var skipSpaces = require('../helpers').skipSpaces;
var skipCharsBack = require('../helpers').skipCharsBack;
module.exports = function heading(state, startLine, endLine, silent) {
var ch, level,
pos = state.bMarks[startLine],
max = state.eMarks[startLine],
start = pos;
offset = state.tShift[startLine];
pos += state.tShift[startLine];
if (offset > 3) { return false; }
pos += offset;
if (pos >= max) { return false; }
@ -32,30 +35,23 @@ module.exports = function heading(state, startLine, endLine, silent) {
if (level > 6 || (pos < max && !isWhiteSpace(ch))) { return false; }
// skip spaces before heading text
pos = pos < max ? skipSpaces(state, pos) : pos;
pos = skipSpaces(state, pos);
// Now pos contains offset of first heared char
// Let's cut tails like ' ### ' from the end of string
max--;
ch = state.src.charCodeAt(max);
max = skipCharsBack(state, max, 0x20/* space */, pos);
max = skipCharsBack(state, max, 0x23/* # */, pos);
while (max > start && isWhiteSpace(ch)) {
ch = state.src.charCodeAt(--max);
}
if (ch === 0x23/* # */) {
while (max > start && ch === 0x23/* # */) {
ch = state.src.charCodeAt(--max);
}
if (isWhiteSpace(ch)) {
while (max > start && isWhiteSpace(ch)) {
ch = state.src.charCodeAt(--max);
}
} else if (ch === 0x5C/* \ */) {
max++;
}
if (max < state.eMarks[startLine] &&
state.src.charCodeAt(max) === 0x23/* # */ &&
state.src.charCodeAt(max - 1) === 0x5C/* \ */) {
max++;
}
max++;
// ## Foo ####
// ^^^
max = skipCharsBack(state, max, 0x20/* space */, pos);
if (silent) { return true; }

3
lib/lexer_block/lheading.js

@ -5,6 +5,7 @@
var skipSpaces = require('../helpers').skipSpaces;
var skipChars = require('../helpers').skipChars;
var skipCharsBack = require('../helpers').skipCharsBack;
module.exports = function lheading(state, startLine, endLine, silent) {
@ -32,7 +33,7 @@ module.exports = function lheading(state, startLine, endLine, silent) {
if (silent) { return true; }
pos = state.bMarks[startLine] + state.tShift[startLine];
max = skipChars(state, state.eMarks[startLine], 0x20/* space */, pos);
max = skipCharsBack(state, state.eMarks[startLine], 0x20/* space */, pos);
state.tokens.push({ type: 'heading_open', level: marker === 0x3D/* = */ ? 1 : 2 });
state.lexerInline.tokenize(state, pos, max);

7
lib/lexer_block/list.js

@ -47,8 +47,7 @@ function skipOrderedListMarker(state, startLine) {
ch = state.src.charCodeAt(pos++);
// First char should be non zero digit
if (ch < 0x31/* 1 */ || ch > 0x39/* 9 */) { return -1; }
if (ch < 0x30/* 0 */ || ch > 0x39/* 9 */) { return -1; }
for (;;) {
// EOL -> fail
@ -156,7 +155,7 @@ module.exports = function list(state, startLine, endLine, silent) {
// " - test"
// ^^^^^ - calculating total length of this thing
indent = (posAfterMarker - state.tShift[line]) + indentAfterMarker;
indent = (posAfterMarker - state.bMarks[line]) + indentAfterMarker;
//
// Scan lines inside list items
@ -236,7 +235,7 @@ module.exports = function list(state, startLine, endLine, silent) {
// If any of list item is loose, mark list as loose
if (!subState.tight) {
state.tokens[listTokIdx] = false;
state.tokens[listTokIdx].tight = false;
}
state.tokens.push({ type: 'list_item_close' });

10
lib/renderer.js

@ -73,8 +73,8 @@ rules.hr = function (state, token, idx) {
rules.bullet_list_open = function (state /*, token, idx*/) {
state.result += '<ul>\n';
};
rules.bullet_list_close = function (state /*, token, idx*/) {
state.result += '</ul>\n';
rules.bullet_list_close = function (state, token, idx) {
state.result += '</ul>' + getBreak(state, idx);
};
rules.list_item_open = function (state /*, token, idx*/) {
state.result += '<li>';
@ -82,13 +82,13 @@ rules.list_item_open = function (state /*, token, idx*/) {
rules.list_item_close = function (state /*, token, idx*/) {
state.result += '</li>\n';
};
rules.ordered_list_open = function (state, token) {
rules.ordered_list_open = function (state, token /*, idx */) {
state.result += '<ol'
+ (token.order > 1 ? ' start="' + token.order + '"' : '')
+ '>\n';
};
rules.ordered_list_close = function (state /*, token, idx*/) {
state.result += '</ol>\n';
rules.ordered_list_close = function (state, token, idx) {
state.result += '</ol>' + getBreak(state, idx);
};

348
test/fixtures/stmd/bad.txt

@ -26,24 +26,6 @@ error:
<h1>foo *bar* *baz*</h1>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 566
.
foo
# bar
.
<p>foo
# bar</p>
.
error:
<p>foo</p>
<pre><code># bar
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 680
@ -64,21 +46,6 @@ error:
<h2>Foo *bar*</h2>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 776
.
Foo
-----
.
<h2>Foo</h2>
.
error:
<h2>Foo </h2>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 1237
@ -92,8 +59,8 @@ aaa</p>
error:
<pre><code class="language-```">aaa
</code></pre>
<p>``` ```
aaa</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -109,8 +76,8 @@ foo</p>
error:
<pre><code class="language-aa">foo
</code></pre>
<p>``` aa ```
foo</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -823,66 +790,13 @@ with two lines.</p>
</ol>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2455
.
- one
two
.
<ul>
<li><p>one</p>
<p>two</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2466
.
- one
two
.
<ul>
<li>one</li>
</ul>
<pre><code> two
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2478
.
- one
two
.
<ul>
<li><p>one</p>
<p>two</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2497
error:
.
> > 1. one
>>
>> two
.
<blockquote>
<blockquote>
<ol>
<li><p>one</p>
<p>two</p></li>
<li><p>A paragraph
with two lines.</p></li>
</ol>
</blockquote>
</blockquote>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2540
@ -919,6 +833,24 @@ bar
</ul>
.
error:
<ul>
<li><p>foo</p>
<p>bar</p></li>
</ul>
<ul>
<li>foo</li>
</ul>
<p>bar</p>
<ul>
<li><pre><code>foo</code></pre>
</li>
</ul>
<p>bar</p>
<pre><code></code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2574
@ -944,6 +876,16 @@ src line: 2574
</ol>
.
error:
<ol>
<li><p>foo</p>
<pre><code>bar
</code></pre>
</li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2612
@ -959,6 +901,15 @@ src line: 2612
</ul>
.
error:
<ul>
<li><p>foo</p>
<pre><code>bar</code></pre>
</li>
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2626
@ -974,6 +925,15 @@ src line: 2626
</ol>
.
error:
<ol start="10">
<li><p>foo</p>
<pre><code>bar</code></pre>
</li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2656
@ -993,6 +953,13 @@ src line: 2656
</ol>
.
error:
<ol>
<li><p>indented code</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2675
@ -1012,19 +979,12 @@ src line: 2675
</ol>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2723
error:
.
- foo
<ol>
<li><p>indented code</p></li>
</ol>
bar
.
<ul>
<li><p>foo</p>
<p>bar</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2743
@ -1048,6 +1008,14 @@ with two lines.</p>
</ol>
.
error:
<ol>
<li><p>A paragraph
with two lines.</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2764
@ -1070,6 +1038,14 @@ with two lines.</p>
</ol>
.
error:
<ol>
<li><p>A paragraph
with two lines.</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2785
@ -1092,6 +1068,14 @@ with two lines.</p>
</ol>
.
error:
<ol>
<li><p>A paragraph
with two lines.</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2833
@ -1114,6 +1098,14 @@ with two lines.</p>
</ol>
.
error:
<ol>
<li><p>A paragraph
with two lines.</p></li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2866
@ -1193,10 +1185,10 @@ error:
<ul>
<li>foo<ul>
<li>bar</li>
<li>bar<ul>
<li>baz</li>
</ul>
</li>
</ul></li>
</ul></li>
</ul>
@ -1220,60 +1212,8 @@ error:
<ol start="10">
<li>foo<ul>
<li>bar</li>
</ul>
</li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2964
.
- - foo
.
<ul>
<li><ul>
<li>foo</li>
</ul></li>
</ul>
.
error:
<ul>
<li><ul>
<li>foo</li>
</ul>
</li>
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2974
.
1. - 2. foo
.
<ol>
<li><ul>
<li><ol start="2">
<li>foo</li>
</ol></li>
</ul></li>
</ol>
.
error:
<ol>
<li><ul>
<li><ol start="2">
<li>foo</li>
</ol>
</li>
</ul>
</li>
</ol>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1389,10 +1329,10 @@ error:
<ul>
<li>foo<ul>
<li>bar</li>
<li>bar<ul>
<li>baz</li>
</ul>
</li>
</ul></li>
</ul></li>
</ul>
<pre><code> bim
</code></pre>
@ -1420,6 +1360,19 @@ src line: 3336
</code></pre>
.
error:
<ul>
<li><p>foo</p>
<p>notcode</p></li>
</ul>
<ul>
<li>foo</li>
</ul>
<pre><code>code
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3383
@ -1474,36 +1427,6 @@ error:
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3415
.
- a
- b
c
- d
.
<ul>
<li><p>a</p></li>
<li><p>b</p>
<p>c</p></li>
<li><p>d</p></li>
</ul>
.
error:
<ul>
<li>a</li>
<li>b</li>
</ul>
<p>c</p>
<ul>
<li>d</li>
</ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3430
@ -1524,12 +1447,10 @@ src line: 3430
error:
<ul>
<li>a</li>
<li>b</li>
</ul>
<p>[ref]: /url</p>
<ul>
<li>d</li>
<li><p>a</p></li>
<li><p>b</p>
<p>[ref]: /url</p></li>
<li><p>d</p></li>
</ul>
@ -1590,9 +1511,9 @@ error:
<ul>
<li>a<ul>
<li>b</li>
</ul>
c</li>
<li><p>b</p>
<p>c</p></li>
</ul></li>
<li>d</li>
</ul>
@ -1678,8 +1599,7 @@ error:
<ul>
<li>a<ul>
<li>b</li>
</ul>
</li>
</ul></li>
</ul>
@ -1743,13 +1663,13 @@ error:
<li>a<ul>
<li>b</li>
<li>c</li>
</ul>
</li>
</ul></li>
</ul>
<ul>
<li>d</li>
<li>d<ul>
<li>e</li>
<li>f</li>
</ul></li>
</ul>
@ -2182,20 +2102,6 @@ error:
<p>&lt;a href=&quot;`&quot;&gt;`</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3963
.
```foo``
.
<p>```foo``</p>
.
error:
<pre><code class="language-foo``"></code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4095

151
test/fixtures/stmd/good.txt

@ -318,6 +318,17 @@ src line: 559
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 566
.
foo
# bar
.
<p>foo
# bar</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 576
@ -497,6 +508,16 @@ Foo
<hr />
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 776
.
Foo
-----
.
<h2>Foo</h2>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 785
@ -1530,6 +1551,67 @@ src line: 2444
<p>two</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2455
.
- one
two
.
<ul>
<li><p>one</p>
<p>two</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2466
.
- one
two
.
<ul>
<li>one</li>
</ul>
<pre><code> two
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2478
.
- one
two
.
<ul>
<li><p>one</p>
<p>two</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2497
.
> > 1. one
>>
>> two
.
<blockquote>
<blockquote>
<ol>
<li><p>one</p>
<p>two</p></li>
</ol>
</blockquote>
</blockquote>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2521
@ -1591,6 +1673,20 @@ src line: 2707
<p>bar</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2723
.
- foo
bar
.
<ul>
<li><p>foo</p>
<p>bar</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2806
@ -1654,6 +1750,34 @@ src line: 2950
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2964
.
- - foo
.
<ul>
<li><ul>
<li>foo</li>
</ul></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2974
.
1. - 2. foo
.
<ol>
<li><ul>
<li><ol start="2">
<li>foo</li>
</ol></li>
</ul></li>
</ol>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 2988
@ -1743,6 +1867,24 @@ src line: 3360
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3415
.
- a
- b
c
- d
.
<ul>
<li><p>a</p></li>
<li><p>b</p>
<p>c</p></li>
<li><p>d</p></li>
</ul>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3528
@ -1845,6 +1987,15 @@ src line: 3828
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3963
.
```foo``
.
<p>```foo``</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3969

Loading…
Cancel
Save