Ansi to ascii or html

Hi all,

I receive data that has ansi color coding and graphics and would like to translate it to make it somewhat more readable. Any suggestions on how I can do that without having to write my own function to translate all the codes to html text and some sort of graphic representation?

How are you getting this data?

Using the mqtt-in node.

I should have been clearer.where does the data originate from?

Does it make a difference as to where it comes from? It comes from my bulletin board.

What you are seeing are ANSI terminal formatting codes. You would have to either make the originator of the msg stop sending them (normally telling it that it isn't using a color terminal should do the trick if it is well programmed), or by adding a node or two to strip them out in Node-RED. There might be an npm package that would do this - check on and if there is, you could use it with a function node.

Knowing the origin of data helps to understand the format its format. If you got it from a bulletin board, it was sent by something or someone. If you have control of that, you should format it in a more user friendly way. If you don't have control of it then you will have to format it yourself.

Is the format of the data always the same?

Not knowing what information you need from that data makes it hard to even begin to make suggestions.

Not at all, it could be anything that is received, nothing standard. This is why I was asking whether anyone knows a way to either strip or convert it without me having to do it manually. If I could have stripped it first, I would have and we wouldn't be having this conversation :slight_smile:

You might want to use google and search for how to strip out ANSI terminal formatting codes
Better yet use remove ansi color codes javascript in the search

There are quite a few libs on npm that you can import in a function node (setup tab, imports). There are libs to strip the colour codes, or even convert the garbled strings into colourful HTML.

Thanks Steve, I'm really not familiar with npm so this is useful. I'm not sure how to incorporate that into a module but I'm sure a Google search will help.

This site might help How to strip ansi codes from a string in JavaScript - QuickRef.ME

I don't want to appear rude or anything, but I do know how to strip characters. I have already said I don't want to have to do it manually. It looks like Steve has provided a viable answer.

Answer, what I wanted to do is try and preserve the output but in html to keep the overall look of the page, however yes, if it comes to the last resort I'll strip it.

This might help you...

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.