views:

427

answers:

2

I'm trying to implement the WMD editor used on StackOverflow to create some basic Wiki-style functionality. I'm up to the point where it's time to save to the database, but I'm unsure as to what I should be saving.

If I save the produced HTML then I can retrieve and display it correctly, but on re-edit I'm faced with editing HTML, not Markdown. However, if I save the Markdown I can't see any way of converting this back to HTML to display, and looking at StackOverflow it sends the browser HTML.

I'm guessing that I don't need to save both, so what simple thing am I missing?

A: 

I would suggest saving the exact entered text to the database, so editing will work with the original markdown or HTML.

When you retrieve the text for display, you parse it on the server side and convert to HTML where necessary

[Edit] At comment: You seem to have a way of parsing and converting to HTML already, if I understand your question correctly. Here you talk about the produced HTML.

If I save the produced HTML then I can retrieve and display it correctly, but on re-edit I'm faced with editing HTML, not Markdown

Otherside
That's great but as I said "I can't see any way of converting this back to HTML to display", so a link to some kind of JavaScript utlity or .NET control wouldn't go amiss. I have even Googled to no avail!
tags2k
The WMD Editor provides a preview function, which is the only way I can see of getting at the HTML. Obviously I cannot instantiate a WMD control every time I'm displaying the content to the user.
tags2k
+4  A: 

Absolutely save the Markdown code as entered by the user.

Then you'll need a Markdown converter that will convert it to HTML for displaying it.

You'll find a bunch of these at
http://en.wikipedia.org/wiki/Markdown#Converters
and
http://markdown.infogami.com/

I'd vote against using JS in your case. There appears to be a .NET implementation but I can't tell you how good it is.

While rendering your page you'll have to pass the Markdown code to a converter like the above and then output the returned HTML.

If performance is an issue you might also consider saving both the Markdown code (for later editing) AND the HTML code (for displaying) inthe database. That way it will only be converted once.

_Lasar
Thanks Lasar, I had actually visited the aspnetresources site but totally missed the link to the source code!
tags2k
-1 wouldn't it be more efficient to convert and store once and to redisplay it without conversion to html? i mean content will be displayed more often than it will be edited.
Eimantas