views:

1335

answers:

3

I am using wmd markdown editor on a project and had a question:

When I post the form containing the markdown text area, it (as expected) posts html to the server. However, say upon server-side validation something fails and I need to send the user back to edit their entry, is there anyway to refill the textarea with just the markdown and not the html? Since as I have it set up, the server only has access to the post data (which is in the form of html) so I can't seem to think of a way to do this. Any ideas? Preferably a non-javascript based solution.

Update: I found an html to markdown converter called markdownify. I guess this might be the best solution for displaying the markdown back to the user...any better alternatives are welcome!

Update 2: I found this post on SO and I guess there is an option to send the data to the server as markdown instead of html. Are there any downsides to simply storing the data as markdown in the database? What about displaying it back to the user (outside of an editor)? Maybe it would be best to post both versions (html AND markdown) to the server...

SOLVED: I can simply use php markdown to convert the markdown to html serverside.

+2  A: 

I would suggest that you simply send and store the text as Markdown. This seems to be what you have settled on already. IMO, storing the text as Markdown will be better because you can safely strip all HTML tags out without worrying about loss of formatting - this makes your code safer, because it will be harder to use a XSS attack (although it may still be possible though - I am only saying that this part will be safer).

a_m0d
Wouldn't stripping all HTML tags cause a problem if the text contained an HTML example?
Max Schmeling
Perhaps, but then it's just a matter of making sure that you don't strip any tags from inside a code block.
a_m0d
Just HTML encode it. I don't think any of the markdown characters are reserved html.
Matt Dotson
+1  A: 

One thing to consider is that WMD appears to have certain different edge cases from certain server-side Markdown implementations. I've definitely seen some quirks in the previews here that have shown up differently after submission (I believe one such case was attempting to escape a backtick surrounded by backticks). By sending the converted preview over the wire, you can ensure that the preview is accurate.

I'm not saying that should make your decision, but it's something to consider.

eyelidlessness
A: 

Try out Pandoc. It's a little more comprehensive and reliable than Markdownify.

Hans