question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extend chalk tagged template string behavior

See original GitHub issue

Hey,

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:closed
  • Created 6 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
clintwoodcommented, Sep 5, 2017

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:

// logger.js
const chalk = require('chalk');
const chalkTemplates = require('chalk/templates');

function log(msg) {
  console.log(chalkTemplates(chalk, msg));
}
module.exports.log = log;

// some-other-file.js
const logger = require('./logger');

const msg = 'here';
logger.log(`Some {red text} right {green ${msg}}!`);


// I'm fully aware you could import chalk and do
console.log(chalk`Some {red text} right {green ${msg}}!`);

So it would be very nice to formally expose chalk/templates.js via chalk’s api - something like chalk.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.

0reactions
Qix-commented, Jun 11, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found