Socket
Socket
Sign inDemoInstall

@contentful/rich-text-react-renderer

Package Overview
Dependencies
Maintainers
114
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contentful/rich-text-react-renderer

React renderer for the Contentful rich text field type.


Version published
Maintainers
114
Created

What is @contentful/rich-text-react-renderer?

@contentful/rich-text-react-renderer is an npm package that allows developers to render Contentful's rich text content in React applications. It provides a set of tools to convert Contentful's rich text JSON structure into React components, making it easier to display rich text content in a React-based frontend.

What are @contentful/rich-text-react-renderer's main functionalities?

Rendering Rich Text

This feature allows you to render Contentful's rich text JSON structure as React components. The code sample demonstrates how to convert a simple rich text document into React elements.

const { documentToReactComponents } = require('@contentful/rich-text-react-renderer');
const richTextDocument = {
  nodeType: 'document',
  content: [
    {
      nodeType: 'paragraph',
      content: [
        {
          nodeType: 'text',
          value: 'Hello, world!',
          marks: [],
          data: {}
        }
      ],
      data: {}
    }
  ],
  data: {}
};
const RichTextComponent = () => (
  <div>
    {documentToReactComponents(richTextDocument)}
  </div>
);

Customizing Renderers

This feature allows you to customize how different node types are rendered. The code sample shows how to apply a custom class to paragraph elements in the rich text content.

const { documentToReactComponents } = require('@contentful/rich-text-react-renderer');
const options = {
  renderNode: {
    'paragraph': (node, children) => <p className="custom-paragraph">{children}</p>
  }
};
const richTextDocument = {
  nodeType: 'document',
  content: [
    {
      nodeType: 'paragraph',
      content: [
        {
          nodeType: 'text',
          value: 'Hello, world!',
          marks: [],
          data: {}
        }
      ],
      data: {}
    }
  ],
  data: {}
};
const RichTextComponent = () => (
  <div>
    {documentToReactComponents(richTextDocument, options)}
  </div>
);

Rendering Embedded Assets

This feature allows you to render embedded assets such as images within the rich text content. The code sample demonstrates how to render an image from the rich text document.

const { documentToReactComponents } = require('@contentful/rich-text-react-renderer');
const options = {
  renderNode: {
    'embedded-asset-block': (node) => (
      <img src={node.data.target.fields.file.url} alt={node.data.target.fields.title} />
    )
  }
};
const richTextDocument = {
  nodeType: 'document',
  content: [
    {
      nodeType: 'embedded-asset-block',
      data: {
        target: {
          fields: {
            file: { url: 'https://example.com/image.jpg' },
            title: 'Example Image'
          }
        }
      }
    }
  ],
  data: {}
};
const RichTextComponent = () => (
  <div>
    {documentToReactComponents(richTextDocument, options)}
  </div>
);

Other packages similar to @contentful/rich-text-react-renderer

FAQs

Package last updated on 21 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc