
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
@contentful/rich-text-html-renderer
Advanced tools
HTML renderer for the Contentful rich text field type.
HTML renderer for the Contentful rich text field type.
Using npm:
npm install @contentful/rich-text-html-renderer
Using yarn:
yarn add @contentful/rich-text-html-renderer
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello world!',
marks: [],
},
],
},
],
};
documentToHtmlString(document); // -> <p>Hello world!</p>
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ type: 'bold' }],
},
{
nodeType: 'text',
value: ' world!',
marks: [{ type: 'italic' }],
},
],
},
],
};
documentToHtmlString(document); // -> <p><b>Hello</b><u> world!</u></p>
You can also pass custom renderers for both marks and nodes as an optional parameter like so:
import { BLOCKS, MARKS } from '@contentful/rich-text-types';
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
data: {},
content: [
{
nodeType: 'paragraph',
data:{},
content: [
{
nodeType: 'text',
value: 'Hello',
marks: [{ type: 'bold' }],
data: {}
},
{
nodeType: 'text',
value: ' world!',
marks: [{ type: 'italic' }]
data: {}
},
],
},
]
};
const options = {
renderMark: {
[MARKS.BOLD]: text => `<custom-bold>${text}<custom-bold>`
},
renderNode: {
[BLOCKS.PARAGRAPH]: (node, next) => `<custom-paragraph>${next(node.content)}</custom-paragraph>`
}
}
documentToHtmlString(document, options);
// -> <custom-paragraph><custom-bold>Hello</custom-bold><u> world!</u></custom-paragraph>
Last, but not least, you can pass a custom rendering component for an embedded entry:
import { BLOCKS } from '@contentful/rich-text-types';
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
data: {},
content: [
{
nodeType: 'embedded-entry-block',
data: {
target: (...)Link<'Entry'>(...);
},
},
]
};
const options = {
renderNode: {
[BLOCKS.EMBEDDED_ENTRY]: (node) => `<custom-component>${customComponentRenderer(node)}</custom-component>`
}
}
documentToHtmlString(document, options);
// -> <custom-component>(...)Link<'Entry'>(...)</custom-component>
The renderNode keys should be one of the following BLOCKS and INLINES properties as defined in @contentful/rich-text-types:
BLOCKS
DOCUMENTPARAGRAPHHEADING_1HEADING_2HEADING_3HEADING_4HEADING_5HEADING_6UL_LISTOL_LISTLIST_ITEMQUOTEHREMBEDDED_ENTRYEMBEDDED_ASSETEMBEDDED_RESOURCEINLINES
EMBEDDED_ENTRY (this is different from the BLOCKS.EMBEDDED_ENTRY)EMBEDDED_RESOURCEHYPERLINKENTRY_HYPERLINKASSET_HYPERLINKRESOURCE_HYPERLINKThe renderMark keys should be one of the following MARKS properties as defined in @contentful/rich-text-types:
BOLDITALICUNDERLINECODESUPERSCRIPTSUBSCRIPTSTRIKETHROUGHIn your HTML rendering options, you can utilize the preserveWhitespace boolean flag. When set to true, this flag ensures that spaces and line breaks in the Contentful rich text content are preserved in the rendered HTML. Specifically, it replaces consecutive spaces with entities and retains line breaks using <br /> tags. This capability is particularly beneficial for content that has specific formatting requirements involving spaces and line breaks.
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
const document = {
nodeType: 'document',
content: [
{
nodeType: 'paragraph',
content: [
{
nodeType: 'text',
value: 'Hello world!',
marks: [],
},
],
},
],
};
const options = {
preserveWhitespace: true,
};
documentToHtmlString(document, options);
// -> <p>Hello world!</p>
With this configuration, the HTML output retains the spaces found between "Hello" and "world!".
The `rich-text-to-html` package provides similar functionality for converting rich text documents to HTML. It offers a straightforward API but may not be as tightly integrated with Contentful's specific rich text format as @contentful/rich-text-html-renderer.
The `draft-js-export-html` package is used to convert Draft.js editor content to HTML. While it is not specifically designed for Contentful's rich text format, it offers robust customization options and can be adapted for various rich text needs.
The `html-to-draftjs` package converts HTML to Draft.js content state. It serves the opposite purpose but can be used in conjunction with Draft.js to achieve similar results. It is not specifically tailored for Contentful's rich text format.
FAQs
HTML renderer for the Contentful rich text field type.
The npm package @contentful/rich-text-html-renderer receives a total of 199,677 weekly downloads. As such, @contentful/rich-text-html-renderer popularity was classified as popular.
We found that @contentful/rich-text-html-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

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.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.