Browse Source

Refactor Codepen payload a little

main
Brian Vaughn 7 years ago
parent
commit
64e0aade1e
  1. 9
      gatsby-node.js
  2. 23
      src/templates/codepen-example.js

9
gatsby-node.js

@ -167,20 +167,17 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
toPath: newestBlogNode.fields.slug, toPath: newestBlogNode.fields.slug,
}); });
// Create Codepen example pages // Create Codepen example pages.
fs.readdirSync('./examples').forEach(file => { fs.readdirSync('./examples').forEach(file => {
const slug = file.substring(0, file.length - 3); // Trim extension const slug = file.substring(0, file.length - 3); // Trim extension
const jsTemplate = fs.readFileSync(`./examples/${file}`, 'utf8'); const code = fs.readFileSync(`./examples/${file}`, 'utf8');
createPage({ createPage({
path: `/examples/${slug}`, path: `/examples/${slug}`,
component: resolve('./src/templates/codepen-example.js'), component: resolve('./src/templates/codepen-example.js'),
context: { context: {
code,
slug, slug,
payload: {
html: '<div id="root"></div>',
js: jsTemplate,
},
}, },
}); });
}); });

23
src/templates/codepen-example.js

@ -3,7 +3,11 @@
import React, {Component} from 'react'; import React, {Component} from 'react';
import Container from 'components/Container'; import Container from 'components/Container';
import {colors} from 'theme'; import {colors} from 'theme';
// import {version} from '../site-constants';
const EXTERNALS = [
'https://unpkg.com/react/umd/react.development.js',
'https://unpkg.com/react-dom/umd/react-dom.development.js',
];
// Copied over styles from ButtonLink for the submit btn // Copied over styles from ButtonLink for the submit btn
const primaryStyle = { const primaryStyle = {
@ -30,13 +34,14 @@ class CodepenExample extends Component {
} }
render() { render() {
const {payload} = this.props.pathContext; // Codepen configuration
// Set codepen options const payload = JSON.stringify({
payload.js_pre_processor = 'babel'; editors: '0010', // Open JS editor by default
// Only have the JS editor open (default for all examples) html: '<div id="root"></div>',
payload.editors = '0010'; js: this.props.pathContext.code,
// We can pass @version in the URL for version locking, if desired. js_external: EXTERNALS.join(';'),
payload.js_external = `https://unpkg.com/react/umd/react.development.js;https://unpkg.com/react-dom/umd/react-dom.development.js`; js_pre_processor: 'babel',
});
return ( return (
<Container> <Container>
@ -48,7 +53,7 @@ class CodepenExample extends Component {
}} }}
action="https://codepen.io/pen/define" action="https://codepen.io/pen/define"
method="POST"> method="POST">
<input type="hidden" name="data" value={JSON.stringify(payload)} /> <input type="hidden" name="data" value={payload} />
<input <input
style={primaryStyle} style={primaryStyle}

Loading…
Cancel
Save