chalk-template icon indicating copy to clipboard operation
chalk-template copied to clipboard

Escape-free, error-free, parser-based chalk-template

Open midzelis opened this issue 1 year ago • 2 comments

Hey @sindresorhus and @Qix- - please check out this enhanced chalk-template function. This started out as a contribution to https://github.com/google/zx/pull/635 - but after thinking about it, it would be more appropriate upstreamed here.

I happen to enjoy writing parsers - weird hobby, I know.

Background: When creating that PR for zx, I realized that I couldn't just send arbitrary text through chalkTemplate - it is very picky about escaping, and matching parenthesis, and styles, etc. I started to write a pre-processor for the template, to adopt a different start/end syntax. I was planning using new new prefix like ct:{ but that wasn't short enough, and it might have been common enough to require escaping, which is a PITA to handle correctly. Then I realized - hey I already have the parser that is pre-processing the template - I could just go all the way and do the actual styling too. This would also solve another problem I had with chalkTemplate - you couldn't use a template as an argument to a template, since it would be automatically escaped by chalkTemplate.

Then I realized - hey - we don't need to handle escapes at all. The syntax of {style or {rgb( or {# was already unique enough to not conflict with most arbitrary text. And, since its a parser, I didn't need to throw errors for mismatching { }s or anything - the fallback would be to just not style the unmatched text.

And so, the parser, AST, and renderer was born. This was written with typescript annotations as well, so I added typescript and prettier as dependencies.

Additionally, I changed the project layout a bit: created /src and /dist. Lastly, the exports were manipulated a little bit. There is no more default-export. There is one that uses chalk, one that uses chalkStderr, and one that you can use to bring your own chalk. There is also an 'experimental' export that exposes the AST. This is really just used to help play/develop the parser, but maybe its useful to someone. Its experimental as this is v1 of the AST and it may need some time to bake before being "API."

Technically, the parser is flexible to allow changing the 'prefix' of the template, currently set to {- this could be configurable, but currently it is not. The suffix is always '}' but that too could probably be configureable without much work - not sure if its a good idea.

Anyways, I'm wondering if there is any appetite for merging this PR, especially since this is basically a rewrite. I'm open to all comments/critiques - especially around the actual exported functions. And naming. Naming is hard. I'm bad at it.

This PR probably needs more documentation, more tests, and more general refinement. But I wanted to start the discussion before deciding if it was worth any more effort.

midzelis avatar May 30 '23 20:05 midzelis