Last active
August 14, 2019 23:03
-
-
Save ryanscherler/3bd91875c646cf8aa42ab97157d50b48 to your computer and use it in GitHub Desktop.
Gatsby Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const { createFilePath } = require('gatsby-source-filesystem') | |
const { fmImagesToRelative } = require('gatsby-remark-relative-images') | |
const config = require('./site-config.json') | |
exports.onCreateNode = ({ node, actions, getNode }) => { | |
const { createNodeField } = actions | |
fmImagesToRelative(node) | |
if (node.internal.type === 'MarkdownRemark') { | |
const relativeFilePath = createFilePath({ node, getNode }) | |
const basename = path.basename(relativeFilePath, '.md') | |
createNodeField({ | |
name: 'name', | |
node, | |
value: basename, | |
}) | |
createNodeField({ | |
name: 'slug', | |
node, | |
value: relativeFilePath, | |
}) | |
} | |
} | |
exports.createPages = async ({ actions, graphql }) => { | |
const { createPage, createRedirect } = actions | |
if (config.redirects.length) { | |
config.redirects.forEach(({ from, to, redirectInBrowser }) => { | |
createRedirect({ | |
fromPath: from, | |
toPath: to, | |
redirectInBrowser: redirectInBrowser, | |
}) | |
}) | |
} | |
return await graphql(` | |
{ | |
allMarkdownRemark( | |
sort: { fields: [frontmatter___sort], order: ASC } | |
limit: 1000 | |
) { | |
edges { | |
node { | |
id | |
excerpt(format: PLAIN) | |
fileAbsolutePath | |
fields { | |
name | |
slug | |
} | |
frontmatter { | |
title | |
sort | |
date(formatString: "MMMM DD, YYYY") | |
template | |
} | |
} | |
} | |
} | |
} | |
`).then(result => { | |
if (result.errors) { | |
return Promise.reject(result.errors) | |
} | |
const pages = result.data.allMarkdownRemark.edges | |
return pages.forEach(page => { | |
const { node } = page | |
createPage({ | |
path: node.fields.slug, | |
component: path.resolve( | |
`./src/templates/${node.frontmatter.template}.js` | |
), | |
context: { | |
slug: node.fields.slug, | |
}, | |
}) | |
}) | |
}) | |
} | |
exports.sourceNodes = ({ actions }) => { | |
const { createTypes } = actions | |
createTypes(` | |
type MarkdownRemarkFrontmatter { | |
sort: Int | |
date: Date @dateformat | |
template: String | |
} | |
type MarkdownRemark implements Node { | |
frontmatter: MarkdownRemarkFrontmatter | |
} | |
`) | |
} | |
exports.onCreateWebpackConfig = ({ getConfig, stage }) => { | |
const config = getConfig() | |
if (stage.startsWith('develop') && config.resolve) { | |
config.resolve.alias = { | |
...config.resolve.alias, | |
'react-dom': '@hot-loader/react-dom', | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment