Hi everyone.
I try to build a website by using Gatsby and Netlify CMS. In the default of config.yml (I’m using gatsby starter), there is collection with “blog” post files and I want to add “project” post as below.
collections:
- name: blog
identifier_field: blog
label: Blog
folder: content/blog
create: true
fields:
- { name: path, label: Path, required: true }
- { name: date, label: Date, widget: datetime, required: true }
- { name: title, label: Title, required: true }
- { name: category, label: Category, widget: list, required: true}
- { name: description, label: Description, widget: string, required: true }
- { name: body, label: Body, widget: markdown, required: true }
- name: project
identifier_field: project
label: Project
folder: content/project
create: true
fields:
- { name: path, label: Path, required: true }
- { name: title, label: Title, required: true }
- { name: featuredimage, label: "Featured Image", widget: image, required: true}
Now, I could get all posts both project and blog. I want to separate those. For example, in the blog page, I can retrieve all blog post onlyand in the project page, I JUST retrieve all project post.
Here my code for gatsby-node.js
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
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(
`
{
allMdx(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}
// Create blog posts pages.
const posts = result.data.allMdx.edges
posts.forEach((post, index) => {
console.log(post);
const previous = index === posts.length - 1 ? null : posts[index + 1].node;
const next = index === 0 ? null : posts[index - 1].node;
createPage({
path: `blog${post.node.fields.slug}`,
component: blogPost,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
createPage({
path: `project${post.node.fields.slug}`,
component: projectPost,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
return null
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
I already found and read the documentation but I still don’t get it. Thank you in advance!