I'm trying to process a terminal output. Let's say it's the string "\x1b[01;32mREADME.md". It has a double backslash. If I run console.log("\\x1b[01;32mREADME.md"), it only shows a single backslash, but if I pass it to the toHtml from ansi-to-html, it doesn't work. And by work, I mean transforming the characters into html.
I tried several solutions involving replace or the fact that \ is U+5c in unicode but nothing worked. The options on the library don't help either.
import Convert from "ansi-to-html";
export default function App() {
const c = new Convert({ escapeXML: true });
const single = "\x1b[01;32mREADME.md";
const double = "\\x1b[01;32mREADME.md";
console.log(double) // prints single backslash
const res1 = c.toHtml(single);
const res2 = c.toHtml(double);
return (
<div className="App">
single backslash works:
<pre>{single}</pre>
<pre>{res1}</pre>
<br />
<br />
double backslash doesnt work:
<pre>{double}</pre>
<pre>{res2}</pre>
</div>
);
}
Any help is appreciated.
You can use String.raw function:
const double = String.raw`\\x1b[01;32mREADME.md`;
With this you dont need to use convert:
export default function App() {
const single = String.raw`\x1b[01;32mREADME.md`;
const double = String.raw`\\x1b[01;32mREADME.md`;
const res1 = single;
const res2 = double;
return (
<div className="App">
single backslash works:
<pre>{single}</pre>
<pre>{res1}</pre>
<br />
<br />
double backslash doesnt work:
<pre>{double}</pre>
<pre>{res2}</pre>
</div>
);
}