whatsthis
Version:
Discover embedable content from URL's
40 lines (33 loc) • 1.07 kB
JavaScript
;
var embedframe = require('../embedframe');
/**
* Detect if the given URL is an youtube URL.
*
* @param {Result} result Result of message parsing.
* @param {Function} next Continuation callback.
* @private
*/
module.exports = function youtube(result, next) {
if (!/youtube\.com|youtu\.be/i.test(result.url.hostname)) {
return next();
}
//
// Check if it's a page url with v=<ocde> so we can extract it for the rest of
// the logic.
//
if (result.url.query.v) {
result.url.set('pathname', result.url.query.v);
}
//
// In the case of a shortned url, we still want to produce a full length
// youtube URL that we can use embeding, so we are going to rewrite and format
// the URL so it because a full and valid URL
//
result.url.set('protocol', 'https:');
result.url.set('hostname', 'youtube.com');
result.url.set('pathname', '/embed/' + result.url.pathname.split('/').pop());
result.type = 'video';
result.service = 'youtube';
result.html = embedframe(result.url.href);
return next(undefined, true);
};