如何从RIchTextJSON中获取文本?在满足



我使用的是contentful。作者正在为这部分使用富文本编辑器。出于我的目的,我只想能够从富文本编辑器中取出文本(但在其他事情上仍然使用相同的JSON,所以我也不想去掉它(,我该怎么做?

目前,JSON看起来是这样的。最有效的方法是什么;这篇文章很重要;。

我所得到的只是做一些类似于下面的事情,但这是低效和冗长的,尤其是当一个帖子变得非常大和复杂时。

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
]
}
]
}
]
}

最好的方法是使用Contentful提供的rich-text-plain-text-renderer包。

如果你想自己实现它,源代码已经有了很好的文档,基本上是一个递归函数,它会调用自己,直到到达所有的文档页。

最新更新