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.
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.
精彩评论