Browse Source

fallback to reference if a link is not valid

This commit makes martdown-it pass a new example introduced in cfc8416447

```
[foo](not a link)

[foo]: /url1
.
<p><a href="/url1">foo</a>(not a link)</p>
```

Ref: https://github.com/jgm/CommonMark/issues/427
pull/306/head
Igor Bochkariov 8 years ago
committed by Alex Kocharin
parent
commit
ebc9f50759
  1. 14
      lib/rules_inline/link.js

14
lib/rules_inline/link.js

@ -20,7 +20,8 @@ module.exports = function link(state, silent) {
href = '', href = '',
oldPos = state.pos, oldPos = state.pos,
max = state.posMax, max = state.posMax,
start = state.pos; start = state.pos,
parseReference = true;
if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; } if (state.src.charCodeAt(state.pos) !== 0x5B/* [ */) { return false; }
@ -36,6 +37,9 @@ module.exports = function link(state, silent) {
// Inline link // Inline link
// //
// might have found a valid shortcut link, disable reference parsing
parseReference = false;
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^ skipping these spaces // ^^ skipping these spaces
pos++; pos++;
@ -84,11 +88,13 @@ module.exports = function link(state, silent) {
} }
if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) { if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) {
state.pos = oldPos; // parsing a valid shortcut link failed, fallback to reference
return false; parseReference = true;
} }
pos++; pos++;
} else { }
if (parseReference) {
// //
// Link reference // Link reference
// //

Loading…
Cancel
Save