@steve02081504/ansi2html
v0.0.0
Published
Convert ANSI colored terminal text into HTML.
Readme
@steve02081504/ansi2html
Convert ANSI terminal output into renderable HTML.
Used By
Install
npm i @steve02081504/ansi2htmlAPI
This package exports one function: ansiToHtml(text).
import { ansiToHtml } from '@steve02081504/ansi2html'
const input = '\x1b[31mred\x1b[0m normal'
const html = ansiToHtml(input)
// => <span style="color:rgb(187,0,0)">red</span> normalFeatures
- Supports common SGR styles (color, bold, italic, underline, strikethrough)
- Supports ANSI 16-color, 256-color, and 24-bit truecolor
- Supports OSC 8 hyperlinks and outputs
<a href="...">...</a> - Escapes HTML automatically for safer rendering
- Preserves spaces and newlines (
and<br/>)
CLI
After installation, use the ansi2html command directly.
ansi2html "\x1b[32mhello\x1b[0m"Pipe input is also supported:
echo "\x1b[31mred\x1b[0m line" | ansi2htmlBrowser (esm.sh)
You can use this package directly in browsers via esm.sh:
<script type="module">
import { ansiToHtml } from 'https://esm.sh/@steve02081504/ansi2html'
const ansi = '\x1b[36mHello from browser\x1b[0m'
const html = ansiToHtml(ansi)
document.getElementById('out').innerHTML = html
</script>
<div id="out"></div>REPL
This repository includes a REPL for previewing conversion output:
npm run replEnter a string or expression in the prompt to print converted HTML.
Development
npm test