Skip to content

Commit

Permalink
feature(excludeTrailingPunctuationFromURLs): excludes trailing punctu…
Browse files Browse the repository at this point in the history
…ation from auto linked URLs

Closes #266, #308
  • Loading branch information
tivie committed Dec 1, 2016
1 parent 0942b5e commit d2fc2a0
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 22 deletions.
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -210,7 +210,7 @@ var defaultOptions = showdown.getDefaultOptions();
<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
some text www.google.com
Expand All @@ -219,6 +219,17 @@ var defaultOptions = showdown.getDefaultOptions();
````
<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.
Expand Down
33 changes: 24 additions & 9 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.

4 changes: 2 additions & 2 deletions 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.

5 changes: 5 additions & 0 deletions src/options.js
Expand Up @@ -36,6 +36,11 @@ function getDefaultOpts(simple) {
describe: 'Turn on/off GFM autolink style',
type: 'boolean'
},
excludeTrailingPunctuationFromURLs: {
defaultValue: false,
describe: 'Excludes trailing punctuation from links generated with autoLinking',
type: 'boolean'
},
literalMidWordUnderscores: {
defaultValue: false,
describe: 'Parse midword underscores as literal underscores',
Expand Down
1 change: 1 addition & 0 deletions src/showdown.js
Expand Up @@ -12,6 +12,7 @@ var showdown = {},
omitExtraWLInCodeBlocks: true,
prefixHeaderId: 'user-content-',
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
Expand Down
25 changes: 17 additions & 8 deletions src/subParsers/autoLinks.js
Expand Up @@ -3,9 +3,10 @@ showdown.subParser('autoLinks', function (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,
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;

text = text.replace(delimUrlRegex, replaceLink);
Expand All @@ -14,20 +15,28 @@ showdown.subParser('autoLinks', function (text, options, globals) {
// Email addresses: <address@domain.foo>

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);
}

function replaceLink(wm, link) {
var lnkTxt = link;
function replaceLink(wm, link, m2, m3, trailingPunctuation) {
var lnkTxt = link,
append = '';
if (/^www\./i.test(link)) {
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) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
function replaceMail(wholeMatch, mail) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(mail);
return showdown.subParser('encodeEmailAddress')(unescapedStr);
}

Expand Down
4 changes: 4 additions & 0 deletions test/features/excludeTrailingPunctuationFromURLs-option.html
@@ -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>
7 changes: 7 additions & 0 deletions test/features/excludeTrailingPunctuationFromURLs-option.md
@@ -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
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Expand Up @@ -37,6 +37,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
} else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
converter = new showdown.Converter({simpleLineBreaks: true});
} else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
} else {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit d2fc2a0

Please sign in to comment.