John Fred
John Fred

Reputation: 77

markdown2 not adding <pre> to code snippets

Usually code snippets wrap the code tag with a pre tag. Looks like markdown is just using a p tag, is this normal?

from markdown2 import Markdown
markdowner = Markdown()
markdowner.convert("```\nthis is code\n```")
u'<p><code>\nthis is code\n</code></p>\n'

Even this website's adding pre tags. How do I add it to markdown?

Upvotes: 0

Views: 530

Answers (2)

Waylan
Waylan

Reputation: 42647

is this normal?

Yes, fenced code blocks are not standard Markdown (only indented code blocks are). However, inline code spans can be deliminated with any number of backticks (as long as both opening an closing deliminators match). Therefore, the parser is correctly parsing your input as an inline code span which consists of a code tag inside a p tag. Of course, if you had inserted any blank lines, then the output would have been multiple paragraphs without any code spans (as the opening and closing deliminators would have been in separate paragraphs).

How do I add it to markdown?

As fenced code blocks are non-standard Markdown, they generally need to be enabled in parsers which support them. Each parser is different, so users should consult the documentation for their parser of choice. The other answer already covers how to enable them in the specific parser used by the OP.

Upvotes: 1

John Fred
John Fred

Reputation: 77

Turns out markdown2 only adds pre's to what is indented by four spaces. To add to above example, use:

markdown2.markdown(text, extras=["fenced-code-blocks"])

Reference

Upvotes: 0

Related Questions