Question

The way we write questions in the SO form, with the help of MD editor. I wanted to know what and how the data goes to the server and in which way it is saved and then rendered. The lifecycle of the text that I am typing.

I type text here, in markdown. How is it submitted to the server, I mean how is the formatting being taken care of, and once they are saved, how are they rendered to the client. How do you go about doing this in python? What is the best practice.

Was it helpful?

Solution

What you usually do is that you store the Markdown input from the user as text, and render it to HTML when you have to display it. (If you're in dire need of performance, you can always cache that).
When you need to modify it, you modify the Markdown input again, not the HTML.

To do the Markdown -> HTML conversion in python, you can use the Markdown library.

You're not really supposed to be turning HTML back into Markdown (Markdown is not as rich formatting-wise as HTML is, so you might lose some formatting), but the python library html2text happens to output valid Markdown - you can't guarantee the Markdown itself will convert to your initial HTML though!


Nevertheless, please keep in mind that as always when outputting client-submitted data, you must take into consideration security risks such as cross-site scripting. You can see an example at this Django ticket.
You could go to this security.stackexchange.com question for more detail.

Actually, the lack of security in the Python Markdown library (among others) has played a role in django.utils.markup being deprecated in Django 1.5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top