Render html from rich text in Node.js

I’m looking for a way to render rich text into html using node. We currently use react and the @contentful/rich-text-react-renderer, but would like to convert to server-side rendering for articles for a number of reasons. However, we use ‘require’ server-side and there doesn’t appear to be a non-es6 library available. Short of writing a custom parser, any suggestions? That seems like such a basic thing, and supporting require and import would be really helpful.

After a bit of digging, I found the distribution comes with a transpiled es5 version that supports commonjs “require” syntax. There is presently no documentation that I can find though and it’s a bit tricky to require and call. There may be a simpler way, but here’s what worked for me based on the transpiled source code:

const es5Contentful = require( '../../node_modules/@contentful/rich-text-html-renderer/dist/rich-text-html-renderer.es5.js' );
const html = es5Contentful.documentToHtmlString( post.fields.body );

Hope this helps someone else.

1 Like