chalker
Set ansi colors in strings using <>
markers and chalk.
Usage
const chalker = ; console; // with template string taggingconsole;
A typical use case is to easily manage colors for logs that could go to console or log servers.
const msg = `<red>some error occurred...</red>`; // log to console for visual with colorsif !production console; // log to log server with colors removedlogger;
Install
npm i --save chalker
Demo
Marker Details
-
Color markers has the
<red>red text</red>
format. You can use any valid methods chalk supports.- For example,
<blue.bold>blue bold text</blue.bold>
will colorizeblue bold text
withchalk.blue.bold
. - Closing marker can be simply
</>
- For example,
-
The following HTML entities escapes are supported:
Entity Character Entity Character <
< >
> &
&
non-breaking space '
' ©
© "
" ®
® -
HTML escape using code points also works:
- Hex -
&#xhhhh;
wherehhhh
is the Hex code point. - Decimal -
&#nnnn;
wherennnn
is the Decimal code point. - ie:
��
makes 👩
- Hex -
Advanced Chalk Colors
Chalk advanced colors can be applied with:
chalk API | chalker marker | chalk API | chalker marker |
---|---|---|---|
chalk.rgb |
<(255, 10, 20)> , <rgb(255,10,20)> |
chalk.bgRgb |
<bg(255, 10, 20)> , <bgRgb(255,10,20)> |
chalk.hex |
<#FF0000> , <hex(#FF0000)> |
chalk.bgHex |
<bg#0000FF> , <bgHex(#0000FF)> |
chalk.keyword |
<orange> , <(orange)> , <keyword(orange)> |
chalk.bgKeyword |
<bg-orange> , <bg(orange)> , <bgKeyword(orange)> |
chalk.hsl |
<hsl(32,100,50)> |
chalk.bgHsl |
<bgHsl(32,100,50)> |
chalk.hsv |
<hsv(32,100,100)> |
chalk.bgHsv |
<bgHsv(32,100,100)> |
chalk.hwb |
<hwb(32,0,50)> |
chalk.bgHwb |
<bgHwb(32,0,50)> |
More details
-
a marker is tried with
chalk.keyword
if:-
it's not detected as hex value
-
it doesn't contain params enclosed in
()
-
it's not found as a basic color that
chalk
supports -
for example, this is a chalk color keyword:
<orange>
-
If it's prefixed with
"bg-"
then it's tried usingchalk.bgKeyword
- ie:
<bg-orange>
- ie:
-
-
All markers can be comined with
.
in any order as long as they work with chalk- ie:
<#FF0000.bg#0000FF.bg-orange.keyword(red)>
- ie:
APIs
chalker
;
str
- String with chalker color markerschalkInstance
- Optional custom instance of chalk.- ie: created from
new chalk.constructor({level: 2})
- ie: created from
Returns: A string with terminal/ansi color codes
If
chalk.supportsColor
isfalse
, then it will simply remove the<>
markers and decode HTML entities only.
chalker.remove
chalker;
str
- String with chalker color markerskeepHtml
- Iftrue
, then don't decode HTML entity escapes.
Simply remove all chalker markers and return the plain text string, with HTML escapes decoded.
Returns: A plain text string without chalker color markers
chalker.decodeHtml
chalker;
str
- String to decode HTML entities
Returns: String with HTML entities escapes decoded
License
Copyright (c) 2019-present, Joel Chen
Licensed under the Apache License, Version 2.0.