I would like to know how to get all the pages to not have the same
result for allMarkdownRemark
, and would like the results in
allMarkdownRemark to be filtered
In these cases, what is commonly used is a key
field for all kind of markdown files that you want to group. As you said, allMarkdownRemark
is a schema inferred by Gatsby (and their transformers and sharps) at the time that you allow it to access to your filesystem so you can't distinguish directly the type of markdown. This is the simplest, cleanest, and less invasive option. You just need to:
---
title: "My title"
category: "My category"
key: "pageTypeOne"
---
Then, in your queries, you just need to filter for key
field when needed:
export const pageQuery = graphql`
query ArticleQuery($path: String, $category: String, $title: String) {
allMarkdownRemark(
filter: {
frontmatter: {
category: {eq: $category},
title: {ne: $title}
key: {eq: "pageTypeOne" }
}
},
sort: {
order: DESC, fields: [frontmatter___date]
}
) {
...
You can change the string-based approach to a context one if needed in your createPage
API in your gatsby-node.js
. Or, depending on your needs, create a filtered query in your gatsby-node.js
, creating different queries for each page, in that way your markdownRemark
will be filtered already.
Alternatively, you can add different filesystems (gatsby-source-filesystem
) and use the inferred sourceInstanceName
to get your data:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/pages/`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `posts`,
path: `${__dirname}/src/posts/`,
},
},
Then:
{
allFile(filter: { sourceInstanceName: { eq: "posts" } }) {
edges {
node {
extension
dir
modifiedTime
}
}
}
}