question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot render the "Rich Text" field from Contentful

See original GitHub issue

Hello everyone,

Description

I have some issues rendering the rich text field from Contentful. I tried to use the gatsby plugin, but there are bugs for the rendering of assets and entries inside the rich text field (#9821 - #10592) So I installed the package from Contentful. But when I want to parse the JSON to HTML nothing renders.

Here is the code from my blog template

import React from "react"
import { Link, graphql } from "gatsby"
import Img from "gatsby-image"
import {DefaultLayout} from "../components/DefaultLayout"
import {
  BLOCKS,
  MARKS,
  INLINES
} from '@contentful/rich-text-types';
import { documentToHtmlString} from '@contentful/rich-text-html-renderer'
const document = {
  nodeType: 'document',
};

const options = {
  renderNode: {
      [INLINES.ASSET_HYPERLINK]: node => {
        return `<img class='custom-asset' src="${
                node.data.target.fields.file['fr-FR'].url
              }"/>`
      },
      [INLINES.EMBEDDED_ENTRY]: node => {
        return `<div class='custom-entry' />${
                node.data.target.fields.name['fr-FR']
              }</div>`
      },
    },
    renderMark: {
      [MARKS.BOLD]: text => `<custom-bold>${text}<custom-bold>`,
    },
  }


class BlogPostContentfulTemplate extends React.Component {
  render() {
    const post = this.props.data.contentfulPost
    const { previous, next } = this.props.pageContext
    return (
      <DefaultLayout location={this.props.location} siteDescription={post.metaDesc} siteTitle={post.metaTitle}>
        <h1>{post.postTitle}</h1>
        <ul>
          <li>Article par {post.author}</li>
        </ul>

        <Img fluid={post.postThumbnail.fluid} />
        <div className="article-content">{post.content.content}</div>
        {
          console.log("Result :" + documentToHtmlString(post.content.content, options))
          /// Console output
          // Result :
        }
        <ul>
          <li>
            {previous && (
              <Link to={`/blog/${previous.slug}`} rel="prev">← {previous.postTitle}</Link>
            )}
          </li>
          <li>
            {next && (
              <Link to={`/blog/${next.slug}/`} rel="next">{next.postTitle} →</Link>
            )}
          </li>
        </ul>
      </DefaultLayout>
    )
  }
}

export default BlogPostContentfulTemplate

export const pageQuery = graphql`
query ContentfulBlogPostBySlug($slug: String!) {
    contentfulPost( slug: { eq: $slug } ) {
        slug
        postTitle
        author
        metaTitle
        metaDesc
        content{
          nodeType
          content
        }
        postThumbnail {
            fluid{
              ...GatsbyContentfulFluid
            }
         }
      }
    }
`

Steps to reproduce

  • Clone the dev branch from my repository

  • Switch to the dev branch

  • Do yarn develop

  • Then go blog post used for the test and check the console. Nothing happens.

Expected result

The HTML rendered completely, with assets and entries.

Actual result

Nothing rendered

Environment

Run gatsby info --clipboard in your project directory and paste the output here.

 gatsby info --clipboard
module.js:549
    throw err;
    ^

Error: Cannot find module 'regenerator-runtime/runtime'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Maral Sabbagh\AppData\Roaming\npm\node_modules\gatsby-cli\lib\index.js:88:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Roshifcommented, Oct 23, 2019

you can try :

content { json }

0reactions
rmcsharrycommented, Oct 23, 2019

@Roshif Yes, using the json node works, thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rendering linked assets and entries in the Rich Text field
The power of the Contentful Rich Text field is that it offers a WYSIWYG interface while storing the data as pure JSON.
Read more >
contentful rich text not rendering - Stack Overflow
__typename is inside ContentfulAsset but you said you are getting trouble rendering rich text, which is a raw field. Can you clarify it?...
Read more >
Image embedded in the rich text field in contentful
Hi, I am beginner in contentful development. I am trying to render the image embedded in the rich text.
Read more >
contentful-rich-text-vue-renderer - npm
Render Contentful Rich Text field using Vue components. Latest version: 3.0.1, last published: 5 months ago.
Read more >
Gatsby Contentful Rich Text Changes | Medium - Ed Pike
The gatsby-source-contentful plugin Rich Text changed from having one “json” field to a “raw” and “references” array. Error: Fragment cannot ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found