1

我正在尝试从博客文章的前端返回作者图片。前面的内容是这样构造的;

---
title: XXX
date: "XXX"
description: "XXX"
featuredImage: './featured-image.jpg'
avatar: './author.jpg'
author: 'XXX'
---

在 gatsby-config 我有这样的 gatsby-plugin-feed 设置 -

{
      resolve: `gatsby-plugin-feed`,
      options: {
        query: `
          {
            site {
              siteMetadata {
                title
                author
                siteUrl
                site_url: siteUrl
              }
            }
          }
        `,
        feeds: [
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => {
              return allMarkdownRemark.edges.map(edge => {
                return Object.assign({}, edge.node.frontmatter, {
                  description: edge.node.frontmatter.description,
                  date: edge.node.frontmatter.date,
                  url: site.siteMetadata.siteUrl + edge.node.fields.slug,
                  avatar: edge.node.frontmatter.avatar,
                  custom_elements: [{ "content:encoded": edge.node.html }],
                })
              })
            },
            query: `
              {
                allMarkdownRemark(
                  sort: { order: DESC, fields: [frontmatter___date] },
                ) {
                  edges {
                    node {
                      excerpt
                      html
                      fields { slug }
                      frontmatter {
                        title
                        date
                        author
                        description
                        avatar
                      }
                    }
                  }
                }
              }
            `,
            output: "/rss.xml",
            title: "Your Site's RSS Feed",
          },
        ],
      },
    },

这会导致错误 Error: Field "avatar" of type "File" must have a selection of subfields. Did you mean "avatar { ... }"? 如何更改它以便在 RSS 提要中返回图像 URL?

谢谢

4

1 回答 1

0

初始查询需要调用image_urland rssMetadata,并且缺少作为 RSS 插件查询一部分的代码,这是正常工作所必需的

guid: rssMetadata.site_url + edge.node.fields.slug

Gatsby RSS Feed Docs也很有帮助

query: `
        {
          site {
            siteMetadata {
              rssMetadata {
                site_url
                title
                author
                image_url
              }
            }
          }
        }
      `,
        feeds: [
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => {
              return allMarkdownRemark.edges.map(edge => {
                return Object.assign({}, edge.node.frontmatter, {
                  description: edge.node.frontmatter.description,
                  date: edge.node.frontmatter.date,
                  url: rssMetadata.site_url + edge.node.fields.slug,
                  guid: rssMetadata.site_url + edge.node.fields.slug,
                  avatar: edge.node.frontmatter.avatar,
                  custom_elements: [{ "content:encoded": edge.node.html }],
                })
              })
            },

最后有可能avatar需要被称为custom_element

custom_elements: [
                    { "avatar": edge.node.frontmatter.avatar},
                    { "content:encoded": edge.node.html }

于 2019-12-05T20:46:31.370 回答