From c9dd942246f7dd991fb3c8fc86b42edcbce4d949 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Mon, 14 Dec 2020 11:48:08 +0300 Subject: [PATCH] Fix crash when processing strikethrough close https://github.com/markdown-it/markdown-it/issues/742 --- CHANGELOG.md | 5 +++++ lib/rules_inline/balance_pairs.js | 4 ++++ lib/rules_inline/strikethrough.js | 4 ++-- test/fixtures/markdown-it/strikethrough.txt | 7 +++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba066aea6..697e82468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [12.0.4] - WIP +### Fixed +- Fix crash introduced in `12.0.3` when processing strikethrough (`~~`) and similar plugins, #742. + + ## [12.0.3] - 2020-12-07 ### Fixed - `[]()` is no longer a valid link. diff --git a/lib/rules_inline/balance_pairs.js b/lib/rules_inline/balance_pairs.js index 4278c8ec8..bd8355085 100644 --- a/lib/rules_inline/balance_pairs.js +++ b/lib/rules_inline/balance_pairs.js @@ -29,6 +29,10 @@ function processDelimiters(state, delimiters) { minOpenerIdx = openersBottom[closer.marker][closer.length % 3]; openerIdx = closerIdx - closer.jump - 1; + + // avoid crash if `closer.jump` is pointing outside of the array, see #742 + if (openerIdx < -1) openerIdx = -1; + newMinOpenerIdx = openerIdx; for (; openerIdx > minOpenerIdx; openerIdx -= opener.jump + 1) { diff --git a/lib/rules_inline/strikethrough.js b/lib/rules_inline/strikethrough.js index f3afe4a3d..cb8944fc3 100644 --- a/lib/rules_inline/strikethrough.js +++ b/lib/rules_inline/strikethrough.js @@ -32,8 +32,8 @@ module.exports.tokenize = function strikethrough(state, silent) { state.delimiters.push({ marker: marker, - length: 0, // disable "rule of 3" length checks meant for emphasis - jump: i, + length: 0, // disable "rule of 3" length checks meant for emphasis + jump: i / 2, // for `~~` 1 marker = 2 characters token: state.tokens.length - 1, end: -1, open: scanned.can_open, diff --git a/test/fixtures/markdown-it/strikethrough.txt b/test/fixtures/markdown-it/strikethrough.txt index ec2678b9e..3e3b17ba6 100644 --- a/test/fixtures/markdown-it/strikethrough.txt +++ b/test/fixtures/markdown-it/strikethrough.txt @@ -127,3 +127,10 @@ Coverage: single tilde .

~a~

. + +Regression test for #742: +. +-~~~~;~~~~~~ +. +

-;~~

+.