8000 Get gist from url by hipstersmoothie · Pull Request #6 · tleunen/react-gist · GitHub
[go: up one dir, main page]

Skip to content

Get gist from url #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
get file from url
  • Loading branch information
hipstersmoothie committed Jun 11, 2018
commit e240d35a2aa38a6d23c3e1abe73b29e9ae31d849
4 changes: 4 additions & 0 deletions demo/src/index.js
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import url from 'url';
import React from 'react';
import PropTypes from 'prop-types';

Expand All @@ -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}` : '';

Expand All @@ -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 = `<script type="text/javascript" src="${gistLink}"></script>`;
const styles = '<style>*{font-size:12px;}</style>';
const elementId = file ? `gist-${id}-${file}` : `gist-${id}`;
Expand Down
0