Friday, May 4, 2012

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?


Source: Tips4all

3 comments:

  1. 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.

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


    If user wants to edit their comment, you have to backwards convert parsed into original markdown
    Space in database (always go by the rule that if you don't need to store it, don't)
    Changes made to markdown parser would have to be run on every comment in the database, instead of just showing up at runtime.

    ReplyDelete
  3. Just render the Markdown to HTML at runtime.

    If your site runs into performance issues, the Markdown will be one of the last things you'll look into tweaking. And even then, I doubt it'll make sense.

    Just take a look at the realtime JavaScript renderer that SO uses. It's fast.

    Edit:
    Sorry, I should've been more clear. I meant just render in PHP. You'll save yourself a lot of headache -- and you probably have more important things to worry about.

    ReplyDelete