diff --git a/lib/rules_block/heading.js b/lib/rules_block/heading.js index 56c0f22..f2d175d 100644 --- a/lib/rules_block/heading.js +++ b/lib/rules_block/heading.js @@ -4,7 +4,7 @@ module.exports = function heading(state, startLine, endLine, silent) { - var ch, level, + var ch, level, tmp, pos = state.bMarks[startLine] + state.tShift[startLine], max = state.eMarks[startLine]; @@ -24,27 +24,16 @@ module.exports = function heading(state, startLine, endLine, silent) { if (level > 6 || (pos < max && ch !== 0x20/* space */)) { return false; } - // skip spaces before heading text - pos = state.skipSpaces(pos); + if (silent) { return true; } - // Now pos contains offset of first heared char // Let's cut tails like ' ### ' from the end of string max = state.skipCharsBack(max, 0x20/* space */, pos); - max = state.skipCharsBack(max, 0x23/* # */, pos); - - if (max < state.eMarks[startLine] && - state.src.charCodeAt(max) === 0x23/* # */ && - state.src.charCodeAt(max - 1) === 0x5C/* \ */) { - max++; + tmp = state.skipCharsBack(max, 0x23/* # */, pos); + if (tmp > pos && state.src.charCodeAt(tmp - 1) === 0x20/* space */) { + max = tmp; } - // ## Foo #### - // ^^^ - max = state.skipCharsBack(max, 0x20/* space */, pos); - - if (silent) { return true; } - state.line = startLine + 1; state.tokens.push({ type: 'heading_open', diff --git a/test/fixtures/commonmark/bad.txt b/test/fixtures/commonmark/bad.txt index c7dd4f3..99133f8 100644 --- a/test/fixtures/commonmark/bad.txt +++ b/test/fixtures/commonmark/bad.txt @@ -1,37 +1,3 @@ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 619 - -. -# foo# -. -

foo#

-. - -error: - -

foo

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 628 - -. -### foo \### -## foo #\## -# foo \# -. -

foo ###

-

foo ###

-

foo #

-. - -error: - -

foo #

-

foo ##

-

foo #

- - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 3124 diff --git a/test/fixtures/commonmark/good.txt b/test/fixtures/commonmark/good.txt index 149d324..e5af533 100644 --- a/test/fixtures/commonmark/good.txt +++ b/test/fixtures/commonmark/good.txt @@ -390,6 +390,28 @@ src line: 611

foo ### b

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 619 + +. +# foo# +. +

foo#

+. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 628 + +. +### foo \### +## foo #\## +# foo \# +. +

foo ###

+

foo ###

+

foo #

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 641