# Token Manipulation Guide

Work with Marked's token tree for advanced processing.

## Token Walking

```javascript { .api }
import { marked } from "marked";

marked.use({
  walkTokens(token) {
    if (token.type === 'heading') {
      // Add custom ID to headings
      token.id = token.text.toLowerCase().replace(/[^\w]+/g, '-');
    }
  },
  renderer: {
    heading({ tokens, depth, id }) {
      const text = this.parser.parseInline(tokens);
      return `<h${depth} id="${id}">${text}</h${depth}>\n`;
    }
  }
});
```

## Low-Level API

```javascript { .api }
import { marked } from "marked";

// Tokenize
const tokens = marked.lexer('# Hello\n\nThis is **markdown**.');

// Modify tokens
tokens.forEach(token => {
  if (token.type === 'heading') {
    token.depth += 1; // Demote all headings
  }
});

// Parse modified tokens
const html = marked.parser(tokens);
```

## Detailed Documentation

For complete token documentation, see:

- [Lexer Reference](../reference/lexer.md)
- [Token Types Reference](../reference/types.md)

## Common Token Patterns

See [Real-World Scenarios](../examples/real-world-scenarios.md) for complete examples:
- Table of contents generation
- Heading ID generation
- Link validation
- Metadata extraction
- Token statistics
