closedverse icon indicating copy to clipboard operation
closedverse copied to clipboard

Markdown library suggestion: paka.cmark

Open roryokane opened this issue 7 years ago • 1 comments

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.

roryokane avatar Oct 23 '17 02:10 roryokane

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

ariankordi avatar Oct 26 '17 02:10 ariankordi