Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Markdown comments

I want to use Markdown for my website's commenting system but I have stumbled upon the following problem: What should I store in the database - the original comment in Markdown, the parsed comment in HTML, or both?

I need the HTML version for viewing and the Markdown version if the user needs to edit his comment. If I store the Markdown version, I have to parse the comments at runtime. If I store the HTML version, I need to convert the comment back to Markdown when the user needs to edit it (I found Markdownify for this but it isn't flawless). If I store both versions, I'm doubling the used space.

What would be the best option? Also, how does Stack Overflow handle this?

like image 835
liviucmg Avatar asked Feb 05 '10 21:02

liviucmg


People also ask

Can you put comments in markdown?

Adding HTML Comments in Markdown Unlike a "normal" HTML comment which opens with <! -- (two dashes), an HTML comment in Markdown opens with <! --- (three dashes). Some Markdown parsers support two-dash HTML comments, but the three-dash version is more universally compatible.

Can you store markdown in a database?

Store content in MarkdownContent is typically either stored in files (eg. git) or a database.

How do you comment in a readme Md file?

[comment]: <> (This is a comment, it will not be included) [comment]: <> (in the output file unless you use it in) [comment]: <> (a reference style link.)


2 Answers

Store both. It goes against the rules for database normalization, but I think it's worth it for the speed optimisation in this case - parsing large amounts of text is a very slow operation.

You only need to store it twice, but you might need to serve it thousands of times, so it's a space-time trade-off.

like image 169
Mark Byers Avatar answered Sep 21 '22 18:09

Mark Byers


Store the original markdown and parse at runtime. There are a few problems with storing the converted version in the database.

  1. If user wants to edit their comment, you have to backwards convert parsed into original markdown
  2. Space in database (always go by the rule that if you don't need to store it, don't)
  3. Changes made to markdown parser would have to be run on every comment in the database, instead of just showing up at runtime.
like image 32
Corey Hart Avatar answered Sep 21 '22 18:09

Corey Hart