Skip to content

Commit

Permalink
fallback to reference if a link is not valid
Browse files Browse the repository at this point in the history
This commit makes martdown-it pass a new example introduced in commonmark/commonmark-spec@cfc8416

```
[foo](not a link)

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

Ref: commonmark/commonmark-spec#427
  • Loading branch information
ujifgc authored and jmalarcon committed Jan 13, 2017
1 parent 007619d commit 6b77146
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/rules_inline/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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]( <href> "title" )
// ^^ skipping these spaces
pos++;
Expand Down Expand Up @@ -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
//
Expand Down

0 comments on commit 6b77146

Please sign in to comment.