markdown-it-py icon indicating copy to clipboard operation
markdown-it-py copied to clipboard

Markdown does not render correctly when used inside a Django @stringfilter

Open marcin-wicha opened this issue 5 years ago • 3 comments

Describe the bug

Markdown is not correctly rendered when the package is used inside a string filter in Django. Lists error out with Int() being passed characters, when I disable the "lists" rule, markdown parses through but it's missing first letter at new lines.

To Reproduce

Steps to reproduce the behavior:

  1. Create a string filter in Django with markdown-it.
from django import template
from django.template.defaultfilters import stringfilter, linebreaks_filter
from django.utils.safestring import mark_safe
from django.conf import settings
from markdown_it import MarkdownIt

register = template.Library()

@register.filter()
@stringfilter
def format_text(text):
    if settings.RICH_TEXT_ENABLED:
        return parse_markdown(text)
    else:
        return linebreaks_filter(text)

def parse_markdown(text):
    md = MarkdownIt()
    html = md.render(text)
    return mark_safe(html)
  1. Use the string filter on your site.
  2. See error

Expected behavior

Source:

---
__Advertisement :)__

- __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
  resize in browser.
- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly
  i18n with plurals support and easy syntax.

You will like those projects!

---

# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading

Log:


Request Method: | GET
-- | --
http://localhost:8000/sprawy/sprawa-5/
2.2.16
ValueError
invalid literal for int() with base 10: 't'
/usr/local/lib/python3.7/site-packages/markdown_it/rules_block/list.py in list_block, line 139
/usr/local/bin/python
3.7.9
['/code',  '/usr/local/lib/python37.zip',  '/usr/local/lib/python3.7',  '/usr/local/lib/python3.7/lib-dynload',  '/usr/local/lib/python3.7/site-packages',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf',  '/usr/local/lib/python3.7/site-packages/odf']
Czw, 8 Paź 2020 21:03:30 +0200


Environment

  • Python Version [3.7.9]

marcin-wicha avatar Oct 08 '20 19:10 marcin-wicha

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

welcome[bot] avatar Oct 08 '20 19:10 welcome[bot]

Heya,

Is there a way to debug what text is actually being parsed to parse_markdown, i.e. is django altering the text in any way before it reaches parse_markdown?

Because, the text you supplied seems to run fine through markdown-it-py:

$ markdown-it 
markdown-it-py [version 0.5.5] (interactive)
Type Ctrl-D to complete input, or Ctrl-C to exit.
>>> ---
__Advertisement :)__

- __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
  resize in browser.
- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly
  i18n with plurals support and easy syntax.

You will like those projects!

---

# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 
... 
... ^D
<hr />
<p><strong>Advertisement :)</strong></p>
<ul>
<li>
<p><strong><a href="https://nodeca.github.io/pica/demo/">pica</a></strong> - high quality and fast image</p>
<p>resize in browser.</p>
</li>
<li>
<p><strong><a href="https://github.com/nodeca/babelfish/">babelfish</a></strong> - developer friendly</p>
<p>i18n with plurals support and easy syntax.</p>
</li>
</ul>
<p>You will like those projects!</p>
<hr />
<h1>h1 Heading 8-)</h1>
<h2>h2 Heading</h2>
<h3>h3 Heading</h3>
<h4>h4 Heading</h4>
<h5>h5 Heading</h5>
<h6>h6 Heading</h6>
>>> 

but it's missing first letter at new lines.

Perhaps this is some issue with newline characters. What operating system are you working on?

chrisjsewell avatar Oct 08 '20 19:10 chrisjsewell

OS: Windows 10 Let me try out OSX as well, to see if there's a difference.

I've tried different Markdown parsers before (Markdown, markdown2, Mistune) and they worked without a problem. I wanted to go for markdown-it for security stance and found this weird behaviour :)

That's the console output of text passed into parse_markdown.

web_1   | ---
web_1   | __Advertisement :)__
web_1   |
web_1   | - __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
web_1   |   resize in browser.
web_1   | - __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly   
web_1   |   i18n with plurals support and easy syntax.
web_1   |
web_1   | You will like those projects!
web_1   |    
web_1   | ---
web_1   |    
web_1   | # h1 Heading 8-)
web_1   | ## h2 Heading
web_1   | ### h3 Heading
web_1   | #### h4 Heading
web_1   | ##### h5 Heading
web_1   | ###### h6 Heading

edit: On OSX with disabled lists. image

marcin-wicha avatar Oct 08 '20 20:10 marcin-wicha