Skip to content

Instantly share code, notes, and snippets.

@stevedya
Created February 22, 2019 21:32
Show Gist options
  • Save stevedya/0abdd0f7a589f79d127e6a3e8b0a07da to your computer and use it in GitHub Desktop.
Save stevedya/0abdd0f7a589f79d127e6a3e8b0a07da to your computer and use it in GitHub Desktop.
Vue filter for youtube and vimeo embeds
Vue.filter('videoUrl', function (url) {
//youtube
if (url.includes('youtube') || url.includes('youtu.be')) {
//Get id
url = url.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
let id = (url[2] !== undefined) ? url[2].split(/[^0-9a-z_\-]/i)[0] : url[0];
return 'https://www.youtube.com/embed/' + id
} else if (url.includes('vimeo')) {
var url = url.match(/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^\/]*)\/videos\/|album\/(?:\d+)\/video\/|video\/|)(\d+)(?:[a-zA-Z0-9_\-]+)?/i);
return 'https://player.vimeo.com/video/' + url[1]
}
});
@stevedya
Copy link
Author

This snippet will take a youtube or video link and return a embed string for that video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment