Skip to content

Commit

Permalink
fix(autolinks): prevent _ and * to be parsed in links
Browse files Browse the repository at this point in the history
Closes #444
  • Loading branch information
tivie committed Oct 24, 2017
1 parent 32800a1 commit 61929bb
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 23 deletions.
23 changes: 13 additions & 10 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/helpers.js
Expand Up @@ -376,5 +376,5 @@ if (typeof(console) === 'undefined') {
* We declare some common regexes to improve performance
*/
showdown.helper.regexes = {
asteriskDashAndColon: /([*_:])/g
asteriskDashAndColon: /([*_:~])/g
};
17 changes: 9 additions & 8 deletions src/subParsers/autoLinks.js
@@ -1,19 +1,20 @@
// url allowed chars [a-z\d_.~:/?#[]@!$&'()*+,;=-]

var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?,()\[\]]?)(?=\s|$)(?!["<>])/gi,
//simpleURLRegex3 = /\b(((https?|ftp):\/\/|www\.)[a-z\d.-]+\.[a-z\d_.~:/?#\[\]@!$&'()*+,;=-]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)()>/gi,
var simpleURLRegex = /([*~_]+|\b)(((https?|ftp|dict):\/\/|www\.)[^'">\s]+?\.[^'">\s]+?)()(\1)?(?=\s|$)(?!["<>])/gi,
simpleURLRegex2 = /([*~_]+|\b)(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?,()\[\]])?(\1)?(?=\s|$)(?!["<>])/gi,
delimUrlRegex = /()<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)()>()/gi,
simpleMailRegex = /(^|\s)(?:mailto:)?([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?=$|\s)/gmi,
delimMailRegex = /<()(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,

replaceLink = function (options) {
'use strict';

return function (wm, link, m2, m3, trailingPunctuation) {
return function (wm, leadingMagicChars, link, m2, m3, trailingPunctuation, trailingMagicChars) {
link = link.replace(showdown.helper.regexes.asteriskDashAndColon, showdown.helper.escapeCharactersCallback);
var lnkTxt = link,
append = '',
target = '';
target = '',
lmc = leadingMagicChars || '',
tmc = trailingMagicChars || '';
if (/^www\./i.test(link)) {
link = link.replace(/^www\./i, 'http://www.');
}
Expand All @@ -23,7 +24,7 @@ var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(
if (options.openLinksInNewWindow) {
target = ' target="¨E95Eblank"';
}
return '<a href="' + link + '"' + target + '>' + lnkTxt + '</a>' + append;
return lmc + '<a href="' + link + '"' + target + '>' + lnkTxt + '</a>' + append + tmc;
};
},

Expand Down
2 changes: 2 additions & 0 deletions src/subParsers/italicsAndBold.js
Expand Up @@ -8,9 +8,11 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
// called "catastrophic backtrace". Ominous!

function parseInside (txt, left, right) {
/*
if (options.simplifiedAutoLink) {
txt = showdown.subParser('simplifiedAutoLinks')(txt, options, globals);
}
*/
return left + txt + right;
}

Expand Down
2 changes: 1 addition & 1 deletion src/subParsers/spanGamut.js
Expand Up @@ -19,9 +19,9 @@ showdown.subParser('spanGamut', function (text, options, globals) {
// Must come after anchors, because you can use < and >
// delimiters in inline links like [this](<url>).
text = showdown.subParser('autoLinks')(text, options, globals);
text = showdown.subParser('simplifiedAutoLinks')(text, options, globals);
text = showdown.subParser('italicsAndBold')(text, options, globals);
text = showdown.subParser('strikethrough')(text, options, globals);
text = showdown.subParser('simplifiedAutoLinks')(text, options, globals);

// we need to hash HTML tags inside spans
text = showdown.subParser('hashHTMLSpans')(text, options, globals);
Expand Down
@@ -0,0 +1 @@
<p><a href="http://www.foobar.com/blegh#**foobar**bazinga">http://www.foobar.com/blegh#**foobar**bazinga</a></p>
@@ -0,0 +1 @@
http://www.foobar.com/blegh#**foobar**bazinga

0 comments on commit 61929bb

Please sign in to comment.