I’m using gatsby-plugin-mdx to create pages. But I want to create different kinds of pages based on the folder they are sourced from with gatsby-source-filesystem. I made the name different in the gatsby-source-filesystem config but I can’t seem to pass that along to the mdx node. Does anyone know of a way to do this?
2 Answers
Don’t query allMdx
directly. Instead do query for allFile
using a filter for sourceInstanceName
field.
{
allFile(filter: { sourceInstanceName: { eq: "products" } }) {
childMdx { ... }
}
}
3
-
1
I didn't know about the childMdx field. I assumed if I queried allFile I would lose the transformed mdx output.
– itwasmattgreggNov 19, 2019 at 16:02
-
1
All transformed content in Gatsby could be accessible through the original sourced file nodes. This is something common for all transformers – remark, yaml, sharp, mdx, etc.
– Z. ZlatevNov 19, 2019 at 16:10
-
in onCreateNode can I create a NodeField on the childMdx node when a file node is created?
– itwasmattgreggNov 19, 2019 at 18:19
I had a similar problem.
My solution was to use a query like this for the links to the MDX pages:
query {
allFile(
filter: {sourceInstanceName: {eq: "blog2"}, childrenMdx: {elemMatch: {id: {ne: "null"}}}}
) {
nodes {
childMdx {
frontmatter {
slug
title
}
id
}
}
}
}
And a query like this for the pages themselves:
query($id: String) {
mdx(id: {eq: $id}) {
frontmatter {
title
slug
date(formatString: "MMMM DD, YYYY")
}
}
}
With this solution MDX-pages from the wrong source can still be accessed, but I am sure there exist several workarounds, naming conventions for example, to solve this remaining problem.