Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ansi-to-html-converter V2

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.

✨ Features

  • 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.

🚀 Getting Started

Browser

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>

Node.js

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>

🎨 Supported Codes

Modifiers

  • 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.

Colors

  • 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

⚙️ Configuration

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);

📜 License

MIT.

Original idea by botnick, upgraded to V2 standards. Happy coding! 🚀

About

A lightweight utility to convert ANSI escape sequences to HTML with corresponding styles. Easily handle text formatting and colors in the browser, perfect for logs and terminal output.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages