#1274·shiki

`codeToANSI` depends on default `ansis` instance which infers color support

Author: TomerAberbachCreated Apr 30, 2026Updated Apr 30, 2026

Validations

Describe the bug

The default exported ansis instance detects color support based on environment variables like FORCE_COLOR, NO_COLOR, --color, random other stuff, etc.

codeToANSI uses this default instance so if any of these things happen to be set "badly", then no highlighting will occur.

This doesn't really make sense because codeToANSI's result isn't necessarily immediately printed to the current environment. It could be used for other things, and it's also possible the current program has different business logic around when to use color.

codeToANSI should probably default to assuming all color is supported and maybe should have a configuration option for setting the specific color support level for people who care about that (they could get the level from ansis itself if they want to respect the environment).

Reproduction

Has ANSI:

typescript
const { codeToANSI } = await import(`@shikijs/cli`)
                                                                                                                                     
const out = await codeToANSI(`# hi`, `markdown`, `github-dark-default`)
console.log(/\x1b\[/u.test(out) ? `has ANSI` : `no ANSI`)                                                                          
console.log(JSON.stringify(out))
//=> has ANSI
//=> "\u001b[1m\u001b[38;2;121;192;255m# hi\u001b[39m\u001b[22m\n"

Does not have ANSI:

typescript
process.env.NO_COLOR = '1'

const { codeToANSI } = await import(`@shikijs/cli`)
                                                                                                                                     
const out = await codeToANSI(`# hi`, `markdown`, `github-dark-default`)
console.log(/\x1b\[/u.test(out) ? `has ANSI` : `no ANSI`)                                                                          
console.log(JSON.stringify(out))
//=> no ANSI
//=> "# hi\n"

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests