# API Reference

Complete API documentation for Marked.

## Main Parsing Functions

### marked.parse()

```typescript { .api }
function marked(src: string, options?: MarkedOptions): string | Promise<string>;
function marked(src: string, options: MarkedOptions & { async: true }): Promise<string>;
function marked(src: string, options: MarkedOptions & { async: false }): string;

// Alias
function parse(src: string, options?: MarkedOptions): string | Promise<string>;
```

Compiles markdown to HTML.

**Parameters**:
- `src` - Markdown source string (empty strings return empty output, null/undefined throws error)
- `options` - Configuration options

**Returns**: HTML string or Promise of HTML string if async enabled

**Throws**: Error if parsing fails and `silent: false` (default)

**Edge Cases**:
- Empty string input returns empty string output
- Null or undefined input throws TypeError
- Invalid markdown with `silent: false` throws error with details
- Invalid markdown with `silent: true` returns HTML error message
- Very large inputs (>10MB) may have performance implications

### marked.parseInline()

```typescript { .api }
function parseInline(src: string, options?: MarkedOptions): string | Promise<string>;
function parseInline(src: string, options: MarkedOptions & { async: true }): Promise<string>;
function parseInline(src: string, options: MarkedOptions & { async: false }): string;
```

Compiles markdown to HTML without wrapping in paragraph tags.

**Use cases**: Image captions, link text, tooltips, inline content

### marked.setOptions()

```typescript { .api }
function setOptions(options: MarkedOptions): typeof marked;
```

Sets default options globally. Merges provided options with existing defaults.

**Returns**: The marked function for chaining

### marked.use()

```typescript { .api }
function use(...extensions: MarkedExtension[]): typeof marked;
```

Apply one or more extensions to marked. Extensions are applied in order.

**Returns**: The marked function for chaining

### marked.lexer()

```typescript { .api }
function lexer(src: string, options?: MarkedOptions): TokensList;
```

Tokenize markdown into a token tree.

**Returns**: TokensList with parsed tokens and link references

### marked.parser()

```typescript { .api }
function parser(tokens: Token[], options?: MarkedOptions): string;
```

Parse tokens into HTML.

**Throws**: Error if tokens are malformed

### marked.walkTokens()

```typescript { .api }
function walkTokens(
  tokens: Token[] | TokensList,
  callback: (token: Token) => void | Promise<void>
): (void | Promise<void>)[];
```

Walk through all tokens and execute a callback. Traverses the entire token tree including nested tokens.

### marked.getDefaults()

```typescript { .api }
function getDefaults(): MarkedOptions;
```

Gets the original default options (not current customized defaults). Returns a copy.

## Classes

### Marked

```typescript { .api }
class Marked {
  constructor(...extensions: MarkedExtension[]);
  
  parse(src: string, options?: MarkedOptions): string | Promise<string>;
  parseInline(src: string, options?: MarkedOptions): string | Promise<string>;
  setOptions(options: MarkedOptions): this;
  use(...extensions: MarkedExtension[]): this;
  walkTokens(tokens: Token[] | TokensList, callback: (token: Token) => void | Promise<void>): (void | Promise<void>)[];
  lexer(src: string, options?: MarkedOptions): TokensList;
  parser(tokens: Token[], options?: MarkedOptions): string;
  
  defaults: MarkedOptions;
  Parser: typeof Parser;
  Renderer: typeof Renderer;
  TextRenderer: typeof TextRenderer;
  Lexer: typeof Lexer;
  Tokenizer: typeof Tokenizer;
  Hooks: typeof Hooks;
}
```

Class for creating custom marked instances with independent configuration.

### Lexer

```typescript { .api }
class Lexer {
  constructor(options?: MarkedOptions);
  
  static lex(src: string, options?: MarkedOptions): TokensList;
  static lexInline(src: string, options?: MarkedOptions): Token[];
  static rules: { block: BlockRules; inline: InlineRules };
  
  lex(src: string): TokensList;
  blockTokens(src: string, tokens?: Token[]): Token[];
  inlineTokens(src: string, tokens?: Token[]): Token[];
  
  tokens: TokensList;
  options: MarkedOptions;
  state: {
    inLink: boolean;
    inRawBlock: boolean;
    top: boolean;
  };
}
```

Lexer class for tokenizing markdown.

[→ Complete Lexer Reference](./lexer.md)

### Parser

```typescript { .api }
class Parser {
  constructor(options?: MarkedOptions);
  
  static parse(tokens: Token[], options?: MarkedOptions): string;
  static parseInline(tokens: Token[], options?: MarkedOptions): string;
  
  parse(tokens: Token[]): string;
  parseInline(tokens: Token[], renderer?: Renderer | TextRenderer): string;
  
  options: MarkedOptions;
  renderer: Renderer;
}
```

Parser class for converting tokens to HTML.

[→ Complete Parser Reference](./parser-renderer.md)

### Renderer

```typescript { .api }
class Renderer {
  constructor(options?: MarkedOptions);
  
  // Block-level methods
  code(token: Tokens.Code): string;
  blockquote(token: Tokens.Blockquote): string;
  html(token: Tokens.HTML): string;
  heading(token: Tokens.Heading): string;
  hr(token: Tokens.Hr): string;
  list(token: Tokens.List): string;
  listitem(token: Tokens.ListItem): string;
  checkbox(token: Tokens.Checkbox): string;
  paragraph(token: Tokens.Paragraph): string;
  table(token: Tokens.Table): string;
  tablerow(token: Tokens.TableRow): string;
  tablecell(token: Tokens.TableCell): string;
  
  // Inline-level methods
  strong(token: Tokens.Strong): string;
  em(token: Tokens.Em): string;
  codespan(token: Tokens.Codespan): string;
  br(token: Tokens.Br): string;
  del(token: Tokens.Del): string;
  link(token: Tokens.Link): string;
  image(token: Tokens.Image): string;
  text(token: Tokens.Text | Tokens.Escape): string;
  
  options: MarkedOptions;
  parser: Parser;
}
```

Renderer class for generating HTML from tokens.

[→ Complete Renderer Reference](./parser-renderer.md)

### Tokenizer

```typescript { .api }
class Tokenizer {
  constructor(options?: MarkedOptions);
  
  // Block-level tokenizers
  space(src: string): Tokens.Space | undefined;
  code(src: string): Tokens.Code | undefined;
  fences(src: string): Tokens.Code | undefined;
  heading(src: string): Tokens.Heading | undefined;
  hr(src: string): Tokens.Hr | undefined;
  blockquote(src: string): Tokens.Blockquote | undefined;
  list(src: string): Tokens.List | undefined;
  html(src: string): Tokens.HTML | undefined;
  def(src: string): Tokens.Def | undefined;
  table(src: string): Tokens.Table | undefined;
  lheading(src: string): Tokens.Heading | undefined;
  paragraph(src: string): Tokens.Paragraph | undefined;
  text(src: string): Tokens.Text | undefined;
  
  // Inline-level tokenizers
  escape(src: string): Tokens.Escape | undefined;
  tag(src: string): Tokens.Tag | undefined;
  link(src: string): Tokens.Link | Tokens.Image | undefined;
  reflink(src: string, links: Links): Tokens.Link | Tokens.Image | Tokens.Text | undefined;
  emStrong(src: string, maskedSrc: string, prevChar: string): Tokens.Em | Tokens.Strong | undefined;
  codespan(src: string): Tokens.Codespan | undefined;
  br(src: string): Tokens.Br | undefined;
  del(src: string): Tokens.Del | undefined;
  autolink(src: string): Tokens.Link | undefined;
  url(src: string): Tokens.Link | undefined;
  inlineText(src: string): Tokens.Text | undefined;
  
  options: MarkedOptions;
  rules: Rules;
  lexer: Lexer;
}
```

Tokenizer class with methods for each markdown element type.

[→ Complete Tokenizer Reference](./tokenizer.md)

### Hooks

```typescript { .api }
class Hooks {
  constructor(options?: MarkedOptions);
  
  preprocess(markdown: string): string | Promise<string>;
  postprocess(html: string): string | Promise<string>;
  processAllTokens(tokens: Token[] | TokensList): Token[] | TokensList | Promise<Token[] | TokensList>;
  emStrongMask(src: string): string;
  provideLexer(): typeof Lexer.lex | typeof Lexer.lexInline;
  provideParser(): typeof Parser.parse | typeof Parser.parseInline;
  
  options: MarkedOptions;
  block?: boolean;
}
```

Hooks for intercepting and modifying the parsing process.

[→ Complete Hooks Reference](./hooks.md)

### TextRenderer

```typescript { .api }
class TextRenderer {
  strong(token: Tokens.Strong): string;
  em(token: Tokens.Em): string;
  codespan(token: Tokens.Codespan): string;
  del(token: Tokens.Del): string;
  html(token: Tokens.HTML): string;
  text(token: Tokens.Text): string;
  link(token: Tokens.Link): string;
  image(token: Tokens.Image): string;
  br(): string;
  checkbox(token: Tokens.Checkbox): string;
}
```

Text-only renderer (no HTML tags). Useful for extracting plain text from markdown.

## Interfaces

### MarkedOptions

```typescript { .api }
interface MarkedOptions {
  async?: boolean;        // Enable async parsing
  breaks?: boolean;       // Convert \n to <br>
  gfm?: boolean;          // Enable GitHub Flavored Markdown
  pedantic?: boolean;     // Conform to markdown.pl
  silent?: boolean;       // Show HTML error message instead of throwing
  hooks?: Hooks | null;
  renderer?: Renderer | null;
  tokenizer?: Tokenizer | null;
  extensions?: ExtensionConfig | null;
  walkTokens?: ((token: Token) => void | Promise<void>) | null;
}
```

[→ Complete Options Reference](./options.md)

### MarkedExtension

```typescript { .api }
interface MarkedExtension {
  async?: boolean;
  breaks?: boolean;
  gfm?: boolean;
  pedantic?: boolean;
  silent?: boolean;
  extensions?: (TokenizerExtension | RendererExtension)[] | null;
  hooks?: Partial<Hooks> | null;
  renderer?: Partial<Renderer> | null;
  tokenizer?: Partial<Tokenizer> | null;
  walkTokens?: ((token: Token) => void | Promise<void>) | null;
}
```

[→ Complete Extension Reference](./extensions.md)

### Token Types

[→ Complete Token Types Reference](./types.md)

## Properties

### marked.defaults

```typescript { .api }
const defaults: MarkedOptions;
```

Current default options object (live reference).

## Related Documentation

- [Extension System](./extensions.md)
- [Hooks System](./hooks.md)
- [Lexer Details](./lexer.md)
- [Parser & Renderer Details](./parser-renderer.md)
- [Tokenizer Details](./tokenizer.md)
- [Token Types](./types.md)
- [Configuration Options](./options.md)
