homepage/gatsby-node.js

106 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-12-12 21:27:56 +00:00
const path = require(`path`)
2020-03-04 00:56:39 +00:00
const { createFilePath } = require(`gatsby-source-filesystem`)
2019-12-12 21:27:56 +00:00
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
2019-12-13 14:51:59 +00:00
const siteTemplate = path.resolve(`src/templates/siteTemplate.js`)
2020-03-04 00:56:39 +00:00
const blogTemplate = path.resolve(`src/templates/blogTemplate.js`)
2019-12-12 21:27:56 +00:00
2020-03-04 00:56:39 +00:00
const pages = await graphql(`
2019-12-12 21:27:56 +00:00
{
2020-02-18 23:00:11 +00:00
allMdx(limit: 1000, filter: { fields: { source: { eq: "pages" } } }) {
2019-12-12 21:27:56 +00:00
edges {
node {
frontmatter {
path
title
2020-01-22 12:33:25 +00:00
edit
2019-12-12 21:27:56 +00:00
}
}
}
}
}
`)
2020-03-04 00:56:39 +00:00
// Handle errors
if (pages.errors) {
reporter.panicOnBuild(`Error while running pages GraphQL query.`)
return
}
2019-12-12 21:27:56 +00:00
2020-03-04 00:56:39 +00:00
const blogEntries = await graphql(`
{
allMdx(limit: 1000, filter: { fields: { source: { eq: "blog" } } }) {
edges {
node {
fields {
slug
}
frontmatter {
title
date
description
}
}
}
}
}
`)
2019-12-12 21:27:56 +00:00
// Handle errors
2020-03-04 00:56:39 +00:00
if (pages.errors) {
reporter.panicOnBuild(`Error while running blog-entry GraphQL query.`)
2019-12-12 21:27:56 +00:00
return
}
2020-03-04 00:56:39 +00:00
pages.data.allMdx.edges.forEach(({ node }) => {
2019-12-12 21:27:56 +00:00
createPage({
path: node.frontmatter.path,
2019-12-13 14:51:59 +00:00
component: siteTemplate,
2019-12-12 21:27:56 +00:00
context: {}, // additional data can be passed via context
})
})
2020-03-04 00:56:39 +00:00
blogEntries.data.allMdx.edges.forEach(({ node }) => {
console.log(node.fields.slug)
createPage({
path: node.fields.slug,
component: blogTemplate,
context: {
slug: node.fields.slug,
},
})
})
2019-12-12 21:27:56 +00:00
}
2020-01-22 12:33:25 +00:00
2020-02-18 23:00:11 +00:00
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
2020-03-04 00:56:39 +00:00
const source = getNode(node.parent).sourceInstanceName
2020-02-18 23:00:11 +00:00
createNodeField({
node,
name: `source`,
2020-03-04 00:56:39 +00:00
value: source,
})
const path = createFilePath({ node, getNode })
createNodeField({
node,
name: `slug`,
value: path,
2020-02-18 23:00:11 +00:00
})
}
}
2020-01-22 12:33:25 +00:00
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Mdx implements Node {
2020-02-18 23:00:11 +00:00
frontmatter: Frontmatter
2020-01-22 12:33:25 +00:00
}
type Frontmatter {
edit: String
}
`
createTypes(typeDefs)
}