Skip to content

Instantly share code, notes, and snippets.

@markshust
Last active December 30, 2015 21:48
Show Gist options
  • Select an option

  • Save markshust/471e3efddedeb57066e5 to your computer and use it in GitHub Desktop.

Select an option

Save markshust/471e3efddedeb57066e5 to your computer and use it in GitHub Desktop.
underscore mixin to mutate html containing youtube or vimeo video url's to embed html
_.mixin({
mutateVideoUrlsToEmbeds: function(html) {
let embed = '';
const youtubeRegEx = /(?:http?s?:\/\/)?(?:www\.)?(?:youtu\.be|youtube\.com)\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=)?((\w|-){11})/g;
const vimeoRegEx = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/(.*)/g;
if (youtubeRegEx.test(html)) {
embed = '<iframe width="360" height="270" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>';
html = html.replace(youtubeRegEx, embed);
}
if (vimeoRegEx.test(html)) {
embed = '<iframe width="360" height="270" src="//player.vimeo.com/video/$1" frameborder="0" allowfullscreen></iframe>';
html = html.replace(vimeoRegEx, embed);
}
return html;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment