Created
August 11, 2019 10:34
-
-
Save thephucit/2e1a0e261d404b981edf73dcfcaa37e3 to your computer and use it in GitHub Desktop.
detect content including youtube, vimeo, dailymotion OR normal link
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
| /** | |
| * detect content including youtube, vimeo, dailymotion OR normal link | |
| * | |
| * @param {String} string | |
| * @param {Number} height | |
| * @return {String} | |
| */ | |
| link: (string, height = 345) => { | |
| let getInfo = (url) => { | |
| url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com)|dailymotion.com)\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(&\S+)?/) | |
| let type = '' | |
| if (RegExp.$3.indexOf('youtu') > -1) { | |
| type = 'youtube' | |
| } else if (RegExp.$3.indexOf('vimeo') > -1) { | |
| type = 'vimeo' | |
| } else if (RegExp.$3.indexOf('dailymotion') > -1) { | |
| type = 'dailymotion' | |
| } else { | |
| type = 'link' | |
| } | |
| return {'type': type, 'id': RegExp.$6} | |
| } | |
| let links = string.match(/\bhttps?:\/\/\S+/gi) | |
| links && links.forEach((url) => { | |
| let info = getInfo(url) | |
| if (info.type === 'youtube') { | |
| string = string.replace(url, `<iframe width="100%" height="${height}" src="https://www.youtube.com/embed/${info.id}" frameborder="0" allowfullscreen></iframe>`) | |
| } else if (info.type === 'vimeo') { | |
| string = string.replace(url, `<iframe width="100%" height="${height}" src="https://player.vimeo.com/video/${info.id}" frameborder="0" allowfullscreen></iframe>`) | |
| } else if (info.type === 'dailymotion') { | |
| string = string.replace(url, `<iframe width="100%" height="${height}" src="https://www.dailymotion.com/embed/video/${info.id}" frameborder="0" allowfullscreen></iframe>`) | |
| } else { | |
| string = string.replace(url, `<p><a href="${url}" target="_blank">${url}</a></p>`) | |
| } | |
| }) | |
| return string | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment