A lightweight, robust, and zero-dependency library to convert ANSI escape codes into HTML. Now powered by a V2 State Machine Parser for full standard compliance.
- Standard ANSI Support: Handles generic foreground/background colors (0-15) and text formatting (bold, italic, underline, strikethrough, blink).
- Full Color Power: Supports 256-color (Xterm) and TrueColor (24-bit RGB) sequences.
- Robust Parsing: Handles nested styles, specific resets (e.g.,
Bold Off), and complex sequences perfectly. - Universal: Works in Browser, Node.js, and as a UMD module.
- Lightweight: Zero dependencies.
Drop ansiToHtml.js into your project:
<script src="ansiToHtml.js"></script>
<script>
const html = ansiToHtml("\x1b[31mHello World\x1b[0m");
document.getElementById('output').innerHTML = html;
</script>const ansiToHtml = require('./ansiToHtml.js');
const text = "\x1b[38;2;255;100;50mTrueColor Text\x1b[0m";
const html = ansiToHtml(text);
console.log(html);
// Output: <span style="color:rgb(255,100,50)">TrueColor Text</span>- Bold:
\x1b[1m - Italic:
\x1b[3m - Underline:
\x1b[4m - Blink:
\x1b[5m - Inverse:
\x1b[7m - Strikethrough:
\x1b[9m - Resets:
\x1b[0m(All),\x1b[22m(Bo/Dim),\x1b[23m(Italic),\x1b[24m(Underline), etc.
- Standard: 30-37 (FG), 40-47 (BG)
- Bright: 90-97 (FG), 100-107 (BG)
- 256-Color:
\x1b[38;5;n - TrueColor:
\x1b[38;2;r;g;b
You can customize the behavior by passing an options object:
const options = {
escapeXml: true, // Default: true (Prevents XSS)
colors: {
1: '#ff0000', // Override ANSI Red
}
};
const html = ansiToHtml("\x1b[31mRed\x1b[0m", options);MIT.
Original idea by botnick, upgraded to V2 standards. Happy coding! 🚀