Marked.js Markdown Parser
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
-
ReferenceError: require is not defined
cause Attempting to use `require()` for Marked.js in an ECMAScript Module (ESM) context (e.g., `"type": "module"` in package.json, or `.mjs` files) where CommonJS `require` is not natively available.fixChange your import statement to `import { marked } from 'marked';` or `import { parse } from 'marked';` for ESM environments. -
TypeError: marked.parse is not a function
cause This usually occurs when attempting to call `marked.parse()` after importing `marked` as a default export (`import marked from 'marked';`) in an environment where `marked` is a named export object, or when using an incorrect UMD/browser script.fixFor Node.js/bundlers using ESM, ensure you use `import { marked } from 'marked';` and then call `marked.parse()`. If using the browser UMD bundle (`marked.umd.js`), the `marked` global object should have the `parse` method directly. -
Unescaped HTML or XSS vulnerability detected in output.
cause Markdown input contained malicious HTML or scripts (e.g., `<script>alert('xss')</script>`), and the output HTML was rendered without proper sanitization. Marked.js does not sanitize its output by design.fixIntegrate a robust HTML sanitization library, such as DOMPurify, and run the `marked.parse()` output through it before rendering. Example: `DOMPurify.sanitize(marked.parse(input))`.
Warnings
- breaking Marked.js versions 18.0.0 and above now depend on TypeScript 6.0.2. Projects using older TypeScript versions or specific TS features that are incompatible with this version may encounter compilation issues. Ensure your project's TypeScript setup is compatible.
- breaking Version 17.0.0 introduced significant internal changes to how tokenizers and renderers operate, specifically affecting consecutive text tokens in lists, the `listItem` renderer, and the addition of a `CheckboxToken` with new `type` and `raw` properties. Custom extensions that directly interact with or override these internal mechanisms will likely require updates.
- gotcha Marked.js explicitly does NOT sanitize its HTML output. Directly rendering output from untrusted Markdown input can lead to Cross-Site Scripting (XSS) vulnerabilities. This is a deliberate design choice for performance and flexibility.
- gotcha Marked.js only supports current and LTS (Long Term Support) Node.js versions. Using End-of-Life (EOL) Node.js versions may lead to unexpected behavior, compatibility issues, or even prevent the library from functioning correctly at any point.
- gotcha Previous versions of Marked.js (e.g., v17.0.4, v17.0.5) contained fixes for Redos (Catastrophic Backtracking) vulnerabilities in its internal regular expressions. While fixed in current releases, this highlights the importance of keeping the library updated to protect against potential Denial-of-Service attacks when parsing malicious or overly complex Markdown input.
Install
-
npm install marked -
yarn add marked -
pnpm add marked
Imports
- marked
const marked = require('marked');import { marked } from 'marked'; - parse
import marked from 'marked'; // then marked('text')import { parse } from 'marked'; - MarkedExtension
import { MarkedExtension } from 'marked'; // for runtime usageimport type { MarkedExtension } from 'marked';
Quickstart
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);