markdown_it.renderer module#

class Renderer

Generates HTML from parsed token stream. Each instance has independent copy of rules. Those can be rewritten with ease. Also, you can add new rules if you create plugin and adds new token types.

class markdown_it.renderer.RendererHTML(parser: Any | None = None)[source]#

Bases: RendererProtocol

Contains render rules for tokens. Can be updated and extended.

Example:

Each rule is called as independent static function with fixed signature:

class Renderer:
    def token_type_name(self, tokens, idx, options, env) {
        # ...
        return renderedHTML
class CustomRenderer(RendererHTML):
    def strong_open(self, tokens, idx, options, env):
        return '<b>'
    def strong_close(self, tokens, idx, options, env):
        return '</b>'

md = MarkdownIt(renderer_cls=CustomRenderer)

result = md.render(...)

See markdown-it/markdown-it for more details and examples.

code_block(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
code_inline(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
fence(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
hardbreak(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
html_block(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
html_inline(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
image(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
render(tokens: Sequence[Token], options: OptionsDict, env: EnvType) str[source]#

Takes token stream and generates HTML.

Parameters:
  • tokens – list on block tokens to render

  • options – params of parser instance

  • env – additional data from parsed input

static renderAttrs(token: Token) str[source]#

Render token attributes to string.

renderInline(tokens: Sequence[Token], options: OptionsDict, env: EnvType) str[source]#

The same as render, but for single token of inline type.

Parameters:
  • tokens – list on block tokens to render

  • options – params of parser instance

  • env – additional data from parsed input (references, for example)

renderInlineAsText(tokens: Sequence[Token] | None, options: OptionsDict, env: EnvType) str[source]#

Special kludge for image alt attributes to conform CommonMark spec.

Don’t try to use it! Spec requires to show alt content with stripped markup, instead of simple escaping.

Parameters:
  • tokens – list on block tokens to render

  • options – params of parser instance

  • env – additional data from parsed input

renderToken(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#

Default token renderer.

Can be overridden by custom function

Parameters:
  • idx – token index to render

  • options – params of parser instance

softbreak(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
text(tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType) str[source]#
class markdown_it.renderer.RendererProtocol(*args, **kwargs)[source]#

Bases: Protocol

render(tokens: Sequence[Token], options: OptionsDict, env: EnvType) Any[source]#