homepage/gatsby-node.js

106 lines
2.3 KiB
JavaScript

const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const siteTemplate = path.resolve(`src/templates/siteTemplate.js`)
const blogTemplate = path.resolve(`src/templates/blogTemplate.js`)
const pages = await graphql(`
{
allMdx(limit: 1000, filter: { fields: { source: { eq: "pages" } } }) {
edges {
node {
frontmatter {
path
title
edit
}
}
}
}
}
`)
// Handle errors
if (pages.errors) {
reporter.panicOnBuild(`Error while running pages GraphQL query.`)
return
}
const blogEntries = await graphql(`
{
allMdx(limit: 1000, filter: { fields: { source: { eq: "blog" } } }) {
edges {
node {
fields {
slug
}
frontmatter {
title
date
description
}
}
}
}
}
`)
// Handle errors
if (pages.errors) {
reporter.panicOnBuild(`Error while running blog-entry GraphQL query.`)
return
}
pages.data.allMdx.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: siteTemplate,
context: {}, // additional data can be passed via context
})
})
blogEntries.data.allMdx.edges.forEach(({ node }) => {
console.log(node.fields.slug)
createPage({
path: node.fields.slug,
component: blogTemplate,
context: {
slug: node.fields.slug,
},
})
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const source = getNode(node.parent).sourceInstanceName
createNodeField({
node,
name: `source`,
value: source,
})
const path = createFilePath({ node, getNode })
createNodeField({
node,
name: `slug`,
value: path,
})
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Mdx implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
edit: String
}
`
createTypes(typeDefs)
}