From 25fea504f64057e6ca461819d73a2de29766b054 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 28 Apr 2015 13:49:53 +0300 Subject: [PATCH] Update emphasis to match CommonMark 0.19 --- lib/rules_inline/emphasis.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index 979e925..051d2d0 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -11,11 +11,11 @@ var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct; // parse sequence of emphasis markers, // "start" should point at a valid marker function scanDelims(state, start) { - var pos = start, lastChar, nextChar, count, + var pos = start, lastChar, nextChar, count, can_open, can_close, isLastWhiteSpace, isLastPunctChar, isNextWhiteSpace, isNextPunctChar, - can_open = true, - can_close = true, + left_flanking = true, + right_flanking = true, max = state.posMax, marker = state.src.charCodeAt(start); @@ -24,10 +24,6 @@ function scanDelims(state, start) { while (pos < max && state.src.charCodeAt(pos) === marker) { pos++; } - if (pos >= max) { - can_open = false; - } - count = pos - start; // treat end of the line as a whitespace @@ -40,27 +36,28 @@ function scanDelims(state, start) { isNextWhiteSpace = isWhiteSpace(nextChar); if (isNextWhiteSpace) { - can_open = false; + left_flanking = false; } else if (isNextPunctChar) { if (!(isLastWhiteSpace || isLastPunctChar)) { - can_open = false; + left_flanking = false; } } if (isLastWhiteSpace) { - can_close = false; + right_flanking = false; } else if (isLastPunctChar) { if (!(isNextWhiteSpace || isNextPunctChar)) { - can_close = false; + right_flanking = false; } } if (marker === 0x5F /* _ */) { - if (can_open && can_close) { - // "_" inside a word can neither open nor close an emphasis - can_open = false; - can_close = isNextPunctChar; - } + // "_" inside a word can neither open nor close an emphasis + can_open = left_flanking && (!right_flanking || isLastPunctChar); + can_close = right_flanking && (!left_flanking || isNextPunctChar); + } else { + can_open = left_flanking; + can_close = right_flanking; } return {