Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown library suggestion: paka.cmark #2

Open
roryokane opened this issue Oct 23, 2017 · 1 comment
Open

Markdown library suggestion: paka.cmark #2

roryokane opened this issue Oct 23, 2017 · 1 comment

Comments

@roryokane
Copy link

roryokane commented Oct 23, 2017

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants