From 8b2157c306414679f52308ab4011e598d9aadc8f Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Tue, 28 Oct 2025 14:57:27 +0000 Subject: [PATCH] chore: migrate from chalk to stdlib --- README.md | 2 +- lib/parser.js | 6 +++--- lib/theme.js | 11 +++++------ package-lock.json | 7 ++++++- package.json | 1 - test/theme.spec.js | 36 ++++++++++++++++++------------------ 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 51c741f4..a3fbd658 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ For the list of available translations, please refer to the main [tldr](https:// You can configure the `tldr` client by adding a `.tldrrc` file in your HOME directory. You can copy the contents of the `config.json` file from the repo to get the basic structure to start with, and modify it to suit your needs. -The default color theme is the one named `"simple"`. You can change the theme by assigning a different value to the `"theme"` variable -- either to one of the pre-configured themes, or to a new theme that you have previously created in the `"themes"` section. Note that the colors and text effects you can choose are limited. Refer to the [chalk documentation](https://github.com/chalk/chalk#styles) for all options. +The default color theme is the one named `"simple"`. You can change the theme by assigning a different value to the `"theme"` variable -- either to one of the pre-configured themes, or to a new theme that you have previously created in the `"themes"` section. Note that the colors and text effects you can choose are limited. Refer to the [Node.js documentation](https://nodejs.org/api/util.html#modifiers) for all options. ```json { diff --git a/lib/parser.js b/lib/parser.js index 6a1088ca..14abd21c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1,8 +1,8 @@ 'use strict'; +const { styleText } = require('node:util'); const unescape = require('lodash/unescape'); const marked = require('marked'); -const chalk = require('chalk'); const index = require('./index'); const allElements = [ @@ -70,11 +70,11 @@ exports.parse = (markdown) => { }; r.strong = (text) => { - return chalk.bold(text); + return styleText('bold', text); }; r.em = (text) => { - return chalk.italic(text); + return styleText('italic', text); }; r.listitem = (text) => { diff --git a/lib/theme.js b/lib/theme.js index 553fe905..a1a59476 100644 --- a/lib/theme.js +++ b/lib/theme.js @@ -1,19 +1,18 @@ 'use strict'; -const get = require('lodash/get'); const isEmpty = require('lodash/isEmpty'); const identity = require('lodash/identity'); -const chalk = require('chalk'); +const { styleText } = require('node:util'); // Translates strings like 'red, underline, bold' -// into function chalk.red.underline.bold(text) +// into function styleText(['red', 'underline', 'bold'], text) function buildStylingFunction(styles) { if (isEmpty(styles)) { return identity; } - let stylingFunction = chalk; - let stylesPath = styles.replace(/,\s*/g, '.'); - return get(stylingFunction, stylesPath); + + let stylesArr = styles.split(/,\s*/g); + return (text) => styleText(stylesArr, text); } class Theme { diff --git a/package-lock.json b/package-lock.json index e7666ed4..042c63a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "dependencies": { "adm-zip": "^0.5.10", "axios": "^1.6.0", - "chalk": "^4.1.0", "commander": "^6.1.0", "fs-extra": "^11.2.0", "glob": "^11.0.0", @@ -87,6 +86,7 @@ "integrity": "sha512-bXYxrXFubeYdvB0NhD/NBB3Qi6aZeV20GOWVI47t2dkecCEoneR4NPVcb7abpXDEvejgrUfFtG6vG/zxAKmg+g==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.27.1", @@ -837,6 +837,7 @@ "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.6.1.tgz", "integrity": "sha512-/KCsg3xSlR+nCK8/8ZYSknYxvXHwubJrU82F3Lm1Fp6789VQ0/3RJKfsmRXjqfaTA++23CvC3hqmqe/2GEt6Kw==", "license": "MIT", + "peer": true, "dependencies": { "cluster-key-slot": "1.1.2", "generic-pool": "3.9.0", @@ -1147,6 +1148,7 @@ "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "dev": true, "license": "MIT", + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -1423,6 +1425,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001718", "electron-to-chromium": "^1.5.160", @@ -2114,6 +2117,7 @@ "integrity": "sha512-ocgh41VhRlf9+fVpe7QKzwLj9c92fDiqOj8Y3Sd4/ZmVA4Btx4PlUYPq4pp9JDyupkf1upbEXecxL2mwNV7jPQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.12.1", @@ -4806,6 +4810,7 @@ "resolved": "https://registry.npmjs.org/pg/-/pg-8.16.0.tgz", "integrity": "sha512-7SKfdvP8CTNXjMUzfcVTaI+TDzBEeaUnVwiVGZQD1Hh33Kpev7liQba9uLd4CfN8r9mCVsD0JIpq03+Unpz+kg==", "license": "MIT", + "peer": true, "dependencies": { "pg-connection-string": "^2.9.0", "pg-pool": "^3.10.0", diff --git a/package.json b/package.json index 22280653..2118414f 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,6 @@ "dependencies": { "adm-zip": "^0.5.10", "axios": "^1.6.0", - "chalk": "^4.1.0", "commander": "^6.1.0", "fs-extra": "^11.2.0", "glob": "^11.0.0", diff --git a/test/theme.spec.js b/test/theme.spec.js index 06cff0f7..ab697ddc 100644 --- a/test/theme.spec.js +++ b/test/theme.spec.js @@ -1,7 +1,7 @@ 'use strict'; +const { styleText } = require('node:util'); const Theme = require('../lib/theme'); -const chalk = require('chalk'); describe('Theme', () => { @@ -18,31 +18,31 @@ describe('Theme', () => { it('should render name with green and bold', () => { theme.renderCommandName('text') .should.equal( - chalk.green.bold('text')); + styleText(['green', 'bold'], 'text')); }); it('should render description with red and underline', () => { theme.renderMainDescription('text') .should.equal( - chalk.red.underline('text')); + styleText(['red', 'underline'], 'text')); }); it('should render example description with blue', () => { theme.renderExampleDescription('text') .should.equal( - chalk.blue('text')); + styleText('blue', 'text')); }); it('should render example code with blue', () => { theme.renderExampleCode('text') .should.equal( - chalk.bold('text')); + styleText('bold', 'text')); }); it('should render example argument with yellow, dim, underline', () => { theme.renderExampleToken('text') .should.equal( - chalk.yellow.dim.underline('text')); + styleText(['yellow', 'dim', 'underline'], 'text')); }); }); @@ -59,31 +59,31 @@ describe('Theme', () => { it('should render name with greenBright and bold', () => { theme.renderCommandName('text') .should.equal( - chalk.greenBright.bold('text')); + styleText(['greenBright', 'bold'], 'text')); }); it('should render description with greenBright and bold', () => { theme.renderMainDescription('text') .should.equal( - chalk.greenBright.bold('text')); + styleText(['greenBright', 'bold'], 'text')); }); it('should render example description with greenBright', () => { theme.renderExampleDescription('text') .should.equal( - chalk.greenBright('text')); + styleText('greenBright', 'text')); }); it('should render example code with redBright', () => { theme.renderExampleCode('text') .should.equal( - chalk.redBright('text')); + styleText('redBright', 'text')); }); it('should render example argument with white', () => { theme.renderExampleToken('text') .should.equal( - chalk.white('text')); + styleText('white', 'text')); }); }); @@ -102,37 +102,37 @@ describe('Theme', () => { it('should render name with greenBright and bold', () => { theme.renderCommandName('text') .should.equal( - chalk.greenBright.bold('text')); + styleText(['greenBright', 'bold'], 'text')); }); it('should render description with greenBright and bold', () => { theme.renderMainDescription('text') .should.equal( - chalk.greenBright.bold('text')); + styleText(['greenBright', 'bold'], 'text')); }); it('should render example description with greenBright', () => { theme.renderExampleDescription('text') .should.equal( - chalk.greenBright('text')); + styleText('greenBright', 'text')); }); it('should render example code with redBright', () => { theme.renderExampleCode('text') .should.equal( - chalk.redBright('text')); + styleText('redBright', 'text')); }); it('should render example arguments with magenta, white, and blue, for boolean, number, and string respectively', () => { theme.renderExampleToken('true') .should.equal( - chalk.magenta('true')); + styleText('magenta', 'true')); theme.renderExampleToken('9') .should.equal( - chalk.white('9')); + styleText('white', '9')); theme.renderExampleToken('text') .should.equal( - chalk.blue('text')); + styleText('blue', 'text')); }); }); });