rburns / ansi-to-html

Convert ansi escaped text streams to html.
MIT License
355 stars 48 forks source link

Support for code 22 #30

Closed mlucool closed 7 years ago

mlucool commented 7 years ago

Hi,

Please add support for escape code 22.

Per Wikipedia: https://en.wikipedia.org/wiki/ANSI_escape_code 22 Normal color or intensity Neither bold nor faint

Example:

const ANSIToHTML = require('ansi-to-html');
const text = '\x1b[1mHello\x1b[22m World';
console.log(text);
console.log(new ANSIToHTML({fg: '#000', bg: '#FFF', newline: true}).toHtml(text));
// "\u001b[1mHello\u001b[22m World"
// "<b>Hello World</b>"

Link: https://runkit.com/585164c7c92fa7001463dd8b/585167e71ca9e00014bc2cff

The </b> is around the whole expression instead of correctly around just Hello.

This comes up because chalk uses this (and it works just fine with node cli):

const chalk = require("chalk")
console.log(chalk.styles.bold);
console.log(chalk.bold('Test') + ' me')
mlucool commented 7 years ago

This also applies to: 23 Not italic, not Fraktur 24 Underline: None Not singly or doubly underlined

rburns commented 7 years ago

It would be nice to have. And possibly a little tricky to implement. I would think you'd need to at least account for the two scenarios here: https://github.com/rburns/ansi-to-html/commit/1fc513cf1ae170094c0635d1d5bd23d30f786776. But, you can probably imagine other scenarios where mismatched opening/closing tags could arise.

Having said that, 24 Underline is already implemented. And doesn't appear to take into account those edge cases. https://github.com/rburns/ansi-to-html/blob/unstyle/test/ansi_to_html.coffee#L118-L121 https://github.com/rburns/ansi-to-html/blob/unstyle/src/ansi_to_html.coffee#L129 So, maybe it's not a practical problem.

Not sure when I'll find the time to look at this in more detail. Could be soon. Patches welcome, in any case.

rburns commented 7 years ago

well, disabling underline actually has more protections against those edge cases than I thought. And I implemented disabling bold. https://github.com/rburns/ansi-to-html/compare/unstyle

it appears that italic (3) is currently implemented to render as underlined text https://github.com/rburns/ansi-to-html/blob/unstyle/src/ansi_to_html.coffee#L124. Should fix that before implementing 23 Not italic.

mlucool commented 7 years ago

:+1: Thanks for looking into this so quickly