Skip to content

Commit

Permalink
Merge pull request #191 from kan/embed_some_url
Browse files Browse the repository at this point in the history
特定のサービスのURLは展開して埋め込むようにした
  • Loading branch information
tdtds committed Jul 16, 2015
2 parents 0a59a01 + e0f5651 commit a5bdf18
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion assets/js/jquery.auto-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
*/

$(function(){
// load twitter widget
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);

t._e = [];
t.ready = function(f) {
t._e.push(f);
};

return t;
}(document, "script", "twitter-wjs"));

$.fn.autoLink = function(config){
this.each(function(){
var re = /((https?|ftp):\/\/[\(\)%#!\/0-9a-zA-Z_$@.&+-,'"*=;?:~-]+|^#[^#\s]+|\s#[^#\s]+)/g;
var embed = "";
$(this).html(
$(this).html().replace(re, function(u){
try {
Expand All @@ -20,12 +39,25 @@ $(function(){
return prefix + '<a href="/search?q='+encodeURIComponent(tag)+'">'+tag+'</a>';
} else {
var url = $.url(u);
if (u.match(/https?:\/\/twitter\.com\/.+\/status\/(\d+)$/)) {
// embed tweet
var tweet_id = RegExp.$1;
embed += '<div id="tw' + tweet_id + '"></div>';
twttr.ready(function() {
twttr.widgets.createTweet(tweet_id, $('#tw'+tweet_id).get(0),{cards:'hidden'});
});
}
else if (u.match(/https?:\/\/www\.youtube\.com\/watch\?v=([0-9a-zA-Z_]+)/)) {
embed += '<div><iframe id="ytplayer" type="text/html" src="//www.youtube.com/embed/' +
RegExp.$1 + '" frameborder="0" /></div>';
}

return '[<a href="'+u+'" target="_blank">'+url.attr('host')+'</a>]';
}
}catch(e){
return u;
}
})
}) + embed
);
});
return this;
Expand Down

0 comments on commit a5bdf18

Please sign in to comment.