Introduction
markdown-it is a fast, spec-compliant markdown parser for JavaScript. It follows the CommonMark specification and adds GFM (GitHub Flavored Markdown) extensions through plugins. The library runs in Node.js and browsers, making it suitable for both server-side rendering and client-side previews.
What markdown-it Does
- Parses markdown text into HTML following the CommonMark spec
- Supports tables, strikethrough, and other GFM extensions via plugins
- Provides a plugin API for adding custom syntax rules
- Generates an AST (token stream) for programmatic manipulation
- Handles typographic replacements and smart quotes
Architecture Overview
markdown-it uses a two-stage pipeline: the parser tokenizes markdown input into a flat token stream, and the renderer converts tokens into HTML output. The parser is split into block-level and inline-level rule chains. Plugins can inject rules at any point in either chain, making it possible to add custom block elements or inline syntax without modifying the core.
Self-Hosting & Configuration
- Install via npm:
npm install markdown-it - Enable presets:
markdownit('commonmark')for strict CommonMark - Add GFM features:
markdownit().enable(['table', 'strikethrough']) - Use plugins:
md.use(require('markdown-it-footnote')) - Configure options:
html,linkify,typographer,breaks
Key Features
- Full CommonMark compliance with the official test suite
- Plugin ecosystem: footnotes, containers, task lists, anchors, math
- Linkify support for auto-detecting URLs
- Configurable security: HTML tag filtering, link validation
- Fast performance suitable for real-time preview rendering
Comparison with Similar Tools
- Marked — simpler API but fewer plugin options
- Remark (unified) — AST-based ecosystem, more powerful transforms but heavier
- Showdown — older library, less actively maintained
- CommonMark.js — reference implementation, strict but no plugin system
- Micromark — low-level parser used by remark, not meant for direct use
FAQ
Q: How do I enable GitHub-style tables?
A: Tables are enabled by default in the default preset. For commonmark preset, call md.enable('table').
Q: Is HTML pass-through safe?
A: Set html: false (the default) to strip raw HTML. For user-generated content, also use a sanitizer like DOMPurify on the output.
Q: Can I render to something other than HTML? A: Yes, you can write a custom renderer that processes the token stream into any output format.
Q: How does performance compare to other parsers? A: markdown-it is among the fastest JS parsers, suitable for real-time keystroke-level rendering in editors.