Skip to content

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

Open
@marcin-wicha

Description

@marcin-wicha

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions