views:

61

answers:

1

Hello, I am saving both the markdown text and html versions of text in my database. I have a question and answer site. When users browse the questions they are viewing snippits of markdown. They should only be viewing plain text just like when browsing questions on stackoverflow there are no markdown text or html text shown. Is there a way to convert either markdown or html text to plain text?

+1  A: 

Questions on StackOverflow are being viewed in HTML not plain text. They are sanitized using Jeff Atwood's HTML sanitizer and then converted to HTML using MarkDownSharp.

I asked this question a few weeks back and the solution I ended up with was to store the raw markdown in the database and then transform it when it's shown to the visitor.

Here's how I'm sanitizing my Markdown

        ''# Because some people can be real ass holes and try to submit bad data (scripts and crap)
        ''# we have to modify the "About" content in order to sanitize it.  At the same time, we
        ''# transform the Markdown into valid HTML
        user.About = Trim(Utilities.HtmlSanitizer.Sanitize(MarkDownSharp.Transform(user.About)))

Since MarkdownSharp is open source, I'm sure you could dig into the source code and remove the additional tags that you don't want to see in the preview.

EDIT:

Since in my example I'm sanitizing the HTML before converting the markdown, I think you would have to remove the <b> or <strong> tags in both the HtmlSanitizer and MarkdownSharp. The reason for this is that you'll need to sanitize raw html tags AND markdown tags.

rockinthesixstring
In this question I made the "Hello" word bold...if you can go back and browse for this question "Hello" will not be bold. This is what I need
Luke101
you would do this by "sanitizing" the `<b>` and the `</b>` tags out of the markdown.
rockinthesixstring
May I ask why you would want to use Markdown if you are only showing plain text? why not just use a TextArea?
rockinthesixstring
I think I'm understanding you now. You show the HTML in the full view, but not in the preview... is that correct? If so, again, you would use the "Html Sanitizer" link I posted above but have two methods. `SanitizeHtmlForDisplay` and `SanitizeHtmlForPreview` where the preview one would have LESS whitelist rules.
rockinthesixstring
Yes..rockint.. That is correct. I will try your suggestion
Luke101
see my edit....
rockinthesixstring
I have tried it and it works..Thanks
Luke101
great, glad to help.
rockinthesixstring