Browse Source

fix: refactor plugin to use source-filesystem and onCreateNode

main
Dustin Schau 6 years ago
parent
commit
9d6015a871
  1. 4
      gatsby-config.js
  2. 45
      plugins/gatsby-transformer-home-example-code/gatsby-node.js
  3. 21
      src/pages/index.js

4
gatsby-config.js

@ -33,14 +33,14 @@ module.exports = {
{ {
resolve: 'gatsby-source-filesystem', resolve: 'gatsby-source-filesystem',
options: { options: {
path: `${__dirname}/src/pages`,
name: 'pages', name: 'pages',
path: `${__dirname}/src/pages`
}, },
}, },
{ {
resolve: 'gatsby-source-filesystem', resolve: 'gatsby-source-filesystem',
options: { options: {
name: 'packages', name: 'content',
path: `${__dirname}/content/`, path: `${__dirname}/content/`,
}, },
}, },

45
plugins/gatsby-transformer-home-example-code/gatsby-node.js

@ -1,28 +1,29 @@
const {readdirSync, readFileSync} = require('fs'); const crypto = require(`crypto`)
const {join, resolve} = require('path');
// docblock goes here
const createContentDigest = obj => crypto
.createHash(`md5`)
.update(obj)
.digest(`hex`)
// Store code snippets in GraphQL for the home page examples. // Store code snippets in GraphQL for the home page examples.
// Snippets will be matched with markdown templates of the same name. // Snippets will be matched with markdown templates of the same name.
exports.sourceNodes = ({graphql, actions}) => { exports.onCreateNode = async ({node, loadNodeContent, actions}) => {
const {createNode} = actions; const {createNode} = actions;
const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node
const path = resolve(__dirname, '../../content/home/examples'); if (sourceInstanceName === 'content' && relativeDirectory === 'home/examples' && ext === '.js') {
const files = readdirSync(path); const code = await loadNodeContent(node);
createNode({
files.forEach(file => { id: name,
if (file.match(/\.js$/)) { children: [],
const code = readFileSync(join(path, file), 'utf8'); parent: 'EXAMPLES',
const id = file.replace(/\.js$/, ''); code,
mdAbsolutePath: absolutePath.replace(/\.js$/, '.md'),
createNode({ internal: {
id, type: 'ExampleCode',
children: [], contentDigest: createContentDigest(JSON.stringify(code)),
parent: 'EXAMPLES', },
internal: { });
type: 'ExampleCode', }
contentDigest: JSON.stringify(code),
},
});
}
});
}; };

21
src/pages/index.js

@ -37,7 +37,12 @@ class Home extends Component {
render() { render() {
const {babelLoaded} = this.state; const {babelLoaded} = this.state;
const {data, location} = this.props; const {data, location} = this.props;
const {examples, marketing} = data; const {codeExamples, examples, marketing} = data;
const code = codeExamples.edges.reduce((lookup, { node }) => {
lookup[node.mdAbsolutePath] = node.code;
return lookup;
}, {});
return ( return (
<Layout location={location}> <Layout location={location}>
@ -262,7 +267,7 @@ class Home extends Component {
marginTop: 80, marginTop: 80,
}, },
}}> }}>
<CodeExample code={node.code} loaded={babelLoaded}> <CodeExample code={code[node.fileAbsolutePath]} loaded={babelLoaded}>
<h3 css={headingStyles}>{node.frontmatter.title}</h3> <h3 css={headingStyles}>{node.frontmatter.title}</h3>
<div <div
dangerouslySetInnerHTML={{__html: node.html}} dangerouslySetInnerHTML={{__html: node.html}}
@ -347,13 +352,23 @@ const CtaItem = ({children, primary = false}) => (
export const pageQuery = graphql` export const pageQuery = graphql`
query IndexMarkdown { query IndexMarkdown {
codeExamples: allExampleCode {
edges {
node {
id
code
mdAbsolutePath
}
}
}
examples: allMarkdownRemark( examples: allMarkdownRemark(
filter: {fileAbsolutePath: {regex: "//home/examples//"}} filter: {fileAbsolutePath: {regex: "//home/examples//"}}
sort: {fields: [frontmatter___order], order: ASC} sort: {fields: [frontmatter___order], order: ASC}
) { ) {
edges { edges {
node { node {
code fileAbsolutePath
fields { fields {
slug slug
} }

Loading…
Cancel
Save