Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Post Type with MarkdownRemark

Tags:

gatsby

I was wondering if I could have multiple post types in a gatsby static site.

On gatsby-config.js I have the following code.

{
resolve: `gatsby-source-filesystem`,
options: {
  path: `${__dirname}/content/books`,
  name: `books`,
}

{
resolve: `gatsby-source-filesystem`,
options: {
  path: `${__dirname}/content/posts`,
  name: `posts`,
}

Both the folders have relevant markdown files and I could not find any examples of graphql to retrieve both.

Thanks in advance.

like image 965
Ali Avatar asked Oct 07 '17 07:10

Ali


2 Answers

Also see this (1) and this (2) issue/comment which clarifies things:

1.

In retrospect it was a mistake adding that field to gatsby-transformer-remark and will be removed in the future

2.

Using regex is a great solution as well which we use a lot on gatsbyjs.org for example.

Example: So this is the way to go in order to get only posts:

{
   allMarkdownRemark(
    sort: { order: DESC, fields: [frontmatter___date]},
    filter: {fileAbsolutePath: {regex: "/(\/content\/posts)/.*\\.md$/"}}
  ) {
      edges {
        node {
          excerpt(pruneLength: 250)
          id
          frontmatter {
            title
            date(formatString: "MMMM DD, YYYY")
            path
          }
        }
      }
    }
}
like image 103
3raindrops Avatar answered Nov 23 '22 01:11

3raindrops


Once they are in grapqhl (via gatsby-source-filesystem) like you have it set up now, gatsby-transformer-remark will pull them all into the AllMarkdownRemark query, regardless of where they come from. The gatsbyjs.org docs do the same thing too, check the source here.

Try creating your query for the markdown content, as in part 4 of the official tutorial, and you should have access to all the pages from both folders. Now if you want to split them in some way, you'll have to do a check. See the gatsby-node.js file in the link above for an example on how that might look.

like image 40
Mark Michon Avatar answered Nov 23 '22 00:11

Mark Michon