https://github.com/stacksjs/ts-md
Extremely performant markdown parser & compiler.
https://github.com/stacksjs/ts-md
compiler markdown markdown-it marked parser
Last synced: 9 days ago
JSON representation
Extremely performant markdown parser & compiler.
- Host: GitHub
- URL: https://github.com/stacksjs/ts-md
- Owner: stacksjs
- License: mit
- Created: 2025-11-09T04:30:09.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2026-06-04T15:09:04.000Z (20 days ago)
- Last Synced: 2026-06-04T17:07:43.727Z (20 days ago)
- Topics: compiler, markdown, markdown-it, marked, parser
- Language: TypeScript
- Homepage:
- Size: 468 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# ts-md
High-performance markdown parser and sanitizer built for Bun.
## Features
- GitHub Flavored Markdown (GFM) support
- Tables, task lists, strikethrough
- Header ID generation
- Syntax highlighting support
- HTML sanitization
- Frontmatter parsing (YAML, TOML, JSON)
## Installation
```bash
bun add @stacksjs/ts-md
```
## Usage
### Basic Markdown Parsing
```typescript
import { parseMarkdown } from '@stacksjs/ts-md'
const html = parseMarkdown('# Hello **world**')
//
Hello world
```
### With Options
```typescript
const html = parseMarkdown(markdown, {
gfm: true, // GitHub Flavored Markdown (default: true)
breaks: false, // Convert \n to
(default: false)
headerIds: true, // Generate header IDs (default: true)
headerPrefix: '', // Prefix for header IDs (default: '')
sanitize: false, // Sanitize HTML output (default: false)
highlight: (code, lang) => {
// Custom syntax highlighting
return highlightedCode
}
})
```
### HTML Sanitization
```typescript
import { sanitizeHtml } from '@stacksjs/ts-md'
const clean = sanitizeHtml(userInput, {
allowedTags: ['p', 'strong', 'em', 'a', 'code'],
allowedAttributes: {
a: ['href', 'title']
},
allowedSchemes: ['http', 'https', 'mailto']
})
```
### Frontmatter Parsing
```typescript
import { parseFrontmatter } from '@stacksjs/ts-md'
const content = `---
title: My Post
date: 2024-01-01
---
# Content here`
const { data, content: markdown } = parseFrontmatter(content)
console.log(data.title) // 'My Post'
```
## Performance
Benchmark results against popular markdown parsers:
| Document Size | @stacksjs/ts-md | markdown-it | marked | showdown |
|--------------|-------------------|-------------|---------|----------|
| Small (< 1KB) | 324B ops/sec | 112B ops/sec | 26B ops/sec | 14B ops/sec |
| Medium (~3KB) | 34.7B ops/sec | 17.7B ops/sec | 2.8B ops/sec | 2.8B ops/sec |
| Large (~50KB) | 1.81B ops/sec | 1.25B ops/sec | 16M ops/sec | 135M ops/sec |
**Performance vs markdown-it:**
- Small documents: 2.89x faster
- Medium documents: 1.96x faster
- Large documents: 1.45x faster
The parser uses a flat token stream architecture with position-based parsing for optimal performance.
## Architecture
The markdown parser is built with several key optimizations:
- **Flat token stream**: Avoids nested object allocations for better cache locality
- **Position-based parsing**: Minimizes string allocations with substring operations
- **Optimized escapeHtml**: Fast-path for strings without special characters
- **Direct inline matching**: Efficient emphasis and link parsing
- **Recursive nested parsing**: Proper support for nested inline elements
## API
### `parseMarkdown(markdown: string, options?: MarkdownOptions): string`
Parse markdown to HTML.
### `sanitizeHtml(html: string, options?: SanitizeOptions): string`
Sanitize HTML to prevent XSS attacks.
### `parseFrontmatter(content: string): { data: any, content: string }`
Extract and parse frontmatter from markdown content. Supports YAML, TOML, and JSON formats.
## License
MIT