From ebc9f50759e67149f3cfe3c836853d8a277dfde0 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Sun, 27 Nov 2016 11:42:00 +0400 Subject: [PATCH] fallback to reference if a link is not valid This commit makes martdown-it pass a new example introduced in https://github.com/jgm/CommonMark/commit/cfc84164475d3bec8be9482c21a705adc93a54f5 ``` [foo](not a link) [foo]: /url1 .

foo(not a link)

``` Ref: https://github.com/jgm/CommonMark/issues/427 --- lib/rules_inline/link.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/rules_inline/link.js b/lib/rules_inline/link.js index 4b9056f..7c9834c 100644 --- a/lib/rules_inline/link.js +++ b/lib/rules_inline/link.js @@ -20,7 +20,8 @@ module.exports = function link(state, silent) { href = '', oldPos = state.pos, max = state.posMax, - start = state.pos; + start = state.pos, + parseReference = true; if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; } @@ -36,6 +37,9 @@ module.exports = function link(state, silent) { // Inline link // + // might have found a valid shortcut link, disable reference parsing + parseReference = false; + // [link]( "title" ) // ^^ skipping these spaces pos++; @@ -84,11 +88,13 @@ module.exports = function link(state, silent) { } if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) { - state.pos = oldPos; - return false; + // parsing a valid shortcut link failed, fallback to reference + parseReference = true; } pos++; - } else { + } + + if (parseReference) { // // Link reference //