@hashicorp/ansi_up

中文文档
6.0.8发布于 2026/05/07MITTypeScriptES Module零依赖近 30 天下载 4.7万

Convert ansi sequences in strings to colorful HTML

原始使用文档
文档目录13 节
  1. Browser Example
  2. Node Example
  3. Typescript Example
  4. Installation
  5. Versions
  6. Quick Start
  7. API Methods and Recommended Settings
  8. Properties
  9. Buffering
  10. Example of a Use Case
  11. UTF8 note
  12. Building
  13. Running tests

ansi_up.js

ansi_up is an easy to use library that transforms text containing ANSI color escape codes into HTML.

This module is a single ES6 Javascript file with no dependencies. It is "isomorphic" javascript. This is just another way of saying that the ansi_up.js file will work in both the browser or node.js. The js library is compiled from TypeScript and its type description ships with the NPM. This code has been used in production since 2011 and is actively maintained.

For example, turn this terminal output:

ESC[1;Foreground
 30   30   30   30   30   30   30   30  
 31   31   31   31   31   31   31   31  
 32   32   32   32   32   32   32   32  
...

...into this browser output:

Browser Example


    <script type="module" type="text/javascript">

    var txt  = "\n\n\033[1;33;40m 33;40  \033[1;33;41m 33;41  \033[1;33;42m 33;42  \033[1;33;43m 33;43  \033[1;33;44m 33;44  \033[1;33;45m 33;45  \033[1;33;46m 33;46  \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

    import { AnsiUp } from './ansi_up.js'
    var ansi_up = new AnsiUp();

    var html = ansi_up.ansi_to_html(txt);

    var cdiv = document.getElementById("console");

    cdiv.innerHTML = html;

    </script>

Node Example

    import { AnsiUp } from './ansi_up.js'
    var ansi_up = new AnsiUp();

    var txt  = "\n\n\033[1;33;40m 33;40  \033[1;33;41m 33;41  \033[1;33;42m 33;42  \033[1;33;43m 33;43  \033[1;33;44m 33;44  \033[1;33;45m 33;45  \033[1;33;46m 33;46  \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

    var html = ansi_up.ansi_to_html(txt);

More examples are in the 'examples' directory in the repo.

Typescript Example

    import { AnsiUp } from './ansi_up.js'
    const ansi_up = new AnsiUp();

    const txt  = "\n\n\x1B[1;33;40m 33;40  \x1B[1;33;41m 33;41  \x1B[1;33;42m 33;42  \x1B[1;33;43m 33;43  \x1B[1;33;44m 33;44  \x1B[1;33;45m 33;45  \x1B[1;33;46m 33;46  \x1B[1m\x1B[0\n\n\x1B[1;33;42m >> Tests OK\n\n"

    let html = ansi_up.ansi_to_html(txt);

Installation

$ npm install ansi_up

Versions

  • Version 6.0 - Switch to ES6 module. Add faint styles. Style css configurable.
  • Version 5.1 - Add italic and underline styles (@DaoDaoNoCode)
  • Version 5.0 - Security fix for OSC URLs
  • Version 4.0 - Re-architect code to support terminal URL codes.
  • Version 3.0 - now treats ANSI bold sequences as CSS font-weight:bold
  • Version 2.0 - moved to a stateful, streaming version of the API
  • Version 1.3 - was the last of the older, deprecated API.

Quick Start

  1. Use whatever module system to import the ansi_up module.
  2. Instantiate the object.
  3. For every piece of ansi escaped text string, call ansi_to_html.
  4. Append the emitted HTML to the previous HTML already emitted.
  5. DONE

You only need the ansi_to_html method. The other properties listed below allow you to override some of the escaping behaviour. You probably don't need to change these from their default values.

It is recommended that the HTML container that holds the span tags is styled with a monospace font. A PRE tag would work just fine for this. It is also recommended that the HTML container is styled with a black background. See the examples, for more CSS theming.

ansi_to_html (txt)

This transforms ANSI terminal escape codes/sequences into SPAN tags that wrap and style the content.

This method only interprets ANSI SGR (Select Graphic Rendition) codes or escaped URL codes. For example, cursor movement codes are ignored and hidden from output.

This method also safely escapes any unsafe HTML characters.

The default style uses colors that are very close to the prescribed standard. The standard assumes that the text will have a black background. These colors are set as inline styles on the SPAN tags. Another option is to set the 'use_classes' property to true'. This will instead set classes on the spans so the colors can be set via CSS. The class names used are of the format ansi-*-fg/bg and ansi-bright-*-fg/bg where * is the colour name, i.e black/red/green/yellow/blue/magenta/cyan/white. For xterm 256 colors, indexes 16..255 use ansi-xterm-<index>-fg/bg classes. When truecolor (38;2 / 48;2) is used with use_classes = true, ansi_up maps the RGB value to the nearest xterm index and emits the corresponding class. See the examples directory for a complete CSS theme for these classes.

Properties

escape_html

(default: true)

By default, HTML's reserved characters & < > " ' are replaced with HTML entities to make them appear as literal characters in your application, rather than being interpreted as HTML structure. If you prefer keeping HTML's reserved characters untouched, you can set this to false.

use_classes

(default: false)

This causes the SPAN tags to use classes to style the SPAN tags instead of specified RGB values.

Class mode details:

  • xterm indexes 0..15 resolve to existing ansi-* / ansi-bright-* classes.
  • xterm indexes 16..255 resolve to ansi-xterm-<index>-fg/bg classes.
  • truecolor (38;2 / 48;2) resolves to the nearest xterm class when use_classes is true.
  • truecolor remains inline rgb(...) styles when use_classes is false.

url_allowlist

(default: { 'http':1, 'https':1 })

This mapping is an 'allow' list of URI schemes that will be allowed to render HTML anchor tags.

boldStyle

(default: 'font-weight:bold')

faintStyle

(default: 'opacity:0.7')

italicStyle

(default: 'font-style:italic')

underlineStyle

(default: 'text-decoration:underline')

Buffering

In general, the ansi_to_html should emit HTML output when invoked with a non-empty string. The only exceptions are an incomplete ESC sequence or an incomplete OSC URL sequence. For those cases, the library will buffer (not emit output), until it receives input that completes those sequences.

Example of a Use Case

I have used this library to 'tail' a file.

On a remote machine, I had process generating a log file. I had a web server running on the same machine. The server hosted a simple HTML page that used AJAX to poll an object with a range query. Specifically I used an HTTP/1.1 GET request with RFC 7233 Range query. The first range query would start at 0, but then progressively move forward after new data was received.

For each new chunk of data received, I would transform the data with ansi_up, and append the new spans to the innerHTML of a PRE tag.

UTF8 note

One last important note, ansi_up takes its input in the form of a Javascript string. These strings are UTF8. When you take the output of some program and send it to Javascript, there will be buffering. Be sure that you do not send incomplete UTF8 sequences. Javascript will ignore or drop the sequence from the stream when it converts it to a string.

Building

To build, a simple Makefile handles it all.

    $ make

Running tests

To run the tests for ansi_up, run npm install to install dev dependencies. Then:

    $ make test

版本与发布记录

@hashicorp/ansi_up 共发布 2 个版本,首次发布于 2026/05/07;过去 12 个月发布了 2 个版本。

版本发布日期距今
6.0.8latest2026/05/076 个月前
6.0.72026/05/076 个月前

关于 @hashicorp/ansi_up 的常见问题

@hashicorp/ansi_up 是什么?

Convert ansi sequences in strings to colorful HTML

如何安装 @hashicorp/ansi_up?

在项目目录运行 npm install @hashicorp/ansi_up,或使用 pnpm add / yarn add / bun add。

@hashicorp/ansi_up 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@hashicorp/ansi_up 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@hashicorp/ansi_up 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js *。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@hashicorp/ansi_up 可以商用吗?

@hashicorp/ansi_up 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@hashicorp/ansi_up 的下载量有多少?

根据 npm 官方下载统计,@hashicorp/ansi_up 在 2026/10/18 – 2026/11/16 这 30 天内被下载 46,675 次,日均约 1,556 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@hashicorp/ansi_up 还在维护吗?

最新版本 6.0.8 发布于 2026/05/07(6 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/05/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。