var Parser = {
integrations: [{
type: 'image',
regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i,
async: false,
fetch: function(url, done) {
done("", "")
},
tag: function (media) {
return '
';
}
}, {
type: 'youtube',
regex: /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i,
async: false,
fetch: function(url, done) {
var id = (url.match(/v=([-_a-zA-Z0-9]{11})/i) || url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i) || url.match(/embed\/([-_a-zA-Z0-9]{11})/i))[1].split('&')[0];
var thumb = "http://i.ytimg.com/vi/" + id + "/hqdefault.jpg"
done(id, thumb);
},
tag: function (media) {
return '
▶';
}
}, {
type: 'vimeo',
regex: /vimeo.com\/\d+$/i,
async: true,
fetch: function(url, done) {
var id = url.match(/\d+$/i)[0];
$.ajax({
type: 'GET',
url: 'http://vimeo.com/api/v2/video/' + id + '.json',
success: function(result){
if (result.length == 0) { return done(id, "") }
done(id, result[0].thumbnail_large)
}
})
},
tag: function (media) {
return '
▶';
}
},
/*
{
type: 'soundcloud',
regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i,
async: true,
fetch: function (url, done) {
$.ajax({
type: 'GET',
url: 'http://api.soundcloud.com/resolve.json?url='
+ url
+ '&client_id='
+ '0673fbe6fc794a7750f680747e863b10',
success: function(result) {
done(result.id, "");
}
});
},
tag: function (media) {
return ''
}
}, {
type: 'link',
regex: /^http.+/i,
async: false,
fetch: function(url, done) {
done("", "")
},
tag: function (media) {
return '' + media.url + ''
}
}
*/
],
parse: function (url, cb) {
var matched = Parser.integrations.some(function(integration){
if (integration.regex.test(url)) {
integration.fetch(url, function(token, thumbnail){
cb({
token: token,
thumbnail: thumbnail,
type: integration.type,
url: url,
})
})
return true
}
return false
})
if (! matched) {
cb(null)
}
}
}