console-highlight icon indicating copy to clipboard operation
console-highlight copied to clipboard

Syntax highlighting in the console

console-highlight

Get syntax highlighting in the console.

Usage

var highlight = require('console-highlight');

console.log(highlight('javascript to highlight', {
  // optional options
  language: 'javascript', // will auto-detect if omitted
  theme: 'default' // a highlight.js theme
}));

Example output (using the 'monokai' theme):

Example - Monokai

Another example (using the 'solarized_dark' theme):

Example - Solarized Dark

How it works

This library uses highlight.js to do the heavy lifting. It then takes the resulting HTML and uses htmlout to convert it to styled console output.

Does this already exist?

Not that I'm aware of. The closest things I could find were ansi-highlight and cardinal, both of which only support JavaScript and appear to implement the lexing and highlighting logic all themselves (or using esprima, in cardinal's case).

By leveraging highlight.js for the actual highlighting logic, this library gains quite a lot: support for 71 languages and 44 styles. Even better, whatever improvements are made to highlight.js in the future will automatically benefit the console-highlight library.

Questions?

Please!