diff --git a/README.md b/README.md index acee40e..3497eef 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,20 @@ React.render( ); ``` +Gist from URL: + +***NOTE: File cannot be parse from the URL. Supply as separate property.*** + +```js +var React = require('react'); +var Gist = require('react-gist'); + +React.render( + , + document.body +); +``` + ## Usage ### `` @@ -39,6 +53,11 @@ React.render( - `id` {string} Id of the gist - `file` {string} Name of a specific file in a multi-file gist +### `` + +- `url` {string} URL of the gist +- `file` {string} Name of a specific file in a multi-file gist + ## License MIT, see [LICENSE.md](http://github.com/tleunen/react-gist/blob/master/LICENSE.md) for details. diff --git a/demo/src/index.js b/demo/src/index.js index a6c0348..5b0a1f2 100644 --- a/demo/src/index.js +++ b/demo/src/index.js @@ -18,6 +18,10 @@ class Demo extends Component { setTimeout(() => { this.setState({id: '5995ea726914f280afb3', file: 'Chef-Dockerfile'}); }, 5000); + + setTimeout(() => { + this.setState({id: null, file: null, url: 'https://gist.github.com/ringods/5995ea726914f280afb3'}); + }, 10000); } render() { diff --git a/src/index.js b/src/index.js index 96180b0..c2d530a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import url from 'url'; import React from 'react'; import PropTypes from 'prop-types'; @@ -11,7 +12,12 @@ class Gist extends React.PureComponent { } _defineUrl() { - const { id, file } = this.props; + let { id, file, url } = this.props; + + if (url) { + const gistUrl = new URL(url); + id = gistUrl.pathname.split('/')[2]; + } const fileArg = file ? `?file=${file}` : ''; @@ -27,7 +33,7 @@ class Gist extends React.PureComponent { if (iframe.contentDocument) doc = iframe.contentDocument; else if (iframe.contentWindow) doc = iframe.contentWindow.document; - const gistLink = this._defineUrl() + const gistLink = this._defineUrl(); const gistScript = ``; const styles = ''; const elementId = file ? `gist-${id}-${file}` : `gist-${id}`;