diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index 1bfb6fb..ad6e53d 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -24,21 +24,16 @@ function scanDelims(state, start) { if (pos >= max) { can_open = false; } count = pos - start; - if (count >= 4) { - // sequence of four or more unescaped markers can't start/end an emphasis - can_open = can_close = false; - } else { - nextChar = pos < max ? state.src.charCodeAt(pos) : -1; - - // check whitespace conditions - if (nextChar === 0x20 || nextChar === 0x0A) { can_open = false; } - if (lastChar === 0x20 || lastChar === 0x0A) { can_close = false; } - - if (marker === 0x5F /* _ */) { - // check if we aren't inside the word - if (isAlphaNum(lastChar)) { can_open = false; } - if (isAlphaNum(nextChar)) { can_close = false; } - } + nextChar = pos < max ? state.src.charCodeAt(pos) : -1; + + // check whitespace conditions + if (nextChar === 0x20 || nextChar === 0x0A) { can_open = false; } + if (lastChar === 0x20 || lastChar === 0x0A) { can_close = false; } + + if (marker === 0x5F /* _ */) { + // check if we aren't inside the word + if (isAlphaNum(lastChar)) { can_open = false; } + if (isAlphaNum(nextChar)) { can_close = false; } } return { @@ -126,19 +121,17 @@ module.exports = function emphasis(state, silent) { state.pos = start + startCount; if (!silent) { - if (startCount === 2 || startCount === 3) { + // we have `startCount` starting and ending markers, + // now trying to serialize them into tokens + for (count = startCount; count > 1; count -= 2) { state.push({ type: 'strong_open', level: state.level++ }); } - if (startCount === 1 || startCount === 3) { - state.push({ type: 'em_open', level: state.level++ }); - } + if (count % 2) { state.push({ type: 'em_open', level: state.level++ }); } state.parser.tokenize(state); - if (startCount === 1 || startCount === 3) { - state.push({ type: 'em_close', level: --state.level }); - } - if (startCount === 2 || startCount === 3) { + if (count % 2) { state.push({ type: 'em_close', level: --state.level }); } + for (count = startCount; count > 1; count -= 2) { state.push({ type: 'strong_close', level: --state.level }); } } diff --git a/test/fixtures/commonmark/bad.txt b/test/fixtures/commonmark/bad.txt index a00e6d9..beb8d30 100644 --- a/test/fixtures/commonmark/bad.txt +++ b/test/fixtures/commonmark/bad.txt @@ -1224,146 +1224,6 @@ error: -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 4844 - -. -____foo__ bar__ -. -

foo bar

-. - -error: - -

____foo__ bar__

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 4850 - -. -**foo **bar**** -. -

foo bar

-. - -error: - -

**foo **bar****

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 4977 - -. -****foo* -. -

***foo

-. - -error: - -

****foo*

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 4989 - -. -*foo**** -. -

foo***

-. - -error: - -

*foo****

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5056 - -. -____foo_ -. -

___foo

-. - -error: - -

____foo_

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5068 - -. -_foo____ -. -

foo___

-. - -error: - -

_foo____

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5104 - -. -****foo**** -. -

foo

-. - -error: - -

****foo****

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5110 - -. -____foo____ -. -

foo

-. - -error: - -

____foo____

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5120 - -. -******foo****** -. -

foo

-. - -error: - -

******foo******

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 5134 - -. -_____foo_____ -. -

foo

-. - -error: - -

_____foo_____

- - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 5521 diff --git a/test/fixtures/commonmark/good.txt b/test/fixtures/commonmark/good.txt index e94ff47..2424e11 100644 --- a/test/fixtures/commonmark/good.txt +++ b/test/fixtures/commonmark/good.txt @@ -3290,6 +3290,24 @@ __foo __bar__ baz__

foo bar baz

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 4844 + +. +____foo__ bar__ +. +

foo bar

+. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 4850 + +. +**foo **bar**** +. +

foo bar

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 4856 @@ -3445,6 +3463,15 @@ src line: 4971

*foo

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 4977 + +. +****foo* +. +

***foo

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 4983 @@ -3454,6 +3481,15 @@ src line: 4983

foo*

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 4989 + +. +*foo**** +. +

foo***

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 4998 @@ -3535,6 +3571,15 @@ ___foo__

_foo

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5056 + +. +____foo_ +. +

___foo

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 5062 @@ -3544,6 +3589,15 @@ __foo___

foo_

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5068 + +. +_foo____ +. +

foo___

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 5077 @@ -3580,6 +3634,33 @@ _*foo*_

foo

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5104 + +. +****foo**** +. +

foo

+. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5110 + +. +____foo____ +. +

foo

+. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5120 + +. +******foo****** +. +

foo

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 5128 @@ -3589,6 +3670,15 @@ src line: 5128

foo

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 5134 + +. +_____foo_____ +. +

foo

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 5142