From e519e6ac198221ace43685360647a3fce666df81 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Fri, 5 Jul 2019 17:28:46 +0300 Subject: [PATCH] Fix emphasis matching where delimiters are multiple of 3 This is now allowed as per commonmark 0.29 spec: ex***amp***le See also: https://github.com/commonmark/commonmark-spec/commit/83ed53e12aa2e011b04474825bfb22a40682e4a0 fix https://github.com/markdown-it/markdown-it/issues/561 --- lib/rules_inline/balance_pairs.js | 19 +++++++++++++++---- test/fixtures/commonmark/bad.txt | 28 ---------------------------- test/fixtures/commonmark/good.txt | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/rules_inline/balance_pairs.js b/lib/rules_inline/balance_pairs.js index f2a5207..93d3100 100644 --- a/lib/rules_inline/balance_pairs.js +++ b/lib/rules_inline/balance_pairs.js @@ -23,11 +23,22 @@ module.exports = function link_pairs(state) { currDelim.end < 0 && currDelim.level === lastDelim.level) { + var odd_match = false; + // typeofs are for backward compatibility with plugins - var odd_match = (currDelim.close || lastDelim.open) && - typeof currDelim.length !== 'undefined' && - typeof lastDelim.length !== 'undefined' && - (currDelim.length + lastDelim.length) % 3 === 0; + if ((currDelim.close || lastDelim.open) && + typeof currDelim.length !== 'undefined' && + typeof lastDelim.length !== 'undefined') { + + // from spec: + // sum of the lengths [...] must not be a multiple of 3 + // unless both lengths are multiples of 3 + if ((currDelim.length + lastDelim.length) % 3 === 0) { + if (currDelim.length % 3 !== 0 || lastDelim.length % 3 !== 0) { + odd_match = true; + } + } + } if (!odd_match) { lastDelim.jump = i - j; diff --git a/test/fixtures/commonmark/bad.txt b/test/fixtures/commonmark/bad.txt index 5d22760..6123b52 100644 --- a/test/fixtures/commonmark/bad.txt +++ b/test/fixtures/commonmark/bad.txt @@ -65,31 +65,3 @@ error: -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 6912 - -. -foo***bar***baz -. -

foobarbaz

-. - -error: - -

foo***bar***baz

- - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 6918 - -. -foo******bar*********baz -. -

foobar***baz

-. - -error: - -

foo******bar*********baz

- - diff --git a/test/fixtures/commonmark/good.txt b/test/fixtures/commonmark/good.txt index 6e945a7..e36b610 100644 --- a/test/fixtures/commonmark/good.txt +++ b/test/fixtures/commonmark/good.txt @@ -5432,6 +5432,24 @@ src line: 6901

foobar

. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 6912 + +. +foo***bar***baz +. +

foobarbaz

+. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 6918 + +. +foo******bar*********baz +. +

foobar***baz

+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 6927