How to get the html elements from Contentful Raw Data for RSS-feeds (gatsby-plugin-feed)

Viewed 333

I'm bustin' my head trying to solve the latest breaking changes of my RSSfeeds using gatsby-plugin-feed@3.4.0 & gatsby@3.4.0 (latest of the latest).

This is my config: (gatsby-config.js)

{
      resolve: 'gatsby-plugin-feed',
      options: {
        query: `
          {
            site {
              siteMetadata {
                title
                author
                description
                bizEmail
                authorEmail
                siteUrl
                site_url: siteUrl
              }
            }
          }
        `,

        setup: ({ query: { site } }, options) => ({
          ...options,
          title: 'Menefex WMB: RSS Feeds',
          description: site.siteMetadata.description,
          site_url: site.siteMetadata.siteUrl,
          feed_url: `${site.siteMetadata.siteUrl}/rss.xml`,
          image_url: 'https://i.postimg.cc/JnqZPb3f/Gx-FAVICON.png',
          webMaster: `${site.siteMetadata.bizEmail} (${site.siteMetadata.title})`,
          managingEditor: `${site.siteMetadata.authorEmail} (${site.siteMetadata.author})`,
          copyright: `© 2019 - ${new Date().getFullYear()} ${
            site.siteMetadata.title
          }, Alle rechten voorbehouden.`,
          language: 'nl',
          generator: 'GatsbyJS',
          ttl: '60',
          custom_namespaces: {
            webfeeds: 'http://webfeeds.org/rss/1.0',
          },
          custom_elements: [
            {
              'webfeeds:cover': {
                _attr: {
                  image: 'https://i.postimg.cc/WbsmfwKc/Gx-NEWLOGO.png',
                },
              },
            },
            { 'webfeeds:icon': 'https://i.postimg.cc/JnqZPb3f/Gx-FAVICON.png' },
            { 'webfeeds:logo': 'https://i.postimg.cc/JnqZPb3f/Gx-FAVICON.png' },
            { 'webfeeds:accentColor': 'FFCC00' },
            {
              'webfeeds:related': {
                _attr: {
                  layout: 'card',
                  target: 'browser',
                },
              },
            },
          ],
        }),

        feeds: [
          {
            serialize: ({ query: { site, allContentfulBlogPost } }) =>
              allContentfulBlogPost.edges.map((edge) => ({
                title: edge.node.title,
                author: site.siteMetadata.authorEmail,
                description: edge.node.subtitle,
                date: edge.node.updatedAt,
                url: `${site.siteMetadata.siteUrl}/blog/${edge.node.slug}`,
                guid: edge.node.updatedAt,
                enclosure: {
                  url: `https:${edge.node.image.file.url}`,
                },
                custom_elements: [
                  {
                    'webfeeds:featuredImage': `https:${edge.node.image.file.url}`,
                  },
                  {
                    'content:encoded': JSON.stringify(edge.node.body),
                  },
                ],
              })),

            query: `
              {
                allContentfulBlogPost(sort: { fields: publishedDate, order: DESC }) {
                  edges {
                    node {
                      title
                      subtitle
                      slug
                      updatedAt
                      body {
                        raw
                        references {
                          ... on ContentfulAsset {
                            contentful_id
                            __typename
                            fixed(width: 1600) {
                              width
                              height
                              src
                              srcSet
                            }
                          }
                        }
                      }
                      image {
                        file {
                          url
                        }
                      }
                    }
                  }
                }
              }
            `,

            output: '/rss.xml',
            title: 'Menefex WMB: RSS Feeds',
            // optional configuration to insert feed reference in pages:
            // if `string` is used, it will be used to create RegExp and then test if pathname of
            // current page satisfied this regular expression;
            // if not provided or `undefined`, all pages will have feed reference inserted
            match: '^/blog/',
            // optional configuration to specify external rss feed, such as feedburner
            link: 'https://feeds.feedburner.com/GimmixWMB',
          },
        ],
      },
    },

With the code above I get this code beneath; (focus: content:encoded). I think this is not good/enough to view my rssfeeds the right way. How do I parse the raw data to html elements?

enter image description here


I used to have this piece of code beneath working before i migrated my Gatsby project to v3: Its now complaining about using a CreateType/createSchemaCustomization function & my rssHtml is not reachable in the GrapQhl playground.

my gatsby-node.js


const { documentToHtmlString } = require('@contentful/rich-text-html-renderer');

exports.createResolvers = ({ createResolvers }) => {
  createResolvers({
    contentfulBlogPostBodyRichTextNode: {
      rssHtml: {
        type: 'String',
        resolve: (source) => documentToHtmlString(source),
      },
    },
  });
};

What am i missing here? Thanks in advance.

1 Answers

Looking at your screenshot, the edge.node.body has the raw field, which I think is what you want to parse. Try passing that value to the documentToHtmlString call

        resolve: (source) => documentToHtmlString(JSON.parse(source.raw)),

and in your RSS feed config pass the new resolved value

                      'content:encoded': edge.node.body.rssHtml,

You might as well try to use the @contentful/rich-text-html-renderer directly in your gatsby-config.js file and avoid adding a custom resolver

const { documentToHtmlString } = require('@contentful/rich-text-html-renderer');

...
                    'content:encoded': documentToHtmlString(edge.node.body.raw),
...

Related