具有MarkdownRemark的多个帖子类型

时间:2017-10-07 07:59:30

标签: gatsby

我想知道我是否可以在gatsby静态站点中拥有多个帖子类型。

在gatsby-config.js上我有以下代码。

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

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

两个文件夹都有相关的markdown文件,我找不到任何graphql示例来检索这两个文件。

提前致谢。

3 个答案:

答案 0 :(得分:3)

另见 this (1)this (2)发布/评论澄清事情:

1

  

回想起来,将这个字段添加到gatsby-transformer-remark是一个错误,将来会被删除

2

  

使用正则表达式也是一个很好的解决方案,例如我们在gatsbyjs.org上使用了很多。

示例:所以这是获取帖子的方法:

{
   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
          }
        }
      }
    }
}

答案 1 :(得分:1)

一旦他们在grapqhl(通过gatsby-source-filesystem)就像你现在设置的一样,gatsby-transformer-remark会将它们全部拉入AllMarkdownRemark查询,无论它们来自何处。 gatsbyjs.org文档也做同样的事情,check the source here

尝试为markdown内容创建查询,如官方教程的第4部分所示,您应该可以访问这两个文件夹中的所有页面。现在,如果你想以某种方式拆分它们,你将不得不进行检查。请参阅上面链接中的gatsby-node.js文件,以获取有关其外观的示例。

答案 2 :(得分:0)

以上解决方案将有效。然而,还有另一种方法:

{
  allFile(sort: {order: DESC, fields: [name]}, filter: {internal: {mediaType: {eq: "text/markdown"}}, sourceInstanceName: {eq: "articles"}}) {
    edges {
      node {
        sourceInstanceName
        childMarkdownRemark {
          excerpt(pruneLength: 250)
          frontmatter {
            title
            date(formatString: "MMMM DD, YYYY")
          }
        }
      }
    }
  }
}