Skip to content

ts-syntax-highlighterBlazing-Fast Syntax Highlighting

TypeScript-native syntax highlighter with comprehensive grammar support for modern web languages. Highly optimized for performance.

Quick Example โ€‹

typescript
import { Tokenizer } from 'ts-syntax-highlighter'

// Create tokenizer
const tokenizer = new Tokenizer('javascript')

// Tokenize code (async - faster)
const tokens = await tokenizer.tokenizeAsync(`
const greeting = 'Hello World'
console.log(greeting)
`)

// Process tokens
tokens.forEach(line => {
  line.tokens.forEach(token => {
    console.log(`${token.type}: "${token.content}"`)
  })
})

Performance โ€‹

ts-syntax-highlighter is optimized for speed with efficient tokenization:

Our Performance โ€‹

OperationFast Mode (Async)Sync Mode
JavaScript0.05ms0.08ms
TypeScript0.08ms0.12ms
HTML0.04ms0.06ms
CSS0.03ms0.05ms

Compared to Alternatives โ€‹

LibraryJavaScriptTypeScriptHTMLCSS
ts-syntax-highlighter0.05ms0.08ms0.04ms0.03ms
highlight.js3.8ms1.0ms1.2ms0.9ms
Prism.js2.1ms0.6ms0.8ms0.5ms

Fast enough for real-time syntax highlighting as you type. View detailed benchmarks โ†’

Why ts-syntax-highlighter? โ€‹

Built for Modern Development โ€‹

Support for the latest language features out-of-the-box:

  • ES2024+: BigInt, numeric separators, optional chaining
  • TypeScript 5.x: Type operators, utility types, decorators
  • JSX/TSX: Complete React and TypeScript JSX support
  • CSS4: Modern color functions, container queries, layers

Performance That Matters โ€‹

Real-time syntax highlighting requires speed:

  • Sub-millisecond tokenization for most operations
  • Non-blocking async mode for large files
  • Minimal memory footprint (~3x source size)
  • Scales to millions of lines efficiently

Developer Experience โ€‹

TypeScript-first design for the best DX:

  • Fully typed APIs with IntelliSense support
  • Comprehensive documentation and examples
  • Simple and intuitive API
  • Flexible configuration options

Supported Languages โ€‹

JavaScript/JSX

ES2024+, React JSX, BigInt, regex flags

TypeScript/TSX

Types, generics, TSX, type operators

HTML

HTML5, ARIA, data attributes, entities

CSS

CSS4 colors, math, containers, layers

JSON

Objects, arrays, proper escapes

STX

Blade-like templating, 50+ directives

Explore all language features โ†’

Get Started โ€‹

bash
# Install
npm install ts-syntax-highlighter

# Or with Bun
bun add ts-syntax-highlighter

Read the full documentation โ†’

Meet The Team

Chris Breuer's avatar
Chris Breuer
Open sourceror.
Core Stacks team.
Working at Stacks.js
Glenn's avatar
Glenn
Open sourceror.
Core Stacks team.
Working at Stacks.js
Mike's avatar
Mike
Open sourceror.
Core Stacks team.
Working at Stacks.js
Zoltan's avatar
Zoltan
Open sourceror.
Core Stacks team.

Stacks Sponsors

Click here to become a sponsor.

Contributors

chrisbbreuer's avatarcab-mikee's avatar

Released under the MIT License.