0

我正在使用我的第一个 Gatsby 模板(Panr 的 Hello Friend),我完全没有使用 React.js 的经验。

我试图了解模板设计的一些逻辑, gatsby-node.js特别gatsby-config.js是:

来自gatsby-config.js

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

gatsby-node.js

    const sortedPages = markdownPages.sort((pageA, pageB) => {
      const typeA = getType(pageA.node)
      const typeB = getType(pageB.node)

      return (typeA > typeB) - (typeA < typeB)
    })

    const posts = allNodes.filter(
      ({ internal, fileAbsolutePath }) =>
        internal.type === 'MarkdownRemark' &&
        fileAbsolutePath.indexOf('/posts/') !== -1,
    )

    // Create posts index with pagination
    paginate({
      createPage,
      items: posts,
      component: indexTemplate,
      itemsPerPage: siteMetadata.postsPerPage,
      pathPrefix: '/',
    })

我是否正确地认为有两个内容类别:

1.pages

2.posts

并且帖子被枚举(分页)但页面不是?

排序pageApageB实现什么?

另外,我将如何添加其他内容类别?

注意:我意识到这是一个不适合 Stack Overflow 的模糊问题。我会在 Gatsby 特定论坛上问这个问题,但我不相信存在这个问题,这是Gatsby 社区页面中推荐的论坛。

4

1 回答 1

2

是的,现在有两个内容类别,默认情况下只列出帖子。

排序可能很幼稚,因为在我编写启动器时,我不知道有什么好的插件可以为每种内容类型处理单独的分页(上一篇/下一篇文章或页面),所以我为此制定了自己的逻辑。sortedPages是一个数组,[...typeA, ...typeB]然后检查每个项目是否有兄弟姐妹来创建自己的导航(上一个/下一个)。

如果您想创建另一个内容类型,您可以按照在 starter 中定义的相同方式执行此操作。只需添加另一个gatsby-source-filesystem插件实例,例如:

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

typeC并使用降价文件在srcdir 中创建文件夹。就是这样

于 2020-01-17T23:30:59.420 回答