Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store a field that supports markdown in my database when I need to render both HTML and "simple text" views?

I have a database and I have a website front end. I have a field in my front end that is text now but I want it to support markdown. I am trying to figure out the right was to store in my database because I have various views that needs to be supported (PDF reports, web pages, excel files, etc)?

My concern is that since some of those views don't support HTML, I don't just want to have an HTML version of this field.

Should I store 2 copies (one text only and one HTML?), or should I store HTML and on the fly try to remove them HTML tags when I am rendering out to Excel for example?

I need to figure out correct format (or formats) to store in the database to be able to render both:

  • HTML, and
  • Regular text (with no markdown or HTML syntax)

Any suggestions would be appreciated as I don't want to go down the wrong path. My point is that I don't want to show any HTML tags or markdown syntax in my Excel output.

like image 282
leora Avatar asked Jun 22 '13 12:06

leora


2 Answers

Decide like this:

  1. Store the original data (text with markdown).
  2. Generate the derived data (HTML and plaintext) on the fly.
  3. Measure the performance:
    • If it's acceptable, you're done, woohoo!
    • If not, cache the derived data.

Caching can be done in many ways... you can generate the derived data immediately, and store it in the database, or you can initially store NULLs and do the generation lazily (when and if it's needed). You can even cache it outside the database.

But whatever you do, make sure the cache is never "stale" - i.e. when the original data changes, the derived data in the cache must be re-generated or at least marked as "dirty" somehow. One way to do that is via triggers.

like image 184
Branko Dimitrijevic Avatar answered Nov 06 '22 19:11

Branko Dimitrijevic


You need to store your data in a canonical format. That is, in one true format within your database. It sounds like this format should be a text column that contains markdown. That answers the database-design part of your question.

Then, depending on what format you need to export, you should take the canonical format and convert it to the required output format. This might be just outputting the markdown text, or running it through some sort of parser to remove the markdown or convert it to HTML.

like image 21
WW. Avatar answered Nov 06 '22 18:11

WW.