RichText output in REST API responses #224
Replies: 2 comments 4 replies
-
Hey @sixers — great questions! In the long run, having content formatted like this is a huge win. In our own projects, this means we get to map different types of elements and "marks" to different React components, which would be harder to do if the string was HTML. Another reason why we store RichText data as JSON is that it makes your content useful in places outside of straight HTML—like native apps and other end use cases where HTML is irrelevant. If you wanted to store HTML in your database, but still have a rich text editor-style UX in the admin panel, you could just use a field with We have considered supporting an HTML editor in the future, but we really think that storing rich text as JSON is the better solution over the long run, even if it introduces a bit more complexity. We have not yet needed to take HTML and convert it into Slate-compatible JSON, although there are examples in Slate's documentation that could be helpful to you. Take a look at this: https://github.com/ianstormtaylor/slate/blob/main/site/examples/paste-html.tsx#L115 This example shows how to do it when a user "pastes" HTML into the RichText editor, although you could use the same principles. On another note, we will be improving how the rich text field handles improperly formatted data in the future. It would be ideal if only the field in question errored, while the rest of the admin panel still functioning properly. What do you think? |
Beta Was this translation helpful? Give feedback.
-
@jmikrut Thanks! That makes a lot of sense. Still, I think it would be nice to be able to support a "simple" use case, where you don't care about the internals of the rich text field, and just want to be able to display it easily (or import it from some other system). The only thing I care about is external representation, so Slate is perfectly fine as editor as long as the API can accept HTML string as input and produce HTML output. Perhaps RichText field could support const Products: CollectionConfig = {
slug: "products",
admin: {
useAsTitle: "name",
},
fields: [
{
name: "description",
label: "Description",
type: "richText",
externalRepresentation: {
serializer: (content) => convertSlateToHTML(content),
deserializer: (content) => convertHTMLToSlate(content),
}
required: true,
}
]
} So what's kept in Mongo is still a Slate JSON, Slate editor is still used in the admin panel, the only thing that changes is the public API representation of the field. WDYT? |
Beta Was this translation helpful? Give feedback.
-
REST API currently outputs a Slate-compatible representation for rich text fields.
Example output:
It also expects the input to be formatted as Slate-compatible JSON. It doesn't complain when it's not, but the Admin dashboard crashes if what's stored is not a correct slate representation. I haven't checked Local and GraphQL APIs, but I assume they work the same way.
Is there a specific reason to use this representation in API? I understand that Payload needs to keep this representation internally, but using it also for public APIs makes them much less convenient, and more difficult to integrate with other systems, which usually output HTML.
In my case specifically, I can't easily import data from WooCommerce.
It would be great if rich text fields could simply accept HTML and convert it to Slate representation internally. Same with output.
The documentation mentions how to generate HTML from Slate representation, could you also provide some guide for the reversed conversion?
Beta Was this translation helpful? Give feedback.
All reactions