Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created January 11, 2017 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbide-public/8a15e38da52180e2abac0b53a29a941f to your computer and use it in GitHub Desktop.
Save carbide-public/8a15e38da52180e2abac0b53a29a941f to your computer and use it in GitHub Desktop.
Creating a Youtube Widget
import React from 'react'
function ytId(url){
var matchYt = /^(http(s)?:\/\/)?((w){3}\.)?youtu(be\.com\/watch\?v=|\.be\/)(\w+)$/
var parts = matchYt.exec(url)
return parts && parts[parts.length-1]
}
class YoutubeWidget extends React.Component {
static title = "Youtube";
static match(value, ast){
return value
&& typeof value == 'string'
&& ytId(value)
}
render(){
return <iframe style={{border: 0}} src={"https://www.youtube.com/embed/"+ytId(this.props.value)} />
}
}
export function __unload(){ ///This cleans everything up so we can run this cell multiple times.
var index = doc.kernel.widgets.indexOf(YoutubeWidget)
doc.kernel.splice(index, 1)
}
doc.kernel.widgets.unshift(YoutubeWidget)
'https://www.youtube.com/watch?v=c1ZlqXyj_FI'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment