Re-factored @roberocity 's twitter autolinks into extension format

This commit is contained in:
Titus 2012-06-20 17:03:23 -06:00
parent 4234de3a91
commit 7db254fcb2

38
src/extensions/twitter.js Normal file
View File

@ -0,0 +1,38 @@
(function(){
var twitter = function(converter) {
return [
// @username syntax
{ type: 'lang', regex: '\\B(\\\\)?@([\\S]+)\\b', replace: function(match, leadingSlash, username) {
// Check if we matched the leading \ and return nothing changed if so
if (leadingSlash === '\\') {
return match;
} else {
return '<a href="http://twitter.com/' + username + '">@' + username + '</a>';
}
}},
// #hashtag syntax
{ type: 'lang', regex: '\\B(\\\\)?#([\\S]+)\\b', replace: function(match, leadingSlash, tag) {
// Check if we matched the leading \ and return nothing changed if so
if (leadingSlash === '\\') {
return match;
} else {
return '<a href="http://twitter.com/search/%23' + tag + '">#' + tag + '</a>';
}
}},
// Escaped @'s
{ type: 'lang', regex: '\\\\@', replace: '@' }
];
};
// Client-side export
if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.twitter = twitter; }
// Server-side export
if (typeof module !== 'undefined') module.exports = twitter;
}());