Last active
December 30, 2015 21:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.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