Skip to content

Commit

Permalink
use function to check for end of link
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Feb 7, 2019
1 parent cc8a452 commit 77a7826
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ inline.tag = edit(inline.tag)
.getRegex();

inline._label = /(?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?/;
inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\()]*\)|[^\s\x00-\x1f()\\])*?)/;
inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)/;
inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;

inline.link = edit(inline.link)
Expand Down Expand Up @@ -719,6 +719,12 @@ InlineLexer.prototype.output = function(src) {

// link
if (cap = this.rules.link.exec(src)) {
var lastParenIndex = findClosingBracket(cap[2], '()');
if (lastParenIndex > -1) {
var removeChars = cap[2].length - lastParenIndex;
cap[2] = cap[2].substring(0, lastParenIndex);
cap[0] = cap[0].substring(0, cap[0].length - removeChars);
}
src = src.substring(cap[0].length);
this.inLink = true;
href = cap[2];
Expand Down Expand Up @@ -1503,6 +1509,27 @@ function rtrim(str, c, invert) {
return str.substr(0, str.length - suffLen);
}

function findClosingBracket(str, b) {
if (str.indexOf(b[1]) === -1) {
return -1;
}
var level = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === '\\') {
i++;
} else if (str[i] === b[0]) {
level++;
continue;
} else if (str[i] === b[1]) {
level--;
if (level < 0) {
return i;
}
}
}
return -1;
}

/**
* Marked
*/
Expand Down
2 changes: 1 addition & 1 deletion test/specs/commonmark/commonmark-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ describe('CommonMark 0.28 Links', function() {
var section = 'Links';

// var shouldPassButFails = [];
var shouldPassButFails = [468, 474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509];
var shouldPassButFails = [474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509];

var willNotBeAttemptedByCoreTeam = [];

Expand Down

0 comments on commit 77a7826

Please sign in to comment.