What is ansi-html?
The ansi-html npm package is a utility for converting ANSI escape codes (often used for colorizing text in terminal/console output) into HTML elements. This can be particularly useful when you want to display styled console output on a web page.
What are ansi-html's main functionalities?
Convert ANSI escape codes to HTML
This feature allows you to convert text containing ANSI escape codes for color and style into equivalent HTML with inline styles. The example code demonstrates how to convert a string with ANSI codes for red text into an HTML span element with the color set to red.
const ansiHTML = require('ansi-html');
const text = '\u001b[31mHello World\u001b[39m';
const html = ansiHTML(text);
console.log(html); // '<span style="color:#FF0000;">Hello World</span>'
Other packages similar to ansi-html
chalk
Chalk is a popular npm package that allows you to style terminal text with chainable ANSI escape codes. Unlike ansi-html, which is focused on converting ANSI codes to HTML, Chalk is used to create styled terminal text directly in Node.js applications.
ansi-to-html
ansi-to-html is another npm package that converts ANSI escape codes into HTML. It provides a similar functionality to ansi-html but offers more customization options, such as setting up different color themes and handling newlines.
colors
Colors is an npm package that adds color support to strings in the terminal. It is similar to ansi-html in that it deals with ANSI codes for colorizing text, but it does not convert them to HTML. Instead, it enhances strings to display colored output in the terminal.
ansi-html
An elegant lib that converts the chalked (ANSI) text to HTML.
Coverage
- All styles of chalk (100%) and colors.
- There are over 150 randomized test cases under
test
.
Installation
$ npm install ansi-html
Usage
var ansiHTML = require('ansi-html');
var str = ansiHTML('[ANSI_TEXT]');
e.g.:
var chalk = require('chalk');
var str = chalk.bold.red('foo') + ' bar';
console.log('[ANSI]', str)
console.log('[HTML]', ansiHTML(str));
See complete examples under test
/ examples
directory.
Set Colors
ansiHTML.setColors({
reset: ['555', '666'],
black: 'aaa',
red: 'bbb',
green: 'ccc',
yellow: 'ddd',
blue: 'eee',
magenta: 'fff',
cyan: '999',
lightgrey: '888',
darkgrey: '777'
});
Reset
ansiHTML.reset();
Exposed Tags
var openTags = ansiHTML.tags.open;
var closeTags = ansiHTML.tags.close;
Test
$ npm install -l
$ npm test