Browse Source

Allow sequence of 4+ markers in emphases

In old CommonMark spec they were always parsed literally.
pull/14/head
Alex Kocharin 10 years ago
committed by Vitaly Puzrin
parent
commit
e12d5cbdad
  1. 39
      lib/rules_inline/emphasis.js
  2. 140
      test/fixtures/commonmark/bad.txt
  3. 90
      test/fixtures/commonmark/good.txt

39
lib/rules_inline/emphasis.js

@ -24,21 +24,16 @@ function scanDelims(state, start) {
if (pos >= max) { can_open = false; } if (pos >= max) { can_open = false; }
count = pos - start; count = pos - start;
if (count >= 4) { nextChar = pos < max ? state.src.charCodeAt(pos) : -1;
// sequence of four or more unescaped markers can't start/end an emphasis
can_open = can_close = false; // check whitespace conditions
} else { if (nextChar === 0x20 || nextChar === 0x0A) { can_open = false; }
nextChar = pos < max ? state.src.charCodeAt(pos) : -1; if (lastChar === 0x20 || lastChar === 0x0A) { can_close = false; }
// check whitespace conditions if (marker === 0x5F /* _ */) {
if (nextChar === 0x20 || nextChar === 0x0A) { can_open = false; } // check if we aren't inside the word
if (lastChar === 0x20 || lastChar === 0x0A) { can_close = false; } if (isAlphaNum(lastChar)) { can_open = false; }
if (isAlphaNum(nextChar)) { 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 { return {
@ -126,19 +121,17 @@ module.exports = function emphasis(state, silent) {
state.pos = start + startCount; state.pos = start + startCount;
if (!silent) { 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++ }); state.push({ type: 'strong_open', level: state.level++ });
} }
if (startCount === 1 || startCount === 3) { if (count % 2) { state.push({ type: 'em_open', level: state.level++ }); }
state.push({ type: 'em_open', level: state.level++ });
}
state.parser.tokenize(state); state.parser.tokenize(state);
if (startCount === 1 || startCount === 3) { if (count % 2) { state.push({ type: 'em_close', level: --state.level }); }
state.push({ type: 'em_close', level: --state.level }); for (count = startCount; count > 1; count -= 2) {
}
if (startCount === 2 || startCount === 3) {
state.push({ type: 'strong_close', level: --state.level }); state.push({ type: 'strong_close', level: --state.level });
} }
} }

140
test/fixtures/commonmark/bad.txt

@ -1224,146 +1224,6 @@ error:
</ul> </ul>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4844
.
____foo__ bar__
.
<p><strong><strong>foo</strong> bar</strong></p>
.
error:
<p>____foo__ bar__</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4850
.
**foo **bar****
.
<p><strong>foo <strong>bar</strong></strong></p>
.
error:
<p>**foo **bar****</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4977
.
****foo*
.
<p>***<em>foo</em></p>
.
error:
<p>****foo*</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4989
.
*foo****
.
<p><em>foo</em>***</p>
.
error:
<p>*foo****</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5056
.
____foo_
.
<p>___<em>foo</em></p>
.
error:
<p>____foo_</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5068
.
_foo____
.
<p><em>foo</em>___</p>
.
error:
<p>_foo____</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5104
.
****foo****
.
<p><strong><strong>foo</strong></strong></p>
.
error:
<p>****foo****</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5110
.
____foo____
.
<p><strong><strong>foo</strong></strong></p>
.
error:
<p>____foo____</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5120
.
******foo******
.
<p><strong><strong><strong>foo</strong></strong></strong></p>
.
error:
<p>******foo******</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5134
.
_____foo_____
.
<p><strong><strong><em>foo</em></strong></strong></p>
.
error:
<p>_____foo_____</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5521 src line: 5521

90
test/fixtures/commonmark/good.txt

@ -3290,6 +3290,24 @@ __foo __bar__ baz__
<p><strong>foo <strong>bar</strong> baz</strong></p> <p><strong>foo <strong>bar</strong> baz</strong></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4844
.
____foo__ bar__
.
<p><strong><strong>foo</strong> bar</strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4850
.
**foo **bar****
.
<p><strong>foo <strong>bar</strong></strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4856 src line: 4856
@ -3445,6 +3463,15 @@ src line: 4971
<p>*<strong>foo</strong></p> <p>*<strong>foo</strong></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4977
.
****foo*
.
<p>***<em>foo</em></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4983 src line: 4983
@ -3454,6 +3481,15 @@ src line: 4983
<p><strong>foo</strong>*</p> <p><strong>foo</strong>*</p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4989
.
*foo****
.
<p><em>foo</em>***</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 4998 src line: 4998
@ -3535,6 +3571,15 @@ ___foo__
<p>_<strong>foo</strong></p> <p>_<strong>foo</strong></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5056
.
____foo_
.
<p>___<em>foo</em></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5062 src line: 5062
@ -3544,6 +3589,15 @@ __foo___
<p><strong>foo</strong>_</p> <p><strong>foo</strong>_</p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5068
.
_foo____
.
<p><em>foo</em>___</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5077 src line: 5077
@ -3580,6 +3634,33 @@ _*foo*_
<p><em><em>foo</em></em></p> <p><em><em>foo</em></em></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5104
.
****foo****
.
<p><strong><strong>foo</strong></strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5110
.
____foo____
.
<p><strong><strong>foo</strong></strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5120
.
******foo******
.
<p><strong><strong><strong>foo</strong></strong></strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5128 src line: 5128
@ -3589,6 +3670,15 @@ src line: 5128
<p><strong><em>foo</em></strong></p> <p><strong><em>foo</em></strong></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5134
.
_____foo_____
.
<p><strong><strong><em>foo</em></strong></strong></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 5142 src line: 5142

Loading…
Cancel
Save