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 does not render correctly when used inside a Django @stringfilter #56

Open
marcin-wicha opened this issue Oct 8, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@marcin-wicha
Copy link

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 marcin-wicha added the bug Something isn't working label Oct 8, 2020
@welcome
Copy link

welcome bot commented Oct 8, 2020

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

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! 🎉

@chrisjsewell
Copy link
Member

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?

@marcin-wicha
Copy link
Author

marcin-wicha commented Oct 8, 2020

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

@chrisjsewell chrisjsewell added bug Something isn't working and removed bug Something isn't working labels Mar 31, 2021
@chrisjsewell chrisjsewell changed the title Markdown does not render correctly when used inside a @stringfilter Markdown does not render correctly when used inside a Django @stringfilter May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants