tessl install tessl/npm-marked@17.0.0A markdown parser built for speed
Marked is a high-performance, low-level markdown compiler that parses markdown without caching or blocking operations. It supports CommonMark and GitHub Flavored Markdown with zero runtime dependencies.
import { marked } from "marked";
const html = marked.parse('# Hello **World**');
// Output: <h1>Hello <strong>World</strong></h1>Security: Marked does not sanitize HTML. Always use DOMPurify on output.
marked (npm)npm install marked| Function | Purpose | Returns |
|---|---|---|
marked.parse(src, options?) | Parse markdown to HTML | string | Promise<string> |
marked.parseInline(src, options?) | Parse inline markdown only | string | Promise<string> |
marked.setOptions(options) | Set global options | typeof marked |
marked.use(...extensions) | Add extensions | typeof marked |
marked.lexer(src, options?) | Tokenize markdown | TokensList |
marked.parser(tokens, options?) | Parse tokens to HTML | string |
// Main import
import { marked } from "marked";
// Specific imports
import {
marked, // Main parse function
parse, // Alias for marked
parseInline, // Parse inline markdown only
Lexer, // Tokenizer class
Parser, // Parser class
Renderer, // HTML renderer class
Tokenizer, // Low-level tokenizer class
Hooks, // Processing hooks class
Marked // Instance class for isolated configs
} from "marked";
// CommonJS
const { marked } = require("marked");Marked uses a two-phase architecture:
Lexer tokenizes markdown into a structured token treeParser walks tokens and uses Renderer to generate HTMLProcessing Pipeline:
Markdown → [Preprocess Hook] → Lexer → [ProcessAllTokens Hook] →
[WalkTokens] → Parser + Renderer → [Postprocess Hook] → HTMLBenefits:
| Use Case | Guide | Example |
|---|---|---|
| Basic markdown parsing | Quick Start | Basic Usage |
| Custom syntax | Extensions Guide | Custom Tokens |
| Syntax highlighting | Rendering Guide | Code Highlighting |
| Custom HTML output | Rendering Guide | Custom Renderer |
| Front matter support | Extensions Guide | Front Matter |
| Table of contents | Token Walking | TOC Generation |
// Set global options
marked.setOptions({
gfm: true, // Enable GitHub Flavored Markdown
breaks: true, // Convert \n to <br>
pedantic: false, // Don't fix markdown bugs
silent: false // Throw on errors
});
// Per-parse options
const html = marked.parse(markdown, { breaks: false });
// Isolated instances
const gfmMarked = new Marked({ gfm: true, breaks: true });Marked supports four types of extensions:
// Add custom emoji syntax
marked.use({
extensions: [{
name: 'emoji',
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src) {
const match = src.match(/^:([a-z_]+):/);
if (match) {
return { type: 'emoji', raw: match[0], name: match[1] };
}
},
renderer(token) {
return `<span class="emoji emoji-${token.name}"></span>`;
}
}]
});→ Extension System Guide | → Extension Reference
Critical: Marked does NOT sanitize HTML output.
import { marked } from "marked";
import DOMPurify from "dompurify";
// UNSAFE
const unsafeHtml = marked.parse(userInput);
// SAFE
const dirty = marked.parse(userInput);
const clean = DOMPurify.sanitize(dirty);Recommended sanitization libraries:
// Throw errors (default)
try {
const html = marked.parse(markdown);
} catch (err) {
console.error('Parse error:', err.message);
}
// Silent mode (return error as HTML)
marked.setOptions({ silent: true });
const html = marked.parse(markdown);// Enable async mode
marked.setOptions({ async: true });
// Use async extensions
marked.use({
async: true,
async walkTokens(token) {
if (token.type === 'link') {
await validateUrl(token.href);
}
}
});
// Must await result
const html = await marked.parse(markdown, { async: true });# Install globally
npm install -g marked
# Parse file
marked input.md -o output.html
# Parse with options
marked input.md --gfm --breaks
# From stdin
echo "# Hello" | markedFrom markdown-it:
// markdown-it
const md = require('markdown-it')();
const html = md.render(markdown);
// marked
import { marked } from "marked";
const html = marked.parse(markdown);From showdown:
// showdown
const converter = new showdown.Converter();
const html = converter.makeHtml(markdown);
// marked
import { marked } from "marked";
const html = marked.parse(markdown);// Cache results for repeated content
const cache = new Map();
function cachedParse(markdown) {
if (cache.has(markdown)) {
return cache.get(markdown);
}
const html = marked.parse(markdown);
cache.set(markdown, html);
return html;
}Full TypeScript support with comprehensive type definitions:
import { marked, MarkedOptions, Token, Tokens } from "marked";
// Type-safe configuration
const options: MarkedOptions = {
gfm: true,
breaks: true
};
// Type guards for token handling
function processToken(token: Token): void {
if (token.type === 'heading') {
// TypeScript knows this is Tokens.Heading
console.log(`Level ${token.depth}: ${token.text}`);
}
}