What is the best way to store WMD input/markdown in SQL server and display later?

烂漫一生 提交于 2019-11-29 22:40:30

I'm implementing Markdown in a Blog engine I'm writing (who doesn't write blog engines?), and I've also implemented Markdown in a number of customized CMSs I've written for clients.

I do it very similarly to how the Stack Overflow team does it:

  1. I use the wmd.js as the client side editor.
  2. I use the MarkdownSharp server side processing.
  3. I use Jeff Atwood's Sanitize HTML to cover processing HTML.

Here are some resources that talk about Markdown:

Bottom line:

  1. I store the post in the form it was submitted in; It's displayed using MarkdownSharp.
  2. I sanitize the HTML using Jeff Atwood's approach (On output, not on input).
  3. I utilize ASP.NET MVC 'best practices' (a highly subjective term) to deal with XSS and XSRF.

So one of the ides behind Markdown is that it will produce "safe" html - there should be no need for separate encoding.

More generally I would recommend storing "raw" data in the database, without transforming it or sanitising it. You should always sanitise or transform as close to the rendering point as possible - it gives greater flexibility (oh, suddenly I need to render as RSS. Or JSON. Damn, I can't because I pre-formatted for HTML) and, should the sanitiser or renderer be updated you see the effects of the update on every piece of data.

I would say store the markdown text in the database, and then convert it when you want it rendered, using the markdown library for this which, in theory, should all safe HTML built from its safe list of tags and attributes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!