Terlio.js includes a small zero-dependency lexical highlighter for common source files. It recognizes comments, strings, numbers, keywords, types, built-ins, constants, properties, annotations, operators and preprocessor lines. It is intentionally lightweight: it does not build an AST and it does not attempt semantic analysis.
Highlighting is opt-in, so existing code and transcript rendering remains unchanged until it is enabled.
The built-in language set includes:
- JavaScript and JSX
- TypeScript and TSX
- Python
- C and
.hheaders - C++ and
.hpp/.hh/.hxxheaders - Objective-C and Objective-C++ (
.mand.mm) - Swift and Metal source (
.swiftand.metal) - shell scripts (
sh, Bash, Zsh and Fish) - JSON and JSON with comments
- XML, HTML, SVG, property lists, entitlements, storyboards and XIB files
- CSS, SCSS and Less
- Java
- Go
- Rust
Language aliases, file extensions and common shebangs are normalized automatically. An explicit language always takes precedence over filename detection.
Use SyntaxText when the highlighted source is part of a component tree:
import { SyntaxText, themes } from 'terlio.js';
const source = SyntaxText({
code: 'const answer = 42;',
language: 'javascript',
theme: themes.ocean,
});Use highlightSyntax() or highlightSyntaxLines() when you need ANSI text directly:
import { highlightSyntaxLines, themes } from 'terlio.js';
const lines = highlightSyntaxLines(sourceCode, {
filename: 'Sources/App.swift',
theme: themes.ocean,
});Unknown languages safely return the original text without styling.
Enable highlighting for one block:
import { createBlock, renderBlockLines, themes } from 'terlio.js';
const block = createBlock({
type: 'code',
filename: 'server.js',
syntaxHighlight: true,
content: 'export const port = 3000;',
});
const lines = renderBlockLines({
block,
width: 72,
theme: themes.ocean,
});Enable it for every code block in one render call:
const lines = renderBlocksLines({
blocks,
width: 72,
theme: themes.ocean,
syntaxHighlight: true,
});A block-level syntaxHighlight: false overrides a globally enabled renderer. filename is used for both detection and the default code-block title when no explicit title is supplied.
Enable highlighting in the complete chat runtime:
const app = new RichTerminalApp({
syntaxHighlight: true,
});Or pass the option to the reusable chat screen:
const screen = ChatScreen({
messages,
syntaxHighlight: true,
columns: 100,
rows: 30,
});The highlighter uses semantic theme fields. Every bundled theme supplies fallbacks, and custom themes may override any of these fields:
const customTheme = {
...themes.ocean,
syntaxKeyword: '\x1b[38;5;207m',
syntaxString: '\x1b[38;5;114m',
syntaxComment: '\x1b[38;5;244m',
};Available fields are:
syntaxTextsyntaxCommentsyntaxStringsyntaxNumbersyntaxKeywordsyntaxTypesyntaxBuiltinsyntaxConstantsyntaxPropertysyntaxPreprocessorsyntaxAnnotationsyntaxOperatorsyntaxPunctuation
Run the packaged snapshot:
npx terlio.js example:syntaxFrom a development checkout:
npm run example:syntax