Extend chalk tagged template string behavior
See original GitHub issueHey,
I would like to extend the behavior of the new tagged template string feature of chalk. I was hoping that I can call chalk.apply()
with the arguments of my own tag and transform the output even more. Unfortunately, this is not working because there is no apply method on the chalk object. Do you know any way I can still get my desired functionality?
'use strict'
const chalk = require('chalk')
function format() {
const args = Array.from(arguments)
// This is not working:
const string = chalk.apply(chalk, args)
const match = args[0].join('').match(/^[ \t]*(?=\S)/gm)
if (!match) {
return string
}
const indent = Math.min.apply(Math, match.map(line => line.length))
const regex = new RegExp(`^[ \\t]{${indent}}`, 'gm')
const dedentedString = indent > 0 ? string.replace(regex, '') : string
return dedentedString.trim()
}
module.exports = format
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Using Chalk 2.0's Tagged Template Literals For Nested And ...
While Chalk works seamlessly in 95% of use-cases, the one behavior that can be a little hit-and-miss is the use of nested styles...
Read more >Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, ... However, a tagged template literal may not result in a string; ...
Read more >How to create a tagged template literal and pass argument to ...
Here is an example of modifying the output from the tagged template literal. const doMore = (template, ...values) => { /** * Make...
Read more >Understanding Template Literals in JavaScript - DigitalOcean
A tagged template starts with a tag function that parses a template literal, allowing you more control over manipulating and returning a dynamic ......
Read more >8. Template literals - Exploring JS
Tagged template literals (code): function calls; Web templates (data): HTML with blanks to be filled in. Template literals are string literals that can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure if this is off-topic but I have found it hugely useful in a logger utility to directly use
chalk/templates.js
to effectively give me the template processing that is available for tagged literals. I’m not sure if I’ve missed if this is already possible but this is (a very summarized version of) what I’m doing:So it would be very nice to formally expose
chalk/templates.js
via chalk’s api - something likechalk.transform
.PS I am aware of ES2015 tagged template literal syntax however this didn’t work for me in my case since string literals are primarily used in the calling site/code. Also doing it this way means chalk is restricted to the logger. Lastly it would be great to be able to detemplatize a string to strip out color templates
chalk.strip('Some {blue text}'); => Some text
.Closing this as I feel there are a sufficient amount of template-related solutions to the various out-of-the-box needs and this is becoming a bit of a kitchen-sink issue.
If you have a case that is not covered here, please open a new ticket with detailed information about what you’re trying to achieve.