Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created October 22, 2022 14:31
Show Gist options
  • Select an option

  • Save devmnj/f1d04cefc404cf0d7dd677d4213f5f11 to your computer and use it in GitHub Desktop.

Select an option

Save devmnj/f1d04cefc404cf0d7dd677d4213f5f11 to your computer and use it in GitHub Desktop.
React contentful RichText rendering component
import { documentToReactComponents } from "@contentful/rich-text-react-renderer";
import { BLOCKS } from "@contentful/rich-text-types";
const renderDocument = (document) => {
const Text = ({ children }) => <p>{children}</p>;
const options = {
renderNode: {
[BLOCKS.PARAGRAPH]: (node, children) => <Text>{children}</Text>
},
renderText: (text) =>
text.split("\n").flatMap((text, i) => [i > 0 && <br />, text])
};
return documentToReactComponents(document, options);
};
const RichText = ({ document }) => (
<div>
{renderDocument(document)}
</div>
);
export default RichText;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment