Rich Text

default discord avatar
.tg888last year
2

Hello! Could someone tell me how can I save the Rich Text content to DB as a text (example: content: "<p>Lorem </p>") instead of an array of objects? Thanks!

  • discord user avatar
    patrikkozak
    last year

    Hey @.tg888 - couple things to note here:



    Most of the time, this kind of approach is not a good idea. This should really be done on your frontend in most cases. Here is an example of how we handle rich text on our website:



    https://github.com/payloadcms/website/blob/main/src/components/RichText/Serialize/index.tsx

    The benefits of the above are that you can map React components to different elements / leaves and maintain a high degree of control.



    And have dynamic relations e.g. links to pages, upload references, etc.



    Here is an example of how we handle rich text on the Payload side:



    https://github.com/payloadcms/website-cms/tree/main/src/fields/richText

    Also, it's worth noting that HTML is not as portable, in other words, it doesn't work in native apps (mobile, non-web-based platforms).



    But if you really wanted to store the HTML, then you could do it with a virtual field where you have an afterRead hook that automatically serializes and stores HTML from rich text JSON. You should not, however, change the way the rich text is stored itself.



    We have a virtual field example in our examples folder as well if you want to check it out:



    https://github.com/payloadcms/payload/tree/master/examples/virtual-fields

    If you have any other questions, let me know!

  • default discord avatar
    .tg888last year

    Thank you very much!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.