Best practices for rich text. Rest. Backend

Viewed 1180

the community I'm looking for some advice about rich texts. Currently the problem: what is the best way to store and manage rich text content on the backend (in db). Why it seems like a problem because we could have multiple platforms: desktop, mobile, web and this bring problems.

As I know we have 3 options, I would be appreciated if you provide me something else:

1) store it like HTML, which is a bad solution for me, because what about editing it on mobile, on the desktop then? For sure all will be okay on the web because of plants of libraries.

2)store it in markdown manner, I think something like that currently used by StackOverflow, not some real problems, but then each client needs to parse markdown text, and for me, it is not so convenient to manage photos along with the text.

3) store it like full json like that:

{
"type": "Paragraph"
"text": "some text"
}

which is also not so convenient and takes a long time to develop I suppose, it is the trickiest way. I'm not sure, but maybe there are devs or ex-devs from Jira, StackOverflow, Google Docs or other systems which use rich text, how did you figure out this problem, and maybe you have some advice and practices. I would be appreciated for all your replies.

1 Answers

While I was not doing editor, I did work with a team that wrote their own and had to process their output, so here's my take on that topic:

Markdown, while kinda lightweight, easy to read and write by human, has limited usage, I found nice summary about that topic recently https://buttondown.email/hillelwayne/archive/please-dont-write-your-documentation-in-markdown/ It also points to some alternatives, thought I am not familiar with them.

Your own JSON format, might not sound bad, but edge cases tend to multiply, Consider for example overlapping styles - paragraph that is bold then bold+talic then italic... and being partially a link.

Html is actually pretty decent choice, if you want to have something out, quick. I belive there are tons of libraries supporting it on every environment.


What other alternatives I cant think of?

Maybe xml + (optionally) xslt. Not the trendiest choice, but why not...

What I do NOT recommend :

Decouple text from styles and have styles defined in an array like "bold":"10,20" (those were character ranges). Nightmare to convert in any nontrivial scenario, because the content you are producing keeps getting more and more offsets for every style you apply.

What I had to deal with?

The "I do not recommend" that was rewritten to your 3. Which I had to process to markdown, which was in turn processed into html in the next layer. We should have started with html, I guess...

Related