https://github.com/sodoteo/contentful-html-to-richtext
Tool that converts HTML to the rich-text format used by Contentful. Useful for migrating content or integrating with the Contentful platform.
https://github.com/sodoteo/contentful-html-to-richtext
contentful html html5 migration-tool module parser rich-text richtext
Last synced: about 1 month ago
JSON representation
Tool that converts HTML to the rich-text format used by Contentful. Useful for migrating content or integrating with the Contentful platform.
- Host: GitHub
- URL: https://github.com/sodoteo/contentful-html-to-richtext
- Owner: SodoTeo
- License: gpl-3.0
- Created: 2022-12-28T19:35:47.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-25T14:05:41.000Z (over 2 years ago)
- Last Synced: 2024-10-25T12:49:46.222Z (6 months ago)
- Topics: contentful, html, html5, migration-tool, module, parser, rich-text, richtext
- Language: TypeScript
- Homepage:
- Size: 155 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# contentful-html-to-richtext



[](https://www.codacy.com/gh/SodoTeo/contentful-html-to-richtext/dashboard?utm_source=github.com&utm_medium=referral&utm_content=SodoTeo/contentful-html-to-richtext&utm_campaign=Badge_Grade)Module that converts HTML to the [Contentful](https://www.contentful.com/) rich-text model.
It was developed to assist in the process of migrating rich text content into contentful, a popular content management system.
The functionality has been thoroughly tested to ensure that it performs reliably. It is a useful tool for anyone who
needs to convert HTML to contentful's rich-text format, whether for the purpose of migrating existing content or integrating with contentful's platform.**This module is under development**
## Current Status
| Verified | Verified | Beta |
| ------------- |:------------------:|:--------------:|
| `
- ` | `` | `
- ` | `` | |
| `- ` | `` | |
| `` | ``| |
| `` | `` | |
| `` | `` | |
| `` | `
` | |
| `
` | `` | |
| `` | `
` | |
| `` | `
` | |
| ``| `` | |
## Installation
Using [npm](https://www.npmjs.com/package/html-to-richtext-contentful):
`npm i html-to-richtext-contentful`
## Usage
```
import { htmlToRichText } from 'html-to-richtext-contentful';const html = '
Name
SodoTeo
';
const result = htmlToRichText(html);
```## Output:
```
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "table",
"data": {},
"content": [
{
"nodeType": "table-row",
"data": {},
"content": [
{
"nodeType": "table-header-cell",
"data": {},
"content": [
{
"nodeType": "paragraph",
"data": {},
"content": [
{
"nodeType": "text",
"value": "Name",
"marks": [],
"data": {}
}
]
}
]
}
]
},
{
"nodeType": "table-row",
"data": {},
"content": [
{
"nodeType": "table-cell",
"data": {},
"content": [
{
"nodeType": "paragraph",
"data": {},
"content": [
{
"nodeType": "text",
"value": "SodoTeo",
"marks": [],
"data": {}
}
]
}
]
}
]
}
]
}
]
}
``````
const { htmlToRichText } = require('html-to-richtext-contentfult');
// id=""
const html = 'title
';
const result = htmlToRichText(html);
``````
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph",
"data": {},
"content": [
{
"nodeType": "entry-hyperlink",
"data": {
"target": {
"sys": {
"id": "main-nav-title-industry",
"type": "Link",
"linkType": "Entry"
}
}
},
"content": [
{
"nodeType": "text",
"value": "title",
"marks": [],
"data": {}
}
]
}
]
}
]
}
```## Note
To use hyperlinks as entry blocks, as well as embedded and inline entries and assets, you will need to render your custom node constructor and (most likely) Contentful's rich-[text-html-renderer](https://www.npmjs.com/package/@contentful/rich-text-html-renderer), [rich-text-types](https://www.npmjs.com/package/@contentful/rich-text-types).
Below is a snippet on how to do this:```
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
import { BLOCKS, INLINES } from '@contentful/rich-text-types';if (field.type === 'RichText') {
const options = {
renderNode: {
[BLOCKS.EMBEDDED_ENTRY]: (node) =>
``,
[BLOCKS.EMBEDDED_ASSET]: (node) =>
``,
[INLINES.EMBEDDED_ENTRY]: (node) =>
``,
[INLINES.ENTRY_HYPERLINK]: (node) =>
`${node.content[0].value}`
}
};if (field.values['en-US']) {
field.values['en-US'] = formatHTML(
documentToHtmlString(field.values['en-US'], options)
);
}return field;
}
```## Git Repository
https://github.com/SodoTeo/contentful-html-to-richtext
| `