Homework tasks
I'm trying the homework tasks of making the content of a post be able to handle Markdown and HTML.
For handling HTML, I've just altered {{ post.content|linebreaks }} in post_detail.html to instead be {{ post.content|safe }}. I assume this is an okay way to do it?
For handling Markdown, I followed the django.contrib.markup instructions, and used {{ post.content|markdown:”safe” }}. But what if I wanted to present an HTML code sample in the post? Using entities doesn't seem to work -my brain collapses and I can't figure out how to do it.
Thanks
Answers
Well, it depends on your markdown library :) (don't you love answers with "depends" in them?)
I like misaka, which is like Github's markdown parser. I'd hold on to the parsed markdown in a different field, too, maybe content_html. Then, on save(), parse content with my markdown library and save the output into content_html and then output the content of that field into the template, filtered through |safe. Using a library like misaka lets you do code blocks which the parser should HTML encode beforehand.