From 666e94a47a2932f1e0e09ef38b712606493e47ae Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Wed, 29 Oct 2014 02:43:44 +0300 Subject: [PATCH] Change priority for all pairs `**foo __bar** baz__` is now parsed as `foo __bar baz__` --- lib/parser_inline.js | 5 +++++ lib/rules_inline/del.js | 15 ++++++++++----- lib/rules_inline/emphasis.js | 9 ++++----- lib/rules_inline/ins.js | 15 ++++++++++----- lib/rules_inline/mark.js | 21 +++++++++++++-------- test/fixtures/remarkable/del.txt | 4 ++-- test/fixtures/remarkable/ins.txt | 4 ++-- test/fixtures/remarkable/mark.txt | 4 ++-- 8 files changed, 48 insertions(+), 29 deletions(-) diff --git a/lib/parser_inline.js b/lib/parser_inline.js index 728a7fc..6c03305 100644 --- a/lib/parser_inline.js +++ b/lib/parser_inline.js @@ -81,6 +81,11 @@ ParserInline.prototype.skipToken = function (state) { } for (i = 0; i < len; i++) { + // TODO: change it to rule chains somehow + if ([ 'autolink', 'backticks', 'escape', 'htmltag', 'links', 'text' ].indexOf(this._rules[i].name) === -1) { + continue; + } + if (this._rules[i](state, true)) { state.cacheSet(pos, state.pos); return; diff --git a/lib/rules_inline/del.js b/lib/rules_inline/del.js index 0ad84ba..3891a65 100644 --- a/lib/rules_inline/del.js +++ b/lib/rules_inline/del.js @@ -5,6 +5,7 @@ module.exports = function del(state, silent) { var found, pos, + stack, max = state.posMax, start = state.pos, lastChar, @@ -13,11 +14,6 @@ module.exports = function del(state, silent) { if (state.src.charCodeAt(start) !== 0x7E/* ~ */) { return false; } if (start + 4 >= max) { return false; } if (state.src.charCodeAt(start + 1) !== 0x7E/* ~ */) { return false; } - - // make del lower a priority tag with respect to links, same as ; - // this code also prevents recursion - if (silent && state.isInLabel) { return false; } - if (state.level >= state.options.maxNesting) { return false; } lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; @@ -37,6 +33,7 @@ module.exports = function del(state, silent) { } state.pos = start + 2; + stack = 1; while (state.pos + 1 < max) { if (state.src.charCodeAt(state.pos) === 0x7E/* ~ */) { @@ -46,6 +43,14 @@ module.exports = function del(state, silent) { if (nextChar !== 0x7E/* ~ */ && lastChar !== 0x7E/* ~ */) { if (lastChar !== 0x20 && lastChar !== 0x0A) { // closing '~~' + stack--; + } else if (nextChar !== 0x20 && nextChar !== 0x0A) { + // opening '~~' + stack++; + } // else { + // // standalone ' ~~ ' indented with spaces + //} + if (stack <= 0) { found = true; break; } diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index cc84a7a..4b7d27c 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -62,11 +62,6 @@ module.exports = function emphasis(state, silent) { if (marker !== 0x5F/* _ */ && marker !== 0x2A /* * */) { return false; } - // skip emphasis in links because it has lower priority, compare: - // [foo *bar]()* - // [foo `bar]()` - if (silent && state.isInLabel) { return false; } - res = scanDelims(state, start); startCount = res.delims; if (!res.can_open) { @@ -110,6 +105,10 @@ module.exports = function emphasis(state, silent) { state.pos += count; continue; } + + if (res.can_open) { stack.push(count); } + state.pos += count; + continue; } state.parser.skipToken(state); diff --git a/lib/rules_inline/ins.js b/lib/rules_inline/ins.js index 291f625..f40a875 100644 --- a/lib/rules_inline/ins.js +++ b/lib/rules_inline/ins.js @@ -5,6 +5,7 @@ module.exports = function ins(state, silent) { var found, pos, + stack, max = state.posMax, start = state.pos, lastChar, @@ -13,11 +14,6 @@ module.exports = function ins(state, silent) { if (state.src.charCodeAt(start) !== 0x2B/* + */) { return false; } if (start + 4 >= max) { return false; } if (state.src.charCodeAt(start + 1) !== 0x2B/* + */) { return false; } - - // make ins lower a priority tag with respect to links, same as ; - // this code also prevents recursion - if (silent && state.isInLabel) { return false; } - if (state.level >= state.options.maxNesting) { return false; } lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; @@ -37,6 +33,7 @@ module.exports = function ins(state, silent) { } state.pos = start + 2; + stack = 1; while (state.pos + 1 < max) { if (state.src.charCodeAt(state.pos) === 0x2B/* + */) { @@ -46,6 +43,14 @@ module.exports = function ins(state, silent) { if (nextChar !== 0x2B/* + */ && lastChar !== 0x2B/* + */) { if (lastChar !== 0x20 && lastChar !== 0x0A) { // closing '++' + stack--; + } else if (nextChar !== 0x20 && nextChar !== 0x0A) { + // opening '++' + stack++; + } // else { + // // standalone ' ++ ' indented with spaces + //} + if (stack <= 0) { found = true; break; } diff --git a/lib/rules_inline/mark.js b/lib/rules_inline/mark.js index 6f153a7..ede557f 100644 --- a/lib/rules_inline/mark.js +++ b/lib/rules_inline/mark.js @@ -1,10 +1,11 @@ -// Process ++inserted text++ +// Process ==highlighted text== 'use strict'; -module.exports = function mark(state, silent) { +module.exports = function del(state, silent) { var found, pos, + stack, max = state.posMax, start = state.pos, lastChar, @@ -13,11 +14,6 @@ module.exports = function mark(state, silent) { if (state.src.charCodeAt(start) !== 0x3D/* = */) { return false; } if (start + 4 >= max) { return false; } if (state.src.charCodeAt(start + 1) !== 0x3D/* = */) { return false; } - - // make ins lower a priority tag with respect to links, same as ; - // this code also prevents recursion - if (silent && state.isInLabel) { return false; } - if (state.level >= state.options.maxNesting) { return false; } lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; @@ -37,6 +33,7 @@ module.exports = function mark(state, silent) { } state.pos = start + 2; + stack = 1; while (state.pos + 1 < max) { if (state.src.charCodeAt(state.pos) === 0x3D/* = */) { @@ -45,7 +42,15 @@ module.exports = function mark(state, silent) { nextChar = state.pos + 2 < max ? state.src.charCodeAt(state.pos + 2) : -1; if (nextChar !== 0x3D/* = */ && lastChar !== 0x3D/* = */) { if (lastChar !== 0x20 && lastChar !== 0x0A) { - // closing '++' + // closing '==' + stack--; + } else if (nextChar !== 0x20 && nextChar !== 0x0A) { + // opening '==' + stack++; + } // else { + // // standalone ' == ' indented with spaces + //} + if (stack <= 0) { found = true; break; } diff --git a/test/fixtures/remarkable/del.txt b/test/fixtures/remarkable/del.txt index 3a9abd8..62792e2 100644 --- a/test/fixtures/remarkable/del.txt +++ b/test/fixtures/remarkable/del.txt @@ -25,8 +25,8 @@ Strikeouts have the same priority as emphases: ~~**test~~** . -

**test**

-

~~test~~

+

~~test~~

+

**test**

. Strikeouts have the same priority as emphases with respect to links: diff --git a/test/fixtures/remarkable/ins.txt b/test/fixtures/remarkable/ins.txt index 33fa0ae..e08600e 100644 --- a/test/fixtures/remarkable/ins.txt +++ b/test/fixtures/remarkable/ins.txt @@ -25,8 +25,8 @@ Inserts have the same priority as emphases: ++**test++** . -

**test**

-

++test++

+

++test++

+

**test**

. Inserts have the same priority as emphases with respect to links: diff --git a/test/fixtures/remarkable/mark.txt b/test/fixtures/remarkable/mark.txt index a88341f..5e12916 100644 --- a/test/fixtures/remarkable/mark.txt +++ b/test/fixtures/remarkable/mark.txt @@ -25,8 +25,8 @@ Marks have the same priority as emphases: ==**test==** . -

**test**

-

==test==

+

==test==

+

**test**

. Marks have the same priority as emphases with respect to links: