Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to approach markdown storage in the db for user content?

I was thinking about allowing users to edit site content with markdown, as it's simple and easy. The question now is how do I store that input - should I convert it to html on save and then store raw HTML in the database, or save the markdown text and re-parse it to HTML on every request? Should a different approach be taken here (write static files, etc)? I was just wondering how to approach this problem, and how do sites like stackoverflow do it. thanks.

like image 712
sa125 Avatar asked Jun 01 '11 08:06

sa125


1 Answers

I'd probably store both the original Markdown and the HTML versions of the content. In fact, I have done similar things (with stripped down minimal HTML instead of Markdown) where I stored both the raw and formatted versions.

If you want to edit the content after it is created, then you'll want the original Markdown as it will probably be easier to work with than some ugly Markdown that came out of a Markdown-to-HTML converter. Keeping the Markdown around will also make it easier to track your revision history or adjust your HTML format in the future.

Displaying the content will probably be more common than creating or editing it. So, you'll probably want to have the HTML handy to avoid doing the same Markdown to HTML conversion over and over and over again.

If you only have the Markdown, then you pay extra for every display. If you only have the HTML, then you'll end up with ugly and unreadable/uneditable Markdown. If you have both, you pay for a little bit of disk space but gain the ability to easily regenerate your HTML with a different internal structure, easy revision history tracking, easy editing, and you get cheap displays as a happy side effect. Furthermore, storing both makes it easy to store snippets for bulk listings (such as the /questions listings on SO) and those listings will be cheap because you're dumping data straight from storage to the page with minimal processing.

like image 85
mu is too short Avatar answered Oct 20 '22 23:10

mu is too short