Markdown-it Parser
Markdown-it is a high-performance and highly extensible Markdown parser, currently at version 14.1.1. It strictly follows the CommonMark specification while also offering several useful syntax extensions like URL autolinking, smart typography (typographer), and HTML tag support. Known for its speed and configurability, users can easily add, modify, or replace parsing rules and extend functionality via a rich ecosystem of community-developed plugins available on npm. It prioritizes safety by design and is suitable for both Node.js and browser environments. The project maintains an active development pace with frequent updates, ensuring ongoing compatibility and feature enhancements.
Common errors
-
TypeError: markdownit is not a constructor
cause Attempting to `import { markdownit } from 'markdown-it'` (named import) instead of `import markdownit from 'markdown-it'` (default import) in an ESM environment, or mixing CJS `require` with an ESM-only context.fixUse `import markdownit from 'markdown-it'` for ESM or `const markdownit = require('markdown-it')` for CommonJS. -
ReferenceError: markdownit is not defined
cause In a browser environment, the `markdown-it` UMD build exposes its API on `window.markdownit`. Directly using `markdownit()` without `window.` will result in this error.fixEnsure you are referencing `window.markdownit()` when using the UMD build directly in the browser. -
My custom plugin isn't working or is conflicting with existing rules.
cause Incorrectly registered the plugin, specified a wrong rule name, or set an incorrect rule priority, leading to it not firing or being overridden.fixVerify plugin registration (`md.use(plugin)`). Check rule names for typos. Adjust rule insertion methods (`.before()`, `.after()`, `.at()`) and priorities to ensure correct execution order.
Warnings
- gotcha When using markdown-it in a browser environment via a `<script>` tag, the global variable created is `window.markdownit` (without a hyphen). Attempting to access `window.markdown-it` will result in a `ReferenceError`.
- breaking Earlier versions of markdown-it (prior to v11) had a different default preset. If you are upgrading from a very old version, some default rules or options may have changed, potentially altering rendering output.
- gotcha While markdown-it is designed to be safe by default, using third-party plugins can introduce security vulnerabilities if they are not properly audited. Always review plugin code for potential XSS or other injection risks, especially if processing untrusted user input.
- gotcha Managing rule priorities and order when adding or replacing custom parsing rules can be complex. Incorrect rule configuration can lead to unexpected parsing behavior or conflicts between rules.
Install
-
npm install markdown-it -
yarn add markdown-it -
pnpm add markdown-it
Imports
- markdownit
import { markdownit } from 'markdown-it'import markdownit from 'markdown-it'
- markdownit
const markdownit = require('markdown-it') - MarkdownIt
import type MarkdownIt from 'markdown-it'
- window.markdownit
const md = window.markdownit()
Quickstart
import markdownit from 'markdown-it';
// Initialize markdown-it with specific options
const md = markdownit({
html: true, // Enable HTML tags in source
linkify: true, // Autoconvert URL-like text to links
typographer: true // Enable smart quotes and other typographic replacements
});
// Example 1: Basic rendering
const markdownText1 = '# Hello, markdown-it!\n\nThis is a *simple* paragraph with an [example link](https://example.com).';
const htmlResult1 = md.render(markdownText1);
console.log('Basic Render:\n', htmlResult1);
// Example 2: Render inline content (without paragraph wrap)
const markdownText2 = '__markdown-it__ rulezz!';
const htmlResult2 = md.renderInline(markdownText2);
console.log('\nInline Render:\n', htmlResult2);
// Example 3: Demonstrating typographer and linkify
const markdownText3 = 'Check out google.com or write "hello" here.';
const htmlResult3 = md.render(markdownText3);
console.log('\nTypographer & Linkify:\n', htmlResult3);