Skip to content

Commit

Permalink
when discarding a trailing paren from an auto-linked URL, we have to …
Browse files Browse the repository at this point in the history
…re-check the last character to exclude punctuation (http://meta.stackoverflow.com/q/179990)
  • Loading branch information
balpha committed May 13, 2013
1 parent e3db84f commit 7e1ae49
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Markdown.Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,12 @@ else
text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback);
return text;
}


var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]",
charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]",
autoLinkRegex = new RegExp("(=\"|<)?\\b(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi"),
endCharRegex = new RegExp(charEndingUrl, "i");

function handleTrailingParens(wholeMatch, lookbehind, protocol, link) {
if (lookbehind)
return wholeMatch;
Expand All @@ -1258,10 +1263,16 @@ else
return "";
});
}

if (tail) {
var lastChar = link.charAt(link.length - 1);
if (!endCharRegex.test(lastChar)) {
tail = lastChar + tail;
link = link.substr(0, link.length - 1);
}
}
return "<" + protocol + link + ">" + tail;
}

function _DoAutoLinks(text) {

// note that at this point, all other URL in the text are already hyperlinked as <a href=""></a>
Expand All @@ -1271,7 +1282,7 @@ else
// must be preceded by a non-word character (and not by =" or <) and followed by non-word/EOF character
// simulating the lookbehind in a consuming way is okay here, since a URL can neither and with a " nor
// with a <, so there is no risk of overlapping matches.
text = text.replace(/(="|<)?\b(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\])])(?=$|\W)/gi, handleTrailingParens);
text = text.replace(autoLinkRegex, handleTrailingParens);

// autolink anything like <http://example.com>

Expand Down

0 comments on commit 7e1ae49

Please sign in to comment.