markdown_it.main module

class markdown_it.main.MarkdownIt(config: Union[str, Mapping] = 'commonmark', options_update: Optional[Mapping] = None, *, renderer_cls: Callable[[markdown_it.main.MarkdownIt], markdown_it.renderer.RendererProtocol] = <class 'markdown_it.renderer.RendererHTML'>)[源代码]

基类:object

add_render_rule(name: str, function: Callable, fmt: str = 'html') None[源代码]

Add a rule for rendering a particular Token type.

Only applied when renderer.__output__ == fmt

configure(presets: Union[str, Mapping], options_update: Optional[Mapping] = None) markdown_it.main.MarkdownIt[源代码]

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: Union[str, Iterable[str]], ignoreInvalid: bool = False) markdown_it.main.MarkdownIt[源代码]

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

参数
  • names – rule name or list of rule names to disable.

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

enable(names: Union[str, Iterable[str]], ignoreInvalid: bool = False) markdown_it.main.MarkdownIt[源代码]

Enable list or rules. (chainable)

参数
  • 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]][源代码]

Return the names of all active rules.

get_all_rules() Dict[str, List[str]][源代码]

Return the names of all active rules.

Normalize destination URLs in links

[label]:   destination   'title'
        ^^^^^^^^^^^
normalizeLinkText(link: str) str[源代码]

Normalize autolink content

<destination>
~~~~~~~~~~~
parse(src: str, env: Optional[MutableMapping] = None) List[markdown_it.token.Token][源代码]

Parse the source string to a token stream

参数
  • 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: Optional[MutableMapping] = None) List[markdown_it.token.Token][源代码]

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

参数
  • 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: Optional[MutableMapping] = None) Any[源代码]

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

参数
  • src – source string

  • env – environment sandbox

返回

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: Optional[MutableMapping] = None) Any[源代码]

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

参数
  • 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][源代码]

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

set(options: MutableMapping) None[源代码]

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: Callable, *params, **options) markdown_it.main.MarkdownIt[源代码]

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.