
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
ts-syntax-highlighter
Advanced tools
A performant and production-ready syntax highlighter with 48 languages, zero dependencies, and 100% test coverage. Built with TypeScript for speed and reliability.
bun add ts-syntax-highlighter
Compared to Shiki and Torchlight:
See FEATURES.md for detailed comparison.
import { createHighlighter } from 'ts-syntax-highlighter'
// Create a highlighter instance
const highlighter = await createHighlighter({
theme: 'github-dark',
cache: true,
})
// Highlight code
const result = await highlighter.highlight(
'const greeting = "Hello World";',
'javascript',
{
lineNumbers: true,
}
)
console.log(result.html) // Highlighted HTML
console.log(result.css) // Theme CSS
import { highlight } from 'ts-syntax-highlighter'
const result = await highlight(
'function add(a: number, b: number) { return a + b; }',
'typescript'
)
# Highlight a file
syntax highlight code.ts typescript --theme github-dark --output highlighted.html
# With line numbers
syntax highlight app.js javascript --line-numbers
# List supported languages
syntax languages
# List available themes
syntax themes
# Get language info
syntax info typescript
# Get theme info
syntax theme-info nord
Create a syntax.config.ts file in your project root:
import type { SyntaxHighlighterConfig } from 'ts-syntax-highlighter'
export default {
verbose: false,
theme: 'github-dark',
defaultLanguage: 'javascript',
cache: true,
plugins: [],
} satisfies SyntaxHighlighterConfig
import type { Theme } from 'ts-syntax-highlighter'
import { createHighlighter } from 'ts-syntax-highlighter'
const customTheme: Theme = {
name: 'My Theme',
type: 'dark',
colors: {
'editor.background': '#1e1e1e',
'editor.foreground': '#d4d4d4',
},
tokenColors: [
{
scope: ['comment'],
settings: {
foreground: '#6A9955',
fontStyle: 'italic',
},
},
// ... more token colors
],
}
const highlighter = await createHighlighter()
await highlighter.loadTheme(customTheme)
import type { Language } from 'ts-syntax-highlighter'
const customLanguage: Language = {
id: 'mylang',
name: 'My Language',
aliases: ['ml'],
extensions: ['.ml'],
grammar: {
name: 'My Language',
scopeName: 'source.mylang',
patterns: [
{
name: 'keyword.mylang',
match: '\\b(if|else|while)\\b',
},
// ... more patterns
],
},
}
const highlighter = await createHighlighter()
await highlighter.loadLanguage(customLanguage)
Create custom plugins to extend functionality:
import type { TokenLine } from 'ts-syntax-highlighter'
import { createHighlighter, createTransformerPlugin } from 'ts-syntax-highlighter'
const myPlugin = createTransformerPlugin('my-plugin', {
name: 'my-transformer',
transform: (tokens: TokenLine[]) => {
// Transform tokens here
return tokens
},
})
const highlighter = await createHighlighter({
plugins: [myPlugin],
})
const result = await highlighter.highlight(code, 'javascript', {
lineNumbers: true,
addedLines: [2, 5], // Show with + indicator
removedLines: [3], // Show with - indicator
annotations: [
{ line: 2, text: 'Fixed bug', type: 'success' },
],
})
const result = await highlighter.highlight(code, 'javascript', {
focusLines: [3, 4, 5], // Keep these lines clear
dimLines: [1, 2, 6, 7], // Dim these lines
})
import { renderDualTheme } from 'ts-syntax-highlighter'
const result = renderDualTheme(tokens, {
lightTheme: githubLight,
darkTheme: githubDark,
lineNumbers: true,
})
// Automatically switches with .dark class or prefers-color-scheme
const result = await highlighter.highlight(code, 'javascript')
// Use colored output in terminal
console.log(result.ansi)
const result = await highlighter.highlight(code, 'javascript', {
lineNumbers: true,
highlightLines: [1, 3, 5], // Highlight lines 1, 3, and 5
})
const result = await highlighter.highlight(code, 'javascript', {
inline: true, // Render as inline code snippet
})
createHighlighter(config?)Creates a new highlighter instance.
Parameters:
config - Optional configuration objectReturns: Promise<Highlighter>
highlight(code, lang, options?)Quick highlight function for simple use cases.
Parameters:
code - The code to highlightlang - The language identifieroptions - Optional render optionsReturns: Promise<RenderedCode>
highlight(code, lang, options?) - Highlight codeloadLanguage(language) - Load a custom languageloadTheme(theme) - Load a custom themegetSupportedLanguages() - Get list of supported languagesgetSupportedThemes() - Get list of available themesclearCache() - Clear the token cachegetCacheSize() - Get current cache sizeThe highlighter includes several performance optimizations:
const highlighter = await createHighlighter({ cache: true })
console.time('First highlight')
await highlighter.highlight(largeCodeFile, 'typescript')
console.timeEnd('First highlight')
console.time('Cached highlight')
await highlighter.highlight(largeCodeFile, 'typescript')
console.timeEnd('Cached highlight')
import { createHighlighter } from 'ts-syntax-highlighter'
const highlighter = await createHighlighter({
theme: 'github-dark',
})
const code = `
function fibonacci(n: number): number {
if (n <= 1) return n
return fibonacci(n - 1) + fibonacci(n - 2)
}
`
const result = await highlighter.highlight(code, 'typescript', {
lineNumbers: true,
})
// Use result.html and result.css in your HTML
const stxCode = `
@extends('layouts.app')
@section('content')
<div class="container">
@if (user.isAuthenticated)
<h1>Welcome, {{ user.name }}!</h1>
@else
<a href="/login">Please log in</a>
@endif
</div>
@endsection
`
const result = await highlighter.highlight(stxCode, 'stx')
const highlighter = await createHighlighter()
// Light theme for documentation
const lightResult = await highlighter.highlight(code, 'javascript', {
theme: 'github-light',
})
// Dark theme for code blocks
const darkResult = await highlighter.highlight(code, 'javascript', {
theme: 'github-dark',
})
// Nord theme for special sections
const nordResult = await highlighter.highlight(code, 'javascript', {
theme: 'nord',
})
Contributions are welcome! Please see CONTRIBUTING for details.
MIT License - see LICENSE for details.
Inspired by:
FAQs
Performant & minimal syntax highlighter.
We found that ts-syntax-highlighter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.