Created
October 22, 2022 14:31
-
-
Save devmnj/f1d04cefc404cf0d7dd677d4213f5f11 to your computer and use it in GitHub Desktop.
React contentful RichText rendering component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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