not-your-blog/gatsby-node.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-02-13 08:56:23 +07:00
const path = require("path")
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const postLayout = path.resolve('./src/pages/Layout/index.tsx');
const result = await graphql(`
query {
allMdx {
nodes {
id
frontmatter {
slug
}
internal {
contentFilePath
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('Error loading MDX result', result.errors)
}
// Create blog post pages.
const posts = result.data.allMdx.nodes
// you'll call `createPage` for each result
posts.forEach(node => {
createPage({
// As mentioned above you could also query something else like frontmatter.title above and use a helper function
// like slugify to create a slug
path: node.frontmatter.slug,
// Provide the path to the MDX content file so webpack can pick it up and transform it into JSX
component: `${postLayout}?__contentFilePath=${node.internal.contentFilePath}`,
// You can use the values in this context in
// our page layout component
context: { id: node.id },
})
})
}