Rendering Contentful Rich Text with Javascript
Contentful offers many tools to help you work with our Rich Text feature. This guide is meant to help put the pieces together and provide resources for learning more. It focuses majorly on Contentful’s client library, rich-text-html-renderer, and rich-text-react-renderer to render your Rich Text fields with HTML.
Prerequisites
You should already have:
- a space
- a CDA token
- at least one content type with a rich text field
- at least one entry of that content type
- Node (at least v8) and NPM installed
- knowledge of installing tools using the command line.
Getting Started
To create a space, check CMA reference. To learn about retrieving tokens, refer to Authentication.
For content using rich text, refer to this Getting Started with Rich Text guide.
What is rich text?
Rich text is a new JSON format for handling complex content structures in a strongly typed manner. It is represented by the rich text field in Contentful.
Working with a rich text property in Contentful JS client libraries
- Install the Contentful client library and rich text html renderer:
- Use the Contentful client library to retrieve an entry containing rich text
- Invoke the rich text html renderer when the entry resolves
Customized rendering
The rich text html renderer also accepts custom renderers as optional parameters.
You can find more examples and a list of accepted custom renderers in the documentation for the html renderer library.
Customized rendering in React
The React renderer as well as html-renderer accepts renderers as optional parameters.
You can find more examples in the documentation for the React renderer library.
Migrating from markdown to rich text
If you already have markdown fields, you can use the rich text from markdown package. It automatically converts many specific markdown nodes to rich text, with the option of adding a callback for unsupported nodes. See the documentation for the package for more examples and the list of supported markdown nodes.
Future integrations
You may have noticed all our rich text tools can be found in our github repository for rich text. We plan to add more rich text integrations in the future. They will all be made available in this same, central location.