百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

fancy-ansi

> 前端框架
开源

用于将 ANSI 转换为 HTML 的小型 JavaScript 库

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于将 ANSI 转换为 HTML 的小型 JavaScript 库

Fancy-ANSI

Fancy-ANSI is a small JavaScript library for converting ANSI to beautiful, browser-safe HTML

Demo: https://www.kubetail.com/demo
Preview: https://kubetail-org.github.io/fancy-ansi/

Introduction

While adding ANSI markup support to kubetail we tested out several popular popular ANSI-to-html conversion libraries (e.g. ansi-html-community, ansi-to-html, ansi_up) and ran into a few problems:

  • Failure to parse some of our users' ANSI markup
  • Use of hard-coded styles that made customization more difficult
  • Lack of support for CSS variables

To solve these problems and make something that integrated nicely into our frontend stack (Tailwind, React) we created Fancy-ANSI. The library is designed to be small (~4 kb gzipped), performant, easy-to-use and safe from XSS attacks. It has the following features:

  • Supports easy customization using CSS variables
  • Supports almost all SGR codes
  • Includes a Tailwind plugin that enables support for easy theming
  • Includes a React component for easy use in a React environment
  • Includes useful utilities like hasAnsi() and stripAnsi() that come in handy when working with ANSI
  • Includes popular color palettes that can be swapped in easily

Try it out and let us know what you think! If you notice any bugs or have any feature requests just create a GitHub Issue.

Quickstart

Install the library using your favorite package manager:

bash
# npm
npm install fancy-ansi

# yarn
yarn add fancy-ansi

# pnpm
pnpm add fancy-ansi

Now you can use it in your code with React:

javascript
// ExampleComponent.jsx
import { AnsiHtml } from 'fancy-ansi/react';

export const ExampleComponent = () => {
  const text = '\x1b[34mhello \x1b[33mworld\x1b[0m';
  return ;
};

Or with Vanilla-JS:

typescript
// example.ts
import { FancyAnsi } from 'fancy-ansi';

const fancyAnsi = new FancyAnsi();

export function addElementWithAnsi() {
  const el = document.createElement('div');
  el.innerHTML = fancyAnsi.toHtml('\x1b[34mhello \x1b[33mworld\x1b[0m');
  document.body.append(el);
}

The HTML rendered is browser-safe and ready with some sensible color choices that can be customized easily (see below).

Configuration

You can configure Fancy-ANSI using CSS variables. For example, to invert blacks in dark mode you can change the value of black when the document has a "dark" class:

css
:root {
  --ansi-black: #000;
}

.dark {
  --ansi-black: #FFF;
}

The full list of supported variables can be found in the SGR Parameters section below.

SGR Parameters

n Name Supported CSS Variables Default
0 Reset :heavy_check_mark:
1 Bold :heavy_check_mark: --ansi-bold-font-weight 600
2 Dim :heavy_check_mark: --ansi-dim-opacity 0.7
3 Italic :heavy_check_mark:
4 Underline :heavy_check_mark:
5 Slow blink
6 Fast blink
7 Invert
8 Hide :heavy_check_mark:
9 Strikethrough :heavy_check_mark:
10 Default font :heavy_check_mark:
11 Alternative font 1 :heavy_check_mark: --ansi-font-1
12 Alternative font 2 :heavy_check_mark: --ansi-font-2
13 Alternative font 3 :heavy_check_mark: --ansi-font-3
14 Alternative font 4 :heavy_check_mark: --ansi-font-4
15 Alternative font 5 :heavy_check_mark: --ansi-font-5
16 Alternative font 6 :heavy_check_mark: --ansi-font-6
17 Alternative font 7 :heavy_check_mark: --ansi-font-7
18 Alternative font 8 :heavy_check_mark: --ansi-font-8
19 Alternative font 9 :heavy_check_mark: --ansi-font-9
20 Gothic
21 Double underline :heavy_check_mark:
22 Bold off :heavy_check_mark:
23 Italic off :heavy_check_mark:
24 Underline off :heavy_check_mark:
25 Blink off
26 Proportional spacing
27 Invert off
28 Hidden off :heavy_check_mark:
29 Strikethrough off :heavy_check_mark:
30 Foreground color - black :heavy_check_mark: --ansi-black #2e3436
31 Foreground color - red :heavy_check_mark: --ansi-red #cc0000
32 Foreground color - green :heavy_check_mark: --ansi-green #4e9a06
33 Foreground color - yellow :heavy_check_mark: --ansi-yellow #c4a000
34 Foreground color - blue :heavy_check_mark: --ansi-blue #3465a4
35 Foregorund color - magenta :heavy_check_mark: --ansi-magenta #75507b
36 Foreground color - cyan :heavy_check_mark: --ansi-cyan #06989a
37 Foreground color - white :heavy_check_mark: --ansi-white #d3d7cf
38 Foreground color - extended (see below) :heavy_check_mark:
39 Default foreground color :heavy_check_mark:
40 Background color - black :heavy_check_mark: --ansi-black #2e3436
41 Background color - red :heavy_check_mark: --ansi-red #cc0000
42 Background color - green :heavy_check_mark: --ansi-green #4e9a06
43 Background color - yellow :heavy_check_mark: --ansi-yellow #c4a000
44 Background color - blue :heavy_check_mark: --ansi-blue #3465a4
45 Background color - magenta :heavy_check_mark: --ansi-magenta #75507b
46 Background color - cyan :heavy_check_mark: --ansi-cyan #06989a
47 Background color - white :heavy_check_mark: --ansi-white #d3d7cf
48 Background color - extended (see below) :heavy_check_mark:
49 Default background color :heavy_check_mark:
50 Proportional spacing off
51 Frame :heavy_check_mark: --ansi-frame-outline 1px solid
52 Encircle
53 Overline :heavy_check_mark:
54 Frame/encircle off :heavy_check_mark:
55 Overline off :heavy_check_mark:
58 Underground color - extended (see below) :heavy_check_mark:
59 Default underline color :heavy_check_mark:
60 Right side line
61 Double line on the right side
62 Left side line
63 Double line on the left side
64 Ideogram stress marking
65 Side lines off
73 Superscript :heavy_check_mark: --ansi-superscript-font-size 80%
74 Subscript :heavy_check_mark: --ansi-subscript-font-size 80%
75 Superscript/subscript off :heavy_check_mark:
90 Foreground color - bright black :heavy_check_mark: --ansi-bright-black #555753
91 Foreground color - bright red :heavy_check_mark: --ansi-bright-red #ef2929
92 Foreground color - bright green :heavy_check_mark: --ansi-bright-green #8ae234
93 Foreground color - bright yellow :heavy_check_mark: --ansi-bright-yellow #fce94f
94 Foreground color - bright blue :heavy_check_mark: --ansi-bright-blue #729fcf
95 Foreground color - bright magenta :heavy_check_mark: --ansi-bright-magenta #ad7fa8
96 Foreground color - bright cyan :heavy_check_mark: --ansi-bright-cyan #34e2e2
97 Foreground color - bright white :heavy_check_mark: --ansi-bright-white #eeeeec
100 Background color - bright black :heavy_check_mark: --ansi-bright-black #555753
101 Background color - bright red :heavy_check_mark: --ansi-bright-red #ef2929
102 Background color - bright green :heavy_check_mark: --ansi-bright-green

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptansiconversionhtml

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架