or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/marked@17.0.x

docs

index.md
tile.json

tessl/npm-marked

tessl install tessl/npm-marked@17.0.0

A markdown parser built for speed

quick-start.mddocs/guides/

Quick Start Guide

Get started with Marked in minutes.

Installation

npm install marked

Basic Usage

Simple Parsing

import { marked } from "marked";

const markdown = '# Hello World\n\nThis is **markdown**.';
const html = marked.parse(markdown);

console.log(html);
// Output: <h1>Hello World</h1>\n<p>This is <strong>markdown</strong>.</p>\n

Inline Parsing

Parse inline markdown without wrapping <p> tags:

import { marked } from "marked";

const inline = marked.parseInline('This is **bold** text');
console.log(inline);
// Output: This is <strong>bold</strong> text

Use cases: Tooltips, short descriptions, inline content

Imports

ES Modules (Recommended)

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");

UMD (Browser)

<script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
<script>
  const html = marked.parse('# Hello');
</script>

TypeScript

import { marked, MarkedOptions, Token, Tokens } from "marked";

const options: MarkedOptions = {
  gfm: true,
  breaks: true
};

marked.setOptions(options);

Configuration

Global Options

import { marked } from "marked";

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
});

const html = marked.parse(markdown);

Per-Parse Options

import { marked } from "marked";

// Override global options for specific parse
const html = marked.parse(markdown, {
  breaks: false,  // Override global setting
  gfm: true
});

Isolated Instances

import { Marked } from "marked";

// Create instance for GitHub Flavored Markdown
const gfmMarked = new Marked({
  gfm: true,
  breaks: true
});

// Create instance for strict CommonMark
const strictMarked = new Marked({
  gfm: false,
  pedantic: false
});

// Each instance has independent configuration
const html1 = gfmMarked.parse('Line 1\nLine 2'); // Has <br>
const html2 = strictMarked.parse('Line 1\nLine 2'); // No <br>

Security

Critical: Marked does NOT sanitize HTML output.

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

// UNSAFE - vulnerable to XSS
const unsafeHtml = marked.parse(userInput);

// SAFE - sanitized output
const dirty = marked.parse(userInput);
const clean = DOMPurify.sanitize(dirty);

// Use clean HTML in your application
document.getElementById('content').innerHTML = clean;

Recommended sanitization libraries:

  • DOMPurify - Most comprehensive
  • sanitize-html - Configurable
  • js-xss - Lightweight

Error Handling

Throw Errors (Default)

import { marked } from "marked";

try {
  const html = marked.parse(malformedMarkdown);
} catch (err) {
  console.error('Parse error:', err.message);
}

Silent Mode

import { marked } from "marked";

marked.setOptions({ silent: true });
const html = marked.parse(malformedMarkdown);
// Returns: <p>An error occurred:</p><pre>Error message</pre>

Use silent mode for: User-generated content, preview functionality

Common Options

GitHub Flavored Markdown

import { marked } from "marked";

marked.setOptions({
  gfm: true,
  breaks: true
});

const html = marked.parse(`
# Title

Line 1
Line 2

| A | B |
|---|---|
| 1 | 2 |

~~strikethrough~~

- [ ] Task 1
- [x] Task 2
`);

Strict CommonMark

import { marked } from "marked";

marked.setOptions({
  gfm: false,
  breaks: false,
  pedantic: false
});

Original markdown.pl Compatibility

import { marked } from "marked";

marked.setOptions({
  pedantic: true,
  gfm: false
});

Async Parsing

import { marked } from "marked";

// Enable async mode
marked.setOptions({ async: true });

const html = await marked.parse('# Async Markdown');

// Or pass async option directly
const html2 = await marked.parse('# Title', { async: true });

When to use async: Required for async walkTokens callbacks, async hooks, or extensions that perform async operations.

CLI Usage

# 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" | marked

# Multiple files
marked file1.md file2.md --output-dir dist/

Next Steps

  • Extensions Guide - Add custom syntax and rendering
  • Rendering Guide - Customize HTML output
  • Token Manipulation - Work with token trees
  • Real-World Examples - Complete usage examples
  • API Reference - Complete API documentation

Common Patterns

Document Parsing

import { marked } from "marked";
import fs from "fs";

const markdown = fs.readFileSync('README.md', 'utf-8');
const html = marked.parse(markdown, { gfm: true });
fs.writeFileSync('README.html', html);

User Comments

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

function renderComment(userComment) {
  const html = marked.parse(userComment);
  return DOMPurify.sanitize(html);
}

Blog Posts

import { marked } from "marked";

marked.setOptions({
  gfm: true,
  breaks: false
});

const postHtml = marked.parse(blogPost);

Tooltips and Short Text

import { marked } from "marked";

const tooltipHtml = marked.parseInline('This is **important**');
// Use parseInline for short content without <p> wrapper

Edge Cases

Empty Input

marked.parse('');              // Returns: ''
marked.parse(null);            // Throws: TypeError
marked.parse(undefined);       // Throws: TypeError

Large Input

// Very large inputs (>10MB) may have performance implications
const largeMarkdown = '...'; // 10MB+

console.time('parse');
const html = marked.parse(largeMarkdown);
console.timeEnd('parse');
// Typical: < 1 second for 10MB

Troubleshooting

See Troubleshooting Guide for common issues and solutions.