Interface Renderer

Source
Expand description

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.

Creates new renderer instance and fills Renderer.rules with defaults.


interface Renderer {
    rules: Record<string, RendererRule>;
    renderAttrs(token: Pick<Token, "attrs">): string;
    renderToken(
        tokens: Token[],
        idx: number,
        options: Required<MarkdownItOptions>,
    ): string;
    renderInline(
        tokens: Token[],
        options: Required<MarkdownItOptions>,
        env: Env | undefined,
    ): string;
    renderInlineAsText(
        tokens: Token[],
        options: Required<MarkdownItOptions>,
        env: Env | undefined,
    ): string;
    render(
        tokens: Token[],
        options: Required<MarkdownItOptions>,
        env?: Env,
    ): string;
}

Properties§

§rules: Record<string, RendererRule> = ...

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

See source code for more details and examples.

Custom render rules
import MarkdownIt from 'markdown-it'
const md = new MarkdownIt()

md.renderer.rules.strong_open = function () { return '<b>'; };
md.renderer.rules.strong_close = function () { return '</b>'; };

const result = md.renderInline(...);
Each rule is called as independent static function with fixed signature
function my_token_render(tokens, idx, options, env, renderer) {
// ...
return renderedHTML;
}

Methods§

Source§

renderAttrs(token: Pick<Token, "attrs">): string

Render token attributes to string.

Source§

renderToken(
    tokens: Token[],
    idx: number,
    options: Required<MarkdownItOptions>,
): string

Default token renderer. Can be overriden by custom function in Renderer.rules.

Source§

renderInline(
    tokens: Token[],
    options: Required<MarkdownItOptions>,
    env: Env | undefined,
): string

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

Source§

renderInlineAsText(
    tokens: Token[],
    options: Required<MarkdownItOptions>,
    env: Env | undefined,
): string

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.

Source§

render(tokens: Token[], options: Required<MarkdownItOptions>, env?: Env): string

Takes token stream and generates HTML. Probably, you will never need to call this method directly.