Home / Market / Use markdown-it to render Markdown to HTML

Use markdown-it to render Markdown to HTML

Use markdown-it to render Markdown to HTML, configure plugins, custom rendering rules, syntax highlighting.

skill★ 00 dlclawhubMIT-0Install in CosmosUpstream Source

Documentation & Preview

markdown-it Architecture Principles

Data Flow

Input is parsed through three nested rule chains: core → block → inline.

core
    core.rule1 (normalize)
    ...
    core.ruleX
    block
        block.rule1 (blockquote)
        ...
        block.ruleX
    core.ruleX1 (intermediate rules operating on block tokens)
    ...
    inline (applied to each block token of type="inline")
        inline.rule1 (text)
        ...
        inline.ruleX
    core.ruleYY (applied to all tokens)
    ... (abbreviation, footnote, typographer, linkify)

Each chain (core / block / inline) has its own state object, making parsing independent and rules can be disabled at any time.

Token Stream

Does not use a traditional AST, but rather a token array (Token Stream). Tokens are simple sequences:

Summary:

Rules

A Rule is a function that operates on the parser state, managed by name via the Ruler instance, and can be enabled/disabled.

Some rules have a "validation mode" — they only look ahead without modifying the token stream. This is a core design principle: the token stream is "write-only" during the block/inline parsing phase.

Rules are independent of each other, so they can be safely enabled/disabled/added. To write a new rule, refer to existing plugins and rule files:

Renderer

After the token stream is generated, it is passed to the Renderer, which traverses the tokens and calls the corresponding rendering function:

function (tokens, idx, options, env, renderer) {
  return htmlResult;
}

Rendering rules are stored in md.renderer.rules[name]. Overriding them directly modifies the output without changing the parser.

Parsing Order Summary

  1. Parse blocks, populate the top level of the token stream
  2. Parse inline container content, populate children
  3. Render

Additional transformations can be inserted in between.