ariankordi / closedverse

Miiverse clone (offdevice) in Django released in late 2017. Gross and ancient please don't look at this
23 stars 17 forks source link

Markdown library suggestion: paka.cmark #2

Open roryokane opened 6 years ago

roryokane commented 6 years ago

You requested help finding a good Markdown library in the Closedverse FAQ:

Haha, now where's my Markdown?

[…]

There is really Markdown, but it's in Messages, which is why one line break now looks like a space in messages.

Again, sorry. If anyone could find a good enough text formatting implementation that works in Django, let me know.

I don’t know the Python/Django ecosystem very well, but after searching, it looks like the paka.cmark Python library does what you need. It implements the CommonMark standard, so it supports all the features of Markdown. And its README shows that it supports converting soft line breaks to <br>s with breaks="hard":

>>> print(cmark.to_html(u"Hello,\n*World*!", breaks="hard"))
<p>Hello,<br />
<em>World</em>!</p>

I didn’t look for a library that specifically integrates paka.cmark with Django, as your current library django-markdown-deux does, but I don’t think you need such a library to use paka.cmark in your app. When I wrote a Ruby on Rails app that rendered Markdown, it was easy enough to write my own function to render Markdown. It just did these two things:

  1. With a library like CommonMarker, convert the Markdown to HTML.
  2. With another library like Sanitize, sanitize the HTML, keeping only whitelisted HTML elements and attributes. So no <script> or <style> tags.

Then you include that raw HTML in your view, marking it as safe.

ariankordi commented 6 years ago

Hm. I'll look into it later, looks good but I'll have to see if it's right for me, thanks.