Is there any way to get gatsby-source-filesystem name in gatsby-plugin-mdx query?

Is there any way to get gatsby-source-filesystem name in gatsby-plugin-mdx query?


0

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
2


4

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.

    – itwasmattgregg

    Nov 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. Zlatev

    Nov 19, 2019 at 16:10

  • in onCreateNode can I create a NodeField on the childMdx node when a file node is created?

    – itwasmattgregg

    Nov 19, 2019 at 18:19



0

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.

New contributor

user22965618 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *