Created
February 22, 2019 21:32
-
-
Save stevedya/0abdd0f7a589f79d127e6a3e8b0a07da to your computer and use it in GitHub Desktop.
Vue filter for youtube and vimeo embeds
This file contains 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
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] | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet will take a youtube or video link and return a embed string for that video