Reputation: 21
I'm using contentful. Authors are using a rich text editor for this portion. For my purpose, i just want to be able to take out the text from the rich text editor (but still use the same JSON for other things, so i don't want to get rid of it either), how do i do so?
Currently, the JSON looks like this. What is the most effective way to just get "This text is important".
All i've gotten is to do something similar to the below, but it is inefficient and wordy especially when a post gets really big and complicated.
content[0].content[0].map((content) => content.value)
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph", // Can be paragraphs, images, lists, embedded entries
"data": {},
"content": [
{
"nodeType": "text",
"value": "This text is ",
"data": {},
"marks": []
},
{
"nodeType": "text",
"value": "important",
"data": {},
"marks": [
"type": "bold" // Can be bold, underline, italicss
]
}
]
}
]
}
Upvotes: 1
Views: 2503
Reputation: 3879
The best approach is to use Contentful's provided rich-text-plain-text-renderer
package.
If you want to implement it yourself, the source code is well documented and basically a recursive function calling itself until it reaches all the document leafs.
Upvotes: 3