{"id":14991057,"url":"https://github.com/storybynumbers/rich-text-to-react","last_synced_at":"2025-04-12T03:25:15.996Z","repository":{"id":33778483,"uuid":"161771997","full_name":"storybynumbers/rich-text-to-react","owner":"storybynumbers","description":"Maps a Contentful rich text field to a React component tree.","archived":false,"fork":false,"pushed_at":"2022-12-10T20:36:36.000Z","size":1602,"stargazers_count":20,"open_issues_count":26,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T02:17:47.986Z","etag":null,"topics":["contentful","react"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/storybynumbers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-14T10:53:45.000Z","updated_at":"2022-07-20T12:21:11.000Z","dependencies_parsed_at":"2023-01-15T02:31:10.620Z","dependency_job_id":null,"html_url":"https://github.com/storybynumbers/rich-text-to-react","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storybynumbers%2Frich-text-to-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storybynumbers%2Frich-text-to-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storybynumbers%2Frich-text-to-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storybynumbers%2Frich-text-to-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/storybynumbers","download_url":"https://codeload.github.com/storybynumbers/rich-text-to-react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248511164,"owners_count":21116359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contentful","react"],"created_at":"2024-09-24T14:21:23.659Z","updated_at":"2025-04-12T03:25:15.978Z","avatar_url":"https://github.com/storybynumbers.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rich-text-to-react\n\n\u003e Render a Contentful rich text field with React components.\n\nWarning: this is in alpha state, and this is not an official Contentful module. It is largely based off their [rich-text-html-renderer](https://github.com/contentful/rich-text#readme) library. Its purpose is to map node of a [Rich Text field from Contentful](https://www.contentful.com/developers/docs/concepts/rich-text/) to customizable React components.\n\n[![NPM](https://img.shields.io/npm/v/rich-text-to-react.svg)](https://www.npmjs.com/package/rich-text-to-react) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Install\n\n```bash\nnpm install --save rich-text-to-react\n```\n\n## Usage\n\n```javascript\nimport RichTextToReact from 'rich-text-to-react';\nimport SampleRichFieldData from './data'\n\nexport default () =\u003e (\n  \u003cRichTextToReact document={SampleRichFieldData.json} /\u003e\n)\n```\n\n## Custom renderers\n\nYou can also pass custom renderers for both marks and nodes:\n\n```javascript\nimport RichTextToReact from 'rich-text-to-react';\nimport { INLINES, BLOCKS, MARKS } from '@contentful/rich-text-types';\nimport MyCustomComponent from '~/components/MyCustomComponent'\n\nconst renderingOptions = {\n  renderMark: {\n    // Render all bold text in red.\n    [MARKS.BOLD]: (text, key) =\u003e \u003cstrong key={key} style={{ color: 'red' }}\u003e{text}\u003c/strong\u003e\n  },\n  renderNode: {\n    // Pass the node data for the inline embed to MyCustomComponent.\n    [INLINES.EMBEDDED_ENTRY]: (node, key, next, options) =\u003e \u003cMyInlineComponent key={key} node={node} {...options} /\u003e,\n    [BLOCKS.EMBEDDED_ENTRY]: (node, key, next, options) =\u003e \u003cMyBlockComponent key={key} node={node} {...options} /\u003e,\n    [BLOCKS.HEADING_1]: (node, key, next) =\u003e \u003cHeading key={key} as=\"h1\"\u003e{next(node.content, key, next)}\u003c/Heading\u003e,\n  },\n  foo: 'bar',\n}\n\nexport default () =\u003e (\n  \u003cRichTextToReact document={myFieldValue.json} options={renderingOptions} /\u003e\n)\n```\n\nThe object passed to RichTextToReact's `options` prop should have two keys, `renderMark` and `renderNode`, each containing objects. Those objects\nshould be keyed with Rich Text types (see next section) to assign rendering functions with a type of a Rich Text node (heading, list item, link).\n\nRendering functions are passed the following arguments:\n\n- `node` - Section of the Rich Text document being processed\n- `key` - Unique key used internally by React\n- `next` - Utility function to iterate through the Rich Text document\n- `options` - Any additional properties (everything except renderMark and renderNode) will be passed here. In the example above, MyInlineComponent will receive the prop 'foo' with value 'bar'.\n\n\n## Rich Text types\n\nThe `renderNode` keys should be one of the following `BLOCKS` and `INLINES` properties as defined in [`@contentful/rich-text-types`](https://www.npmjs.com/package/@contentful/rich-text-types):\n\n- `BLOCKS`\n  - `DOCUMENT`\n  - `PARAGRAPH`\n  - `HEADING_1`\n  - `HEADING_2`\n  - `HEADING_3`\n  - `HEADING_4`\n  - `HEADING_5`\n  - `HEADING_6`\n  - `UL_LIST`\n  - `OL_LIST`\n  - `LIST_ITEM`\n  - `QUOTE`\n  - `HR`\n  - `EMBEDDED_ENTRY`\n  - `EMBEDDED_ASSET`\n\n- `INLINES`\n  - `EMBEDDED_ENTRY` (this is different from the `BLOCKS.EMBEDDED_ENTRY`)\n  - `HYPERLINK`\n  - `ENTRY_HYPERLINK`\n  - `ASSET_HYPERLINK`\n\nThe `renderMark` keys should be one of the following `MARKS` properties as defined in [`@contentful/rich-text-types`](https://www.npmjs.com/package/@contentful/rich-text-types):\n\n- `BOLD`\n- `ITALIC`\n- `UNDERLINE`\n- `CODE`\n\n\n## License\n\nMIT © [storybynumbers](https://github.com/storybynumbers)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstorybynumbers%2Frich-text-to-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstorybynumbers%2Frich-text-to-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstorybynumbers%2Frich-text-to-react/lists"}