🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

@stacksjs/ts-md

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stacksjs/ts-md

A fast, native Bun-powered markdown parser with frontmatter support. Replaces gray-matter, marked, and yaml.

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
2
Created
Source

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

bun add @stacksjs/ts-md

Usage

Basic Markdown Parsing

import { parseMarkdown } from '@stacksjs/ts-md'

const html = parseMarkdown('# Hello **world**')
// <h1 id="hello-world">Hello <strong>world</strong></h1>

With Options

const html = parseMarkdown(markdown, {
  gfm: true,              // GitHub Flavored Markdown (default: true)
  breaks: false,          // Convert \n to <br> (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

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

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-mdmarkdown-itmarkedshowdown
Small (< 1KB)324B ops/sec112B ops/sec26B ops/sec14B ops/sec
Medium (~3KB)34.7B ops/sec17.7B ops/sec2.8B ops/sec2.8B ops/sec
Large (~50KB)1.81B ops/sec1.25B ops/sec16M ops/sec135M 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

Keywords

markdown

FAQs

Package last updated on 09 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts