-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(simpleAutoLinks): URLs with emphasis/strikethrough are parsed
correctly When a user enters a URL with emphasis or strikethrough, the html output were incorrect. Now, URLs inside emphasis or strikethrough are parsed corerctly Closes #347
- Loading branch information
Showing
25 changed files
with
382 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,74 @@ | ||
// 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, | ||
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) { | ||
var lnkTxt = link, | ||
append = ''; | ||
if (/^www\./i.test(link)) { | ||
link = link.replace(/^www\./i, 'http://www.'); | ||
} | ||
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) { | ||
append = trailingPunctuation; | ||
} | ||
return '<a href="' + link + '">' + lnkTxt + '</a>' + append; | ||
}; | ||
}, | ||
|
||
replaceMail = function (options, globals) { | ||
'use strict'; | ||
return function (wholeMatch, b, mail) { | ||
var href = 'mailto:'; | ||
b = b || ''; | ||
mail = showdown.subParser('unescapeSpecialChars')(mail, options, globals); | ||
if (options.encodeEmails) { | ||
href = showdown.helper.encodeEmailAddress(href + mail); | ||
mail = showdown.helper.encodeEmailAddress(mail); | ||
} else { | ||
href = href + mail; | ||
} | ||
return b + '<a href="' + href + '">' + mail + '</a>'; | ||
}; | ||
}; | ||
|
||
showdown.subParser('autoLinks', function (text, options, globals) { | ||
'use strict'; | ||
|
||
text = globals.converter._dispatch('autoLinks.before', text, options, globals); | ||
|
||
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi, | ||
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?()]?)(?=\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; | ||
|
||
text = text.replace(delimUrlRegex, replaceLink); | ||
text = text.replace(delimMailRegex, replaceMail); | ||
// simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi, | ||
// Email addresses: <address@domain.foo> | ||
|
||
if (options.simplifiedAutoLink) { | ||
if (options.excludeTrailingPunctuationFromURLs) { | ||
text = text.replace(simpleURLRegex2, replaceLink); | ||
} else { | ||
text = text.replace(simpleURLRegex, replaceLink); | ||
} | ||
text = text.replace(simpleMailRegex, replaceMail); | ||
} | ||
text = text.replace(delimUrlRegex, replaceLink(options)); | ||
text = text.replace(delimMailRegex, replaceMail(options, globals)); | ||
|
||
text = globals.converter._dispatch('autoLinks.after', text, options, globals); | ||
|
||
function replaceLink (wm, link, m2, m3, trailingPunctuation) { | ||
var lnkTxt = link, | ||
append = ''; | ||
if (/^www\./i.test(link)) { | ||
link = link.replace(/^www\./i, 'http://www.'); | ||
} | ||
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) { | ||
append = trailingPunctuation; | ||
} | ||
return '<a href="' + link + '">' + lnkTxt + '</a>' + append; | ||
return text; | ||
}); | ||
|
||
showdown.subParser('simplifiedAutoLinks', function (text, options, globals) { | ||
'use strict'; | ||
|
||
if (!options.simplifiedAutoLink) { | ||
return text; | ||
} | ||
|
||
function replaceMail (wholeMatch, b, mail) { | ||
var href = 'mailto:'; | ||
b = b || ''; | ||
mail = showdown.subParser('unescapeSpecialChars')(mail, options, globals); | ||
if (options.encodeEmails) { | ||
href = showdown.helper.encodeEmailAddress(href + mail); | ||
mail = showdown.helper.encodeEmailAddress(mail); | ||
} else { | ||
href = href + mail; | ||
} | ||
return b + '<a href="' + href + '">' + mail + '</a>'; | ||
text = globals.converter._dispatch('simplifiedAutoLinks.before', text, options, globals); | ||
|
||
if (options.excludeTrailingPunctuationFromURLs) { | ||
text = text.replace(simpleURLRegex2, replaceLink(options)); | ||
} else { | ||
text = text.replace(simpleURLRegex, replaceLink(options)); | ||
} | ||
text = text.replace(simpleMailRegex, replaceMail(options, globals)); | ||
|
||
text = globals.converter._dispatch('autoLinks.after', text, options, globals); | ||
text = globals.converter._dispatch('simplifiedAutoLinks.after', text, options, globals); | ||
|
||
return text; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.