feature(excludeTrailingPunctuationFromURLs): excludes trailing punctuation from auto linked URLs

Closes #266, #308
This commit is contained in:
Estevao Soares dos Santos 2016-12-01 15:25:46 +00:00
parent 0942b5e87d
commit d2fc2a0c5c
11 changed files with 48 additions and 9 deletions

View File

@ -210,7 +210,7 @@ var defaultOptions = showdown.getDefaultOptions();
<h3>foo</h3> <h3>foo</h3>
``` ```
* **simplifiedAutoLink**: (boolean) [default false] Turning this on will enable GFM autolink style. This means that * **simplifiedAutoLink**: (boolean) [default false] Turning this option on will enable automatic linking to urls. This means that
```md ```md
some text www.google.com some text www.google.com
@ -219,6 +219,17 @@ var defaultOptions = showdown.getDefaultOptions();
```` ````
<p>some text <a href="www.google.com">www.google.com</a> <p>some text <a href="www.google.com">www.google.com</a>
``` ```
* **excludeTrailingPunctuationFromURLs**: (boolean) [default false] This option excludes trailing punctuation from autolinking urls.
Punctuation excluded: `. ! ? ( )`. Only applies if **simplifiedAutoLink** option is set to `true`.
```md
check this link www.google.com!
```
will be parsed as
```html
<p>check this link <a href="www.google.com">www.google.com</a>!</p>
```
* **literalMidWordUnderscores**: (boolean) [default false] Turning this on will stop showdown from interpreting underscores in the middle of words as `<em>` and `<strong>` and instead treat them as literal underscores. * **literalMidWordUnderscores**: (boolean) [default false] Turning this on will stop showdown from interpreting underscores in the middle of words as `<em>` and `<strong>` and instead treat them as literal underscores.

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -36,6 +36,11 @@ function getDefaultOpts(simple) {
describe: 'Turn on/off GFM autolink style', describe: 'Turn on/off GFM autolink style',
type: 'boolean' type: 'boolean'
}, },
excludeTrailingPunctuationFromURLs: {
defaultValue: false,
describe: 'Excludes trailing punctuation from links generated with autoLinking',
type: 'boolean'
},
literalMidWordUnderscores: { literalMidWordUnderscores: {
defaultValue: false, defaultValue: false,
describe: 'Parse midword underscores as literal underscores', describe: 'Parse midword underscores as literal underscores',

View File

@ -12,6 +12,7 @@ var showdown = {},
omitExtraWLInCodeBlocks: true, omitExtraWLInCodeBlocks: true,
prefixHeaderId: 'user-content-', prefixHeaderId: 'user-content-',
simplifiedAutoLink: true, simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
literalMidWordUnderscores: true, literalMidWordUnderscores: true,
strikethrough: true, strikethrough: true,
tables: true, tables: true,

View File

@ -3,9 +3,10 @@ showdown.subParser('autoLinks', function (text, options, globals) {
text = globals.converter._dispatch('autoLinks.before', text, options, globals); text = globals.converter._dispatch('autoLinks.before', text, options, globals);
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi, 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, delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
simpleMailRegex = /(?:^|\s)([A-Za-z0-9!#$%&'*+-/=?^_`\{|}~\.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|\s)/gi, simpleMailRegex = /(?:^|\s)([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|\s)/gi,
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi; delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
text = text.replace(delimUrlRegex, replaceLink); text = text.replace(delimUrlRegex, replaceLink);
@ -14,20 +15,28 @@ showdown.subParser('autoLinks', function (text, options, globals) {
// Email addresses: <address@domain.foo> // Email addresses: <address@domain.foo>
if (options.simplifiedAutoLink) { if (options.simplifiedAutoLink) {
text = text.replace(simpleURLRegex, replaceLink); if (options.excludeTrailingPunctuationFromURLs) {
text = text.replace(simpleURLRegex2, replaceLink);
} else {
text = text.replace(simpleURLRegex, replaceLink);
}
text = text.replace(simpleMailRegex, replaceMail); text = text.replace(simpleMailRegex, replaceMail);
} }
function replaceLink(wm, link) { function replaceLink(wm, link, m2, m3, trailingPunctuation) {
var lnkTxt = link; var lnkTxt = link,
append = '';
if (/^www\./i.test(link)) { if (/^www\./i.test(link)) {
link = link.replace(/^www\./i, 'http://www.'); link = link.replace(/^www\./i, 'http://www.');
} }
return '<a href="' + link + '">' + lnkTxt + '</a>'; if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
append = trailingPunctuation;
}
return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
} }
function replaceMail(wholeMatch, m1) { function replaceMail(wholeMatch, mail) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1); var unescapedStr = showdown.subParser('unescapeSpecialChars')(mail);
return showdown.subParser('encodeEmailAddress')(unescapedStr); return showdown.subParser('encodeEmailAddress')(unescapedStr);
} }

View File

@ -0,0 +1,4 @@
<p>url <a href="http://www.google.com">http://www.google.com</a>.</p>
<p>url <a href="http://www.google.com">http://www.google.com</a>!</p>
<p>url <a href="http://www.google.com">http://www.google.com</a>? foo</p>
<p>url (<a href="http://www.google.com">http://www.google.com</a>) bazinga</p>

View File

@ -0,0 +1,7 @@
url http://www.google.com.
url http://www.google.com!
url http://www.google.com? foo
url (http://www.google.com) bazinga

View File

@ -37,6 +37,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true}); converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
} else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') { } else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
converter = new showdown.Converter({simpleLineBreaks: true}); converter = new showdown.Converter({simpleLineBreaks: true});
} else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
} else { } else {
converter = new showdown.Converter(); converter = new showdown.Converter();
} }