Skip to content

Commit

Permalink
feat(simplifiedAutoLink): add support for GFM autolinks
Browse files Browse the repository at this point in the history
Github Flavored Markdown detects urls and mails embeded in the text without any extra markup or delimiter.
This commit adds this feature to showdown through an option called "simplifiedAutoLink".
Related to #164
  • Loading branch information
tivie committed Jul 11, 2015
1 parent 1e2bbcf commit cff0237
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 53 deletions.
60 changes: 51 additions & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,42 @@ module.exports = function (grunt) {
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
},
issues: {
src: 'test/node/testsuite.issues.js',
options: {
globals: ['should'],
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
}
}
},
issues: {
src: 'test/node/testsuite.issues.js',
options: {
globals: ['should'],
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
}
},
standard: {
src: 'test/node/testsuite.standard.js',
options: {
globals: ['should'],
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
}
},
features: {
src: 'test/node/testsuite.features.js',
options: {
globals: ['should'],
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
}
},
single: {
src: 'test/node/**/*.js',
options: {
globals: ['should'],
timeout: 3000,
ignoreLeaks: false,
reporter: 'spec'
}
}
}
Expand All @@ -113,6 +140,21 @@ module.exports = function (grunt) {

require('load-grunt-tasks')(grunt);

grunt.registerTask('single-test', function (grep) {
'use strict';
grunt.config.merge({
simplemocha: {
single: {
options: {
grep: grep
}
}
}
});

grunt.task.run('simplemocha:node');
});

grunt.registerTask('concatenate', ['concat:dist']);
grunt.registerTask('lint', ['jshint', 'jscs']);
grunt.registerTask('test', ['lint', 'concat:test', 'simplemocha:node', 'clean']);
Expand Down
40 changes: 20 additions & 20 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.

3 changes: 2 additions & 1 deletion src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var showdown = {},
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false
parseImgDimensions: false,
simplifiedAutoLink: false
},
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P

Expand Down
35 changes: 17 additions & 18 deletions src/subParsers/autoLinks.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
showdown.subParser('autoLinks', function (text) {
showdown.subParser('autoLinks', function (text, options) {
'use strict';

text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi, '<a href=\"$1\">$1</a>');
//simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,

var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi,
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
simpleMailRegex = /\b(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)\b/gi,
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;

text = text.replace(delimUrlRegex, '<a href=\"$1\">$1</a>');
text = text.replace(delimMailRegex, replaceMail);
//simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
// Email addresses: <[email protected]>

/*
text = text.replace(/
<
(?:mailto:)?
(
[-.\w]+
\@
[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
)
>
/gi);
*/
var pattern = /<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
text = text.replace(pattern, function (wholeMatch, m1) {
if (options.simplifiedAutoLink) {
text = text.replace(simpleURLRegex, '<a href=\"$1\">$1</a>');
text = text.replace(simpleMailRegex, '<a href=\"$1\">$1</a>');
}

function replaceMail(wholeMatch, m1) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1);
return showdown.subParser('encodeEmailAddress')(unescapedStr);
});
}

return text;

});
11 changes: 11 additions & 0 deletions test/features/#164.1.simple_autolink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>foo.bar</p>

<p>www.foobar</p>

<p><a href="www.foobar.com">www.foobar.com</a></p>

<p><a href="http://foobar.com">http://foobar.com</a></p>

<p><a href="https://www.foobar.com/baz?bazinga=nhecos;">https://www.foobar.com/baz?bazinga=nhecos;</a></p>

<p><a href="http://www.google.com/">http://www.google.com</a></p>
11 changes: 11 additions & 0 deletions test/features/#164.1.simple_autolink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
foo.bar

www.foobar

www.foobar.com

http://foobar.com

https://www.foobar.com/baz?bazinga=nhecos;

<a href="http://www.google.com/">http://www.google.com</a>
3 changes: 2 additions & 1 deletion test/node/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('showdown.options', function () {
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false
parseImgDimensions: false,
simplifiedAutoLink: false
};
expect(showdown.getDefaultOptions()).to.be.eql(opts);
});
Expand Down
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({parseImgDimensions: true});
} else if (testsuite[i].name === '#69.header_level_start') {
converter = new showdown.Converter({headerLevelStart: 3});
} else if (testsuite[i].name === '#164.1.simple_autolink') {
converter = new showdown.Converter({simplifiedAutoLink: true});
} else {
converter = new showdown.Converter();
}
Expand Down
13 changes: 13 additions & 0 deletions test/single.test.wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Created by Estevao on 10-07-2015.
*/
/*
var showdown = require('../dist/showdown.js'),
bootstrap = require('./bootstrap.js'),
assertion = bootstrap.assertion,
testsuite = bootstrap.getTestSuite('test/features/');
describe('makeHtml() single test', function () {
'use strict';
});
*/

0 comments on commit cff0237

Please sign in to comment.