From 69d12afc5240a0ba2cece8528b3bb23e8080df5f Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 18 Mar 2019 20:02:25 -0400 Subject: [PATCH] Add gist support to embed (#703) * support gists in embed * update GistContainer * make init function have more generic naming --- components/Editor.js | 29 ++++++--------------------- components/GistContainer.js | 40 +++++++++++++++++++++++++++++++++++++ pages/embed.js | 5 ++++- 3 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 components/GistContainer.js diff --git a/components/Editor.js b/components/Editor.js index 5b64c17..560e07b 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -15,6 +15,7 @@ import Carbon from './Carbon' import ExportMenu from './ExportMenu' import Themes from './Themes' import TweetButton from './TweetButton' +import GistContainer from './GistContainer' import { LANGUAGES, LANGUAGE_MIME_HASH, @@ -29,7 +30,7 @@ import { DEFAULT_PRESET_ID } from '../lib/constants' import { serializeState, getQueryStringState } from '../lib/routing' -import { getSettings, escapeHtml, unescapeHtml, formatCode, omit } from '../lib/util' +import { getSettings, unescapeHtml, formatCode, omit } from '../lib/util' import LanguageIcon from './svg/Language' const languageIcon = @@ -61,29 +62,9 @@ class Editor extends React.Component { async componentDidMount() { const { asPath = '' } = this.props.router - const { query, pathname } = url.parse(asPath, true) - const path = escapeHtml(pathname.split('/').pop()) + const { query } = url.parse(asPath, true) const queryParams = getQueryStringState(query) - let initialState = Object.keys(queryParams).length ? queryParams : {} - try { - // TODO fix this hack - if (path.length >= 19 && path.indexOf('.') === -1 && this.context.gist) { - const { code, language, config } = await this.context.gist.get(path) - - if (typeof config === 'object') { - initialState = config - } - - if (language) { - initialState.language = language.toLowerCase() - } - - initialState.code = code - } - } catch (e) { - // eslint-disable-next-line - console.log(e) - } + const initialState = Object.keys(queryParams).length ? queryParams : {} const newState = { // Load from localStorage @@ -355,6 +336,8 @@ class Editor extends React.Component { )} + + this.setState(stateFromGist)} />