Skip to content

Commit 9d6015a

Browse files
committed
fix: refactor plugin to use source-filesystem and onCreateNode
1 parent 3ead525 commit 9d6015a

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

gatsby-config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ module.exports = {
3333
{
3434
resolve: 'gatsby-source-filesystem',
3535
options: {
36-
path: `${__dirname}/src/pages`,
3736
name: 'pages',
37+
path: `${__dirname}/src/pages`
3838
},
3939
},
4040
{
4141
resolve: 'gatsby-source-filesystem',
4242
options: {
43-
name: 'packages',
43+
name: 'content',
4444
path: `${__dirname}/content/`,
4545
},
4646
},
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
const {readdirSync, readFileSync} = require('fs');
2-
const {join, resolve} = require('path');
1+
const crypto = require(`crypto`)
2+
3+
// docblock goes here
4+
const createContentDigest = obj => crypto
5+
.createHash(`md5`)
6+
.update(obj)
7+
.digest(`hex`)
38

49
// Store code snippets in GraphQL for the home page examples.
510
// Snippets will be matched with markdown templates of the same name.
6-
exports.sourceNodes = ({graphql, actions}) => {
11+
exports.onCreateNode = async ({node, loadNodeContent, actions}) => {
712
const {createNode} = actions;
13+
const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node
814

9-
const path = resolve(__dirname, '../../content/home/examples');
10-
const files = readdirSync(path);
11-
12-
files.forEach(file => {
13-
if (file.match(/\.js$/)) {
14-
const code = readFileSync(join(path, file), 'utf8');
15-
const id = file.replace(/\.js$/, '');
16-
17-
createNode({
18-
id,
19-
children: [],
20-
parent: 'EXAMPLES',
21-
internal: {
22-
type: 'ExampleCode',
23-
contentDigest: JSON.stringify(code),
24-
},
25-
});
26-
}
27-
});
15+
if (sourceInstanceName === 'content' && relativeDirectory === 'home/examples' && ext === '.js') {
16+
const code = await loadNodeContent(node);
17+
createNode({
18+
id: name,
19+
children: [],
20+
parent: 'EXAMPLES',
21+
code,
22+
mdAbsolutePath: absolutePath.replace(/\.js$/, '.md'),
23+
internal: {
24+
type: 'ExampleCode',
25+
contentDigest: createContentDigest(JSON.stringify(code)),
26+
},
27+
});
28+
}
2829
};

src/pages/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ class Home extends Component {
3737
render() {
3838
const {babelLoaded} = this.state;
3939
const {data, location} = this.props;
40-
const {examples, marketing} = data;
40+
const {codeExamples, examples, marketing} = data;
41+
42+
const code = codeExamples.edges.reduce((lookup, { node }) => {
43+
lookup[node.mdAbsolutePath] = node.code;
44+
return lookup;
45+
}, {});
4146

4247
return (
4348
<Layout location={location}>
@@ -262,7 +267,7 @@ class Home extends Component {
262267
marginTop: 80,
263268
},
264269
}}>
265-
<CodeExample code={node.code} loaded={babelLoaded}>
270+
<CodeExample code={code[node.fileAbsolutePath]} loaded={babelLoaded}>
266271
<h3 css={headingStyles}>{node.frontmatter.title}</h3>
267272
<div
268273
dangerouslySetInnerHTML={{__html: node.html}}
@@ -347,13 +352,23 @@ const CtaItem = ({children, primary = false}) => (
347352

348353
export const pageQuery = graphql`
349354
query IndexMarkdown {
355+
codeExamples: allExampleCode {
356+
edges {
357+
node {
358+
id
359+
code
360+
mdAbsolutePath
361+
}
362+
}
363+
}
364+
350365
examples: allMarkdownRemark(
351366
filter: {fileAbsolutePath: {regex: "//home/examples//"}}
352367
sort: {fields: [frontmatter___order], order: ASC}
353368
) {
354369
edges {
355370
node {
356-
code
371+
fileAbsolutePath
357372
fields {
358373
slug
359374
}

0 commit comments

Comments
 (0)