嗨,我正在使用Gatsby starter blog。
我有两个页面-博客和项目。现在,我有6个帖子,5个博客帖子和1个项目帖子。所有帖子(包括项目帖子)都在/ blog页面下创建。如何确保在/ projects页面上创建项目帖子?
我已经将gatsby-source-filesystem
和路径${__dirname}/content/projects
添加到了我的gatsby-config.js中。
Gatsby-node.js
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent);
const value = createFilePath({ node, getNode })
const parent = fileNode.sourceInstanceName;
let basePath = (parent === `blog`) ? `blog` : `projects`
createNodeField({
name: `slug`,
node,
value: `/${basePath}${value}`,
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const blogPost = path.resolve(`./src/templates/blog-post.js`)
const projectPost = path.resolve(`./src/templates/project-post.js`)
return graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
).then(result => {
console.log(JSON.stringify(result, null, 4));
if (result.errors) {
throw result.errors
}
// Create blog and projects posts.
const posts = result.data.allMarkdownRemark.edges
posts.forEach((post, index) => {
const slug = post.node.fields.slug;
createPage({
path: `/${slug}`,
component: blogPost,
context: {
slug,
},
})
})
return null
})
}