diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index 1822ccd..965995f 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -15,11 +15,11 @@ function isAlphaNum(code) { // note: in case if 4+ markers it is still not a valid emphasis, // should be treated as a special case function parseStart(state, start) { - var pos = start, lastChar, count, + var pos = start, lastChar, nextChar, count, max = state.posMax, marker = state.src.charCodeAt(start); - lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1; + lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; while (pos < max && state.src.charCodeAt(pos) === marker) { pos++; } if (pos >= max) { return -1; } @@ -40,7 +40,8 @@ function parseStart(state, start) { } // check condition 2, marker followed by whitespace - if (state.src.charCodeAt(pos) === 0x20) { return -1; } + nextChar = state.src.charCodeAt(pos); + if (nextChar === 0x20 || nextChar === 0x0A) { return -1; } if (marker === 0x5F /* _ */) { // check condition 3, if it's the beginning of the word @@ -61,7 +62,7 @@ function parseEnd(state, start) { max = state.posMax, marker = state.src.charCodeAt(start); - lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1; + lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; while (pos < max && state.src.charCodeAt(pos) === marker) { pos++; } count = pos - start; @@ -80,7 +81,7 @@ function parseEnd(state, start) { } // check condition 2, marker preceded by whitespace - if (lastChar === 0x20) { return -1; } + if (lastChar === 0x20 || lastChar === 0x0A) { return -1; } if (marker === 0x5F) { // check condition 3, if it's the end of the word diff --git a/lib/rules_inline/strikethrough.js b/lib/rules_inline/strikethrough.js index 74baeff..e496791 100644 --- a/lib/rules_inline/strikethrough.js +++ b/lib/rules_inline/strikethrough.js @@ -25,12 +25,12 @@ module.exports = function strikethrough(state) { if (state.level >= state.options.level) { return false; } - lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1; + lastChar = start > 0 ? state.src.charCodeAt(start - 1) : -1; nextChar = state.src.charCodeAt(start + 2); if (lastChar === 0x7E/* ~ */) { return false; } if (nextChar === 0x7E/* */) { return false; } - if (nextChar === 0x20/* */) { return false; } + if (nextChar === 0x20 || nextChar === 0x0A) { return false; } pos = start + 2; while (pos < max && state.src.charCodeAt(pos) === 0x7E/* ~ */) { pos++; } @@ -52,13 +52,13 @@ module.exports = function strikethrough(state) { while (state.pos + 1 < max) { if (state.src.charCodeAt(state.pos) === 0x7E/* ~ */) { if (state.src.charCodeAt(state.pos + 1) === 0x7E/* ~ */) { - lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1; + lastChar = state.src.charCodeAt(state.pos - 1); nextChar = state.pos + 2 < max ? state.src.charCodeAt(state.pos + 2) : -1; if (nextChar !== 0x7E/* ~ */ && lastChar !== 0x7E/* ~ */) { - if (lastChar !== 0x20) { + if (lastChar !== 0x20 && lastChar !== 0x0A) { // closing '~~' stack--; - } else if (nextChar !== 0x20) { + } else if (nextChar !== 0x20 && nextChar !== 0x0A) { // opening '~~' stack++; } // else { diff --git a/test/fixtures/remarkable/regression.txt b/test/fixtures/remarkable/regression.txt index 7263b62..9cce42c 100644 --- a/test/fixtures/remarkable/regression.txt +++ b/test/fixtures/remarkable/regression.txt @@ -17,3 +17,46 @@ Regression tests for link backtracking optimizations:
[[some unrelated text [link]
. +This is not a valid emphasis, because \n considered a whitespace: + +. +**test +** + +** +test** + +** +test +** +. +**test +**
+** +test**
+** +test +**
+. + +Same for strikethrough: + +. +~~test +~~ + +~~ +test~~ + +~~ +test +~~ +. +~~test +~~
+~~ +test~~
+~~ +test +~~
+.