chalk/chalk-template

Call template literal as normal function

luwol03 opened this issue · 5 comments

Hello,

I try to parse a string (e.g. "hello {green world}") that contains the chalk template syntax.

My first try was to parse that in the template literal like this:

const myString = "hello {green world}";
console.log(chalk`${myString}`);

but without success. It returned the exact string I passed in without the green color format. So I debugged a little bit and found out that following works.

const d = ["hello {green world}"];
d.raw = [...d];
console.log(chalk(d));

But that looks very messy. Is there a better approche?

I currently use this. I am getting the template string from config file, so chalk`...` tag cannot be used.
Will this enhancement be a part of the main chalk repo too?

image

I dont really mind the extra two lines, however chalk/source/templates.js might change, without any change in the chalk API.
Something like chalk.template("hello {red world}!") would be nice

The chalk cli also used that really messy raw thing I mentioned in the description above.

Qix- commented

The "really messy raw thing" is how you do what is being asked in Javascript. That's how tagged template literals work under the hood - it's the same way you'd call many other tagged template literal functions.

Qix- commented

0.4.0 has just landed with this support - see #8. Thanks @GeorgeTaveras1231!