Moving supported syntax list to gatsby-config

This commit is contained in:
Jeff Avallone 2019-01-13 11:33:33 -05:00
parent 152cf7f7b3
commit c3116bf5b6
4 changed files with 53 additions and 15 deletions

View File

@ -9,7 +9,12 @@ const banner = process.env.BANNER || (process.env.NODE_ENV === 'production'
module.exports = { module.exports = {
siteMetadata: { siteMetadata: {
buildId, buildId,
banner banner,
defaultSyntax: 'js',
syntaxList: [
{ id: 'js', label: 'JavaScript' },
{ id: 'pcre', label: 'PCRE' }
]
}, },
plugins: [ plugins: [
'gatsby-plugin-react-helmet', 'gatsby-plugin-react-helmet',

View File

@ -93,7 +93,8 @@ class App extends React.PureComponent {
const { const {
syntax, syntax,
expr, expr,
permalinkUrl permalinkUrl,
syntaxList
} = this.props; } = this.props;
const { const {
loading, loading,
@ -105,7 +106,8 @@ class App extends React.PureComponent {
const formProps = { const formProps = {
onSubmit: this.handleSubmit, onSubmit: this.handleSubmit,
syntax, syntax,
expr expr,
syntaxList
}; };
const actionProps = { const actionProps = {
imageDetails, imageDetails,
@ -137,7 +139,11 @@ class App extends React.PureComponent {
App.propTypes = { App.propTypes = {
syntax: PropTypes.string, syntax: PropTypes.string,
expr: PropTypes.string, expr: PropTypes.string,
permalinkUrl: PropTypes.string permalinkUrl: PropTypes.string,
syntaxList: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
label: PropTypes.string
}))
}; };
export default App; export default App;

View File

@ -5,11 +5,6 @@ import ExpandIcon from 'react-feather/dist/icons/chevrons-down';
import style from './style.module.css'; import style from './style.module.css';
const syntaxList = [
{ id: 'js', label: 'JavaScript' },
{ id: 'pcre', label: 'PCRE' }
];
class Form extends React.PureComponent { class Form extends React.PureComponent {
state = { state = {
expr: this.props.expr, expr: this.props.expr,
@ -36,6 +31,7 @@ class Form extends React.PureComponent {
render() { render() {
const { const {
syntaxList,
children children
} = this.props; } = this.props;
const { expr, syntax } = this.state; const { expr, syntax } = this.state;
@ -70,6 +66,10 @@ class Form extends React.PureComponent {
Form.propTypes = { Form.propTypes = {
expr: PropTypes.string, expr: PropTypes.string,
syntax: PropTypes.string, syntax: PropTypes.string,
syntaxList: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
label: PropTypes.string
})),
onSubmit: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired,
children: PropTypes.oneOfType([ children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node), PropTypes.arrayOf(PropTypes.node),

View File

@ -1,13 +1,24 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Link } from 'gatsby'; import { Link, graphql } from 'gatsby';
import URLSearchParams from '@ungap/url-search-params'; import URLSearchParams from '@ungap/url-search-params';
import Metadata from 'components/Metadata'; import Metadata from 'components/Metadata';
import Message from 'components/Message'; import Message from 'components/Message';
import App from 'components/App'; import App from 'components/App';
const readURLHash = location => { export const query = graphql`
query IndexPageQuery {
site {
siteMetadata {
defaultSyntax
syntaxList { id, label }
}
}
}
`;
const readURLHash = (location, defaultSyntax) => {
const query = location.hash.slice(1); const query = location.hash.slice(1);
const params = new URLSearchParams(query); const params = new URLSearchParams(query);
const permalinkUrl = query ? location.href : null; const permalinkUrl = query ? location.href : null;
@ -21,14 +32,17 @@ const readURLHash = location => {
} else { } else {
// Assuming old-style URL // Assuming old-style URL
return { return {
syntax: 'js', syntax: defaultSyntax,
expr: query, expr: query,
permalinkUrl permalinkUrl
}; };
} }
}; };
export const IndexPage = ({ location }) => <> export const IndexPage = ({
location,
data: { site: { siteMetadata } }
}) => <>
<Metadata/> <Metadata/>
<noscript> <noscript>
<Message type="error" heading="JavaScript Required"> <Message type="error" heading="JavaScript Required">
@ -37,11 +51,24 @@ export const IndexPage = ({ location }) => <>
please see the <Link to="/privacy">Privacy Policy</Link>.</p> please see the <Link to="/privacy">Privacy Policy</Link>.</p>
</Message> </Message>
</noscript> </noscript>
<App { ...readURLHash(location) } /> <App
syntaxList={ siteMetadata.syntaxList }
{ ...readURLHash(location, siteMetadata.defaultSyntax) } />
</>; </>;
IndexPage.propTypes = { IndexPage.propTypes = {
location: PropTypes.object location: PropTypes.object,
data: PropTypes.shape({
site: PropTypes.shape({
siteMetadata: PropTypes.shape({
defaultSyntax: PropTypes.string,
syntaxList: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
label: PropTypes.string
}))
})
})
})
}; };
export default IndexPage; export default IndexPage;