Skip to main content

toAnsi(md, options?): string

Converts a Markdown string to an ANSI-escaped terminal string suitable for printing in a terminal emulator.

Import​

import { toAnsi } from 'md-to-rich'
// or sub-path:
import { toAnsi } from 'md-to-rich/ansi'

Signature​

function toAnsi(md: string, options?: AnsiOptions): string

Options​

OptionTypeDefaultDescription
columnsnumberprocess.stdout.columns ?? 80Terminal column width for word-wrap and horizontal rules
hyperlinksbooleanfalseEmit OSC 8 hyperlink sequences (clickable links in supported terminals)
themePartial<AnsiTheme>built-inOverride individual ANSI styles — only specified keys are replaced
gfmbooleantrueEnable GitHub Flavored Markdown
remarkPluginsPlugin[][]Additional remark plugins applied before serialization

Examples​

Basic​

import { toAnsi } from 'md-to-rich'

const output = toAnsi('# Hello\n\n**bold** and *italic*', { columns: 80 })
process.stdout.write(output)
toAnsi('[Docs](https://example.com)', { hyperlinks: true })
// → OSC 8 ;; https://example.com \a Docs OSC 8 ;; \a

Supported in iTerm2, Kitty, WezTerm, and most modern terminal emulators.

Custom theme​

import type { AnsiTheme } from 'md-to-rich'

const theme: Partial<AnsiTheme> = {
h1: { open: '\x1b[1m', close: '\x1b[0m' },
listBullet: '→',
}

toAnsi('# Title\n\n- item', { theme })

Default Theme​

The built-in theme uses only inline ANSI constants — no external dependencies like chalk:

KeyEffect
h1Bold + underline
h2Bold
h3Bold
boldBold
italicItalic
strikethroughStrikethrough
inlineCodeReverse video
codeBlockBox-drawing border
blockquoteDim + │ prefix
linkUnderline
listBullet• character
hrChar─ character

See the ANSI Theme guide for full details and examples.