Terminal Syntax Highlighter

2.1.11 · maintenance · verified Wed Apr 22

cli-highlight is a utility for applying syntax highlighting to code and text within a terminal environment, leveraging the capabilities of highlight.js for language detection and tokenization. It provides both a command-line interface for piping input or reading files and a programmatic API for integrating highlighting into Node.js applications. The package is currently at version 2.1.11, with its last update in March 2021, suggesting a maintenance rather than active feature development cadence. Key differentiators include its robust language support inherited from highlight.js, the ability to define custom themes using Chalk styles for fine-grained control over terminal colors, and its seamless integration with other CLI tools through standard input/output piping. It is particularly useful for debugging, logging, or displaying code snippets in a more readable format directly in the console.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a basic HTTP server that logs a TypeScript code snippet with syntax highlighting to the console when '/code' is accessed.

import { highlight } from 'cli-highlight';
import { createServer } from 'http';

const server = createServer((req, res) => {
  if (req.url === '/code' && req.method === 'GET') {
    const codeExample = `function greet(name: string) {
  console.log('Hello, ' + name + '!');
}

greet('World');
`;
    console.log(highlight(codeExample, { language: 'typescript', ignoreIllegals: true }));
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Code highlighted in console.');
  } else {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Visit /code to see an example of highlighted code.');
  }
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
  console.log('Make a GET request to http://localhost:3000/code to see highlighted output in your terminal.');
});

view raw JSON →