简体   繁体   English

如何从 Gatsby.js 中的 YAML 数组获取图像

[英]How to source images from a YAML array in Gatsby.js

I'm developing a blog with Gatsby.js我正在用 Gatsby.js 开发一个博客

Each post is a YAML file in which there is an array for the gallery like this:每个帖子都是一个 YAML 文件,其中有一个画廊数组,如下所示:

gallery:
-'/uploads/image1.jpg'
-'/uploads/image2.jpg'
-'/uploads/image3.jpg'
-'/uploads/image4.jpg'
-'/uploads/image5.jpg'

in the post i have something like this:在帖子中,我有这样的事情:


const images = data.postData.frontmatter.gallery;

return (

{images.map((image, index) => {
            return (
  <img src={image}/>
)
}
)};

export const query = graphql`
query PostData($slug: String!) {
  postData: markdownRemark(fields: {slug: {eq: $slug}}) {
        frontmatter {
          gallery

        }
    }
}
`;

But the images don't show up as they are not processed and put in the static folder at build time.但是图像没有显示,因为它们在构建时没有被处理并放在静态文件夹中。

As I understand it, the plugin 'gatsby-plugin-sharp' is not transforming the images that are found in the array in the YAML file, but it does when it's just one image...据我了解,插件 'gatsby-plugin-sharp' 不会转换在 YAML 文件中的数组中找到的图像,但是当它只是一张图像时会转换...

(in some of the post there is a field like this (在一些帖子中有这样一个字段

main-image: 'path/to/the/image'

which then I can source with graphql like this:然后我可以像这样使用graphql:

     main-image {
       fluid {
         src
       }
     }
   }

and instead for the 'gallery' array the 'fluid' node doesn't get created.)而对于 'gallery' 数组,不会创建 'fluid' 节点。)

I hope this makes some sense, I realise I'm a bit confused about how some things, I hope you can help me understand some stuff.我希望这是有道理的,我意识到我对某些事情有点困惑,我希望你能帮助我理解一些东西。

Thanks,谢谢,

M

EDIT编辑

I went a bit forwards thanks to @Z.感谢@Z,我向前走了一点。 Zlatev.兹拉特夫。

I insert this in gatsby-node.js:我在 gatsby-node.js 中插入这个:

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = `
    type MarkdownRemark
    implements Node {
      frontmatter: Frontmatter
    }
    type Frontmatter {
      gallery: [File]
    }
  `;
  createTypes(typeDefs);
};

and now nodes are created for each image in the gallery array.现在为画廊数组中的每个图像创建节点。

However, querying the images I get null...但是,查询图像我得到空...

Here some details:这里有一些细节:

The YAML File: YAML 文件:

---
date: 2019-11-06T13:47:07.000+00:00
title: Cool Project
main_picture: "/uploads/simon-matzinger-Gpck1WkgxIk-unsplash.jpg"
gallery:
 - "/uploads/PROEGELHOEF.jpg"
 - "/uploads/swapnil-dwivedi-N2IJ31xZ_ks-unsplash-1.jpg"
 - "/uploads/swapnil-dwivedi-N2IJ31xZ_ks-unsplash.jpg"
 - "/uploads/simon-matzinger-Gpck1WkgxIk-unsplash.jpg"
---

Here the GraphQl query:这里是 GraphQl 查询:

query MyQuery {
  allMarkdownRemark(filter: {id: {eq: "af697225-a842-545a-b5e1-4a4bcb0baf87"}}) {
    edges {
      node {
        frontmatter {
          title
          gallery {
            childImageSharp {
              fluid {
                src
              }
            }
          }
        }
      }
    }
  }
}

Here the data response:这里的数据响应:

{
  "data": {
    "allMarkdownRemark": {
      "edges": [
        {
          "node": {
            "frontmatter": {
              "title": "Cool Project",
              "gallery": [
                {
                  "childImageSharp": null
                },
                {
                  "childImageSharp": null
                },
                {
                  "childImageSharp": null
                },
                {
                  "childImageSharp": null
                }
              ]
            }
          }
        }
      ]
    }
  }
}

I guess I'm still missing something...我想我仍然缺少一些东西......

I will try to explain how this works in Gatsby.我将尝试解释这在 Gatsby 中是如何工作的。 First of all, it's the gatsby-transformer-sharp plugin that's transforming your File nodes to ImageSharp nodes.首先,是gatsby-transformer-sharp插件将您的File节点转换为ImageSharp节点。 gatsby-plugin-sharp is of course involved too as a low-level API. gatsby-plugin-sharp当然也作为低级 API 参与其中。

The main issue you have is that Gatsby can't recognize(infer) your data as reference to files.您遇到的主要问题是 Gatsby 无法识别(推断)您的数据作为对文件的引用。 Once it does a chain of transformation will automatically kick in. Gatsby actually tries to figure out if string is a file path but those paths must be relative to the file they are found in.一旦它进行了一系列转换,就会自动启动。 Gatsby 实际上试图确定 string 是否是文件路径,但这些路径必须与它们所在的文件相关。

Consider the following example:考虑以下示例:

gatsby-project
├── content
│   ├── images
│   │   └── home.png
│   └── pages
│       └── home.yml
└── src

content/pages/home.yml内容/页面/home.yml

title: Homepage
url: /
image: ../images/home.png

The easiest solution would be to provide a correct relative paths in your yaml files.最简单的解决方案是在 yaml 文件中提供正确的相对路径。 Sadly we know that's not always possible.遗憾的是,我们知道这并不总是可能的。 An example of that are files created by NetlifyCMS. NetlifyCMS 创建的文件就是一个例子。 If this is your case too try some of the existing solutions like: https://www.gatsbyjs.org/packages/gatsby-plugin-netlify-cms-paths/如果这也是您的情况,请尝试一些现有的解决方案,例如: https : //www.gatsbyjs.org/packages/gatsby-plugin-netlify-cms-paths/

Since Gatsby 2.2.0 the createSchemaCustomization API exists that allows us to handle such scenarios more gracefully by defining custom resolvers and field extensions but it may be daunting for people who are not familiar with GraphQL.自 Gatsby 2.2.0 以来, createSchemaCustomization API 的存在允许我们通过定义自定义解析器和字段扩展来更优雅地处理此类场景,但对于不熟悉 GraphQL 的人来说可能令人生畏。 Read more about it here .在此处阅读更多相关信息。

I solved by installing this: https://www.npmjs.com/package/@forestryio/gatsby-remark-normalize-paths我通过安装这个解决了: https : //www.npmjs.com/package/@forestryio/gatsby-remark-normalize-paths

Thanks for putting me in the right direction.谢谢你把我放在正确的方向。

M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM