dhoulb / gatsby-plugin-templated-files

DEPRECATED: Plugin is no longer supported
BSD Zero Clause License
5 stars 1 forks source link

Pass frontmatter to filterTemplated #1

Closed ragunaru closed 5 years ago

ragunaru commented 5 years ago

Hi there Dave and firstly, thanks so much for creating this plugin – it's fantastic.

There is however a small feature request that I have. I would like to filter on the properties of frontmatter, which are available on allMarkdownRemark but not allTemplated. I could easily query allMarkdownRemark but then I lose the slug field which is on allTemplated. This is basically so that I could filter my content based on a specific property.

As an example: I have content in two categories and I want to query the different categories in different template files. I am very new to graphql and I don't really see a way of doing that with allTempalted right now. I have tried for 2 hours and haven't found a way yet.

image

Any help super appreciated!

Thanks, Ragnar

dhoulb commented 5 years ago

Hi Ragnar

With this plugin I decided it was neater to only deal with the files and paths themselves, not their contents (so I could avoid crossing streams with complex plugins like gatsby-transformer-remark).

If you want to do a query on the frontmatter it's best to query the MarkdownRemark nodes (using allMarkdownRemark)

Then if you want to use properties of the Templated node (e.g. slug) remember that because the Templated node is the parent of the MarkdownRemark node you can crawl up the nodes and get it that way instead (rather than querying the Templated and going down through childMarkdownRemark).

The following GraphQL query achieves that:

{
  allMarkdownRemark(filter: { frontmatter: { title: { eq: "My Page" } } }){
    edges {
      node {
        id
        frontmatter {
          title
        }
        parent {
          id
          ... on Templated {
            slug
            absolutePath
          }
        }
      }
    }
  }
}
ragunaru commented 5 years ago

That's fantastic, @dhoulb, thanks so much! 🙏