Marked.js Markdown Parser

18.0.2 · active · verified Tue Apr 21

Marked.js is a high-performance Markdown parser designed to efficiently convert Markdown text into HTML. It is currently at version 18.0.2 and maintains a very active release cadence, with frequent patch and minor versions often released weekly or bi-weekly, and major versions arriving every few months. Key differentiators include its strong focus on speed, its architecture as a low-level compiler that avoids caching and prolonged blocking operations, and its lightweight footprint. It aims to implement all Markdown features from supported specifications and is versatile, capable of running in browser environments, on a server (Node.js), or via its command-line interface. A critical consideration for users is that Marked.js intentionally does *not* sanitize its HTML output, necessitating the integration of a separate sanitization library like DOMPurify for any security-sensitive applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic Markdown parsing with Marked.js, including critical HTML sanitization using DOMPurify and configuring options.

import { marked } from 'marked';
import DOMPurify from 'dompurify';

const markdownInput = `# Hello from Marked.js!

This is a paragraph with **bold** and *italic* text.

- List item 1
- List item 2

### Code Example

```javascript
function greet(name) {
  console.log('Hello, ' + name + '!');
}
greet('World');
```

<script>alert('XSS attempt!');</script>`;

// Parse the markdown to HTML
const unsafeHTML = marked.parse(markdownInput);

// Sanitize the HTML output (CRITICAL STEP for untrusted input)
const safeHTML = DOMPurify.sanitize(unsafeHTML);

console.log('--- Unsafe HTML (for demonstration) ---\n', unsafeHTML);
console.log('\n--- Safe HTML (after DOMPurify) ---\n', safeHTML);

// Example with custom options
marked.setOptions({
  gfm: true, // GitHub Flavored Markdown
  breaks: true, // Interpret line breaks as <br/>
  headerIds: false // Disable auto-generated header IDs
});

const customParsedHTML = DOMPurify.sanitize(marked.parse(`## Custom Options Test\nLine 1\nLine 2`));
console.log('\n--- HTML with Custom Options ---\n', customParsedHTML);

view raw JSON →