如何在 Gatsby 中处理 Contentful 内容数据

How to handle Contentful content data in Gatsby

我有兴趣使用 Gatsby to build a Netlify static site using content from Contentful

Netlify 有这个不错的 Gatsby 入门指南: https://www.netlify.com/blog/2016/02/24/a-step-by-step-guide-gatsby-on-netlify

但我有点不确定如何将 Contentful 融入其中。我是否需要编写脚本将我的 Contentful 内容转换为 Gatsby 'markdown'?

感谢任何想法、想法和链接!

现在最好的选择是编写一个脚本,将内容从 Contentful 同步到您的 Gatsby 网站的页面目录。

不过,我们计划在 Gatsby 中添加支持以半自动地实现这一点。早期的日子还在这里!查看此问题了解更多 https://github.com/gatsbyjs/gatsby/issues/324

这是我用来从 Contentful 中提取数据的脚本: https://gist.github.com/ivanoats/e79ebbd711831be2536d1650890055c4

我 运行 在 gatsby 构建之前通过 npm 运行 脚本。

我很乐意为这个过程开发插件或获得关于更好架构的想法。

我在Aerobatic blog

上更详细地写了一篇关于这个架构的post

自发布此问题后,官方的 Contentful 插件已添加到 Gatsby 的 collection(由 Gatsby 团队创建的官方插件,而非 Contentful):https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-contentful

示例站点的 src 代码在这里:https://github.com/gatsbyjs/gatsby/tree/master/examples/using-contentful

该插件通过 gatsby-transformer-remark 处理 markdown 并生成结果 HTML,您可以通过 Gatsby 的 GraphQL 服务器访问它,并使用来自 the example proj 的查询:

 contentfulProduct(id: { eq: $id }) {
      productName {
        productName
      }
      productDescription {
        childMarkdownRemark {
          html
        }
      }
      price
    }

您可以使用插件将两者连接到内容 API(对于已发布的 assets/content)and/or 预览 API(对于已发布的 草稿 content/assets)。

我们使用 NODE_ENV 从开发中的预览 API 和生产中的内容 API 中提取。