Pompeyo
Pompeyo

Reputation: 1469

Display text without markdown syntax in Django

Is it possible to display just text without the markdown syntax with Django?

If a user adds some text with markdown syntax and if I don't call markdown in my template ({{some_text}}) I get something like this:

**this is a test** _with markdown_ `syntax`

what I would like to display is pure text like this:

this is a test with markdown syntax 

Upvotes: 0

Views: 1924

Answers (2)

Waylan
Waylan

Reputation: 42647

As Markdown's documentation states:

Markdown provides backslash escapes for the following characters:

\   backslash
`   backtick
*   asterisk
_   underscore
{}  curly braces
[]  square brackets
()  parentheses
#   hash mark
+   plus sign
-   minus sign (hyphen)
.   dot
!   exclamation mark

You could write a template tag that strips out any of those characters as long as they are not preceded by a backslash escape (and only strips the backslash when they are).

However, Markdown doesn't always give those characters special meaning. For example:

These (parenthesis) are ignored by Markdown.

However [these](aren't) because they are part of a link.

In other words, some times you may want to strip the above list of characters, but sometimes you may not. In the end, you may find yourself re-implementing a markdown parser. Might as well use one that already exists and then simply pass the html through Django's built-in striptags filter. Or if you want to maintain block level tags and only strip a small selection of (inline) tags, the removetags filter may serve you better.

In either case, you don't need to write any Python code as you can just string a few existing filters together.

{{ value|markdown|striptags }}

Upvotes: 3

snahor
snahor

Reputation: 1201

Create a template filter to remove those characters. For instance, convert the markdown to html and then apply striptags.

Upvotes: 1

Related Questions