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

1

8

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.

Grapheme answered 1/6, 2011 at 8:0 Comment(0)
I
14

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.

Impute answered 1/6, 2011 at 8:24 Comment(2)
What does "store both -- html & markdown" mean?. Please can you explain more detail? Several example as well,....Jevons
@Ntlzz93 Markdown text comes in, you can convert it to HTML every time you display it or you can convert it to HTML once (when the markdown source changes) and store the HTML and the markdown in your database.Impute

© 2022 - 2024 — McMap. All rights reserved.