markdown_it.main module#

class markdown_it.main.MarkdownIt(config: str | Mapping = 'commonmark', options_update: Mapping | None = None, *, renderer_cls: Callable[[MarkdownIt], RendererProtocol] = <class 'markdown_it.renderer.RendererHTML'>)[source]#

Bases: object

add_render_rule(name: str, function: collections.abc.Callable, fmt: str = 'html') None[source]#

Add a rule for rendering a particular Token type.

Only applied when renderer.__output__ == fmt

configure(presets: str | Mapping, options_update: Mapping | None = None) MarkdownIt[source]#

Batch load of all options and component settings. This is an internal method, and you probably will not need it. But if you will - see available presets and data structure [here](https://github.com/markdown-it/markdown-it/tree/master/lib/presets)

We strongly recommend to use presets instead of direct config loads. That will give better compatibility with next versions.

disable(names: str | Iterable[str], ignoreInvalid: bool = False) MarkdownIt[source]#

The same as [[MarkdownIt.enable]], but turn specified rules off. (chainable)

Parameters
  • names – rule name or list of rule names to disable.

  • ignoreInvalid – set true to ignore errors when rule not found.

enable(names: str | Iterable[str], ignoreInvalid: bool = False) MarkdownIt[source]#

Enable list or rules. (chainable)

Parameters
  • names – rule name or list of rule names to enable.

  • ignoreInvalid – set true to ignore errors when rule not found.

It will automatically find appropriate components, containing rules with given names. If rule not found, and ignoreInvalid not set - throws exception.

Example:

md = MarkdownIt().enable(['sub', 'sup']).disable('smartquotes')
get_active_rules() dict[str, list[str]][source]#

Return the names of all active rules.

get_all_rules() dict[str, list[str]][source]#

Return the names of all active rules.

Normalize destination URLs in links

[label]:   destination   'title'
        ^^^^^^^^^^^
normalizeLinkText(link: str) str[source]#

Normalize autolink content

<destination>
~~~~~~~~~~~
parse(src: str, env: MutableMapping | None = None) list[Token][source]#

Parse the source string to a token stream

Parameters
  • src – source string

  • env – environment sandbox

Parse input string and return list of block tokens (special token type “inline” will contain list of inline tokens).

env is used to pass data between “distributed” rules and return additional metadata like reference info, needed for the renderer. It also can be used to inject data in specific cases. Usually, you will be ok to pass {}, and then pass updated object to renderer.

parseInline(src: str, env: MutableMapping | None = None) list[Token][source]#

The same as [[MarkdownIt.parse]] but skip all block rules.

Parameters
  • src – source string

  • env – environment sandbox

It returns the block tokens list with the single inline element, containing parsed inline tokens in children property. Also updates env object.

render(src: str, env: MutableMapping | None = None) Any[source]#

Render markdown string into html. It does all magic for you :).

Parameters
  • src – source string

  • env – environment sandbox

Returns

The output of the loaded renderer

env can be used to inject additional metadata ({} by default). But you will not need it with high probability. See also comment in [[MarkdownIt.parse]].

renderInline(src: str, env: MutableMapping | None = None) Any[source]#

Similar to [[MarkdownIt.render]] but for single paragraph content.

Parameters
  • src – source string

  • env – environment sandbox

Similar to [[MarkdownIt.render]] but for single paragraph content. Result will NOT be wrapped into <p> tags.

reset_rules() Generator[None, None, None][source]#

A context manager, that will reset the current enabled rules on exit.

set(options: collections.abc.MutableMapping) None[source]#

Set parser options (in the same format as in constructor). Probably, you will never need it, but you can change options after constructor call.

__Note:__ To achieve the best possible performance, don’t modify a markdown-it instance options on the fly. If you need multiple configurations it’s best to create multiple instances and initialize each with separate config.

use(plugin: collections.abc.Callable, *params, **options) markdown_it.main.MarkdownIt[source]#

Load specified plugin with given params into current parser instance. (chainable)

It’s just a sugar to call plugin(md, params) with curring.

Example:

def func(tokens, idx):
    tokens[idx].content = tokens[idx].content.replace('foo', 'bar')
md = MarkdownIt().use(plugin, 'foo_replace', 'text', func)

Validate if the URL link is allowed in output.

This validator can prohibit more than really needed to prevent XSS. It’s a tradeoff to keep code simple and to be secure by default.

Note: the url should be normalized at this point, and existing entities decoded.