Cardinal JavaScript Syntax Highlighter
Cardinal is a JavaScript library and command-line tool (cdl) designed to syntax highlight JavaScript and JSON code with ANSI colors for terminal output. It offers features such as customizable color themes, optional line number printing, and support for UNIX pipes. The current stable version is 2.1.1, with its last known publication approximately eight years ago, indicating that the package is no longer actively maintained with regular updates or new features. This makes it suitable for projects requiring a stable, lightweight terminal highlighter with specific historical syntax support, but users should be aware of the lack of ongoing development. Its key differentiators include a strong focus on terminal-specific ANSI coloring and dual API/CLI functionality for direct code strings or files.
Common errors
-
Error: Cannot parse code
cause The input string or file contains invalid JavaScript syntax that cardinal's parser cannot understand, or JSX without the `jsx: true` option.fixReview the code for syntax errors. If using JSX, ensure the `{ jsx: true }` option is provided to the `highlight` function. Wrap calls to `highlight` or `highlightFileSync` in a `try...catch` block to handle parsing errors gracefully. -
TypeError: require is not a function
cause Attempting to use `require('cardinal')` in an ES Module context, where `require` is not natively available.fixSince `cardinal` is a CommonJS module, it should be used in a CommonJS environment or through a compatible loader/bundler. If you must use it in an ESM project, consider dynamic `import()` or transpilation if your toolchain supports it, or wrap it in a separate CJS file and import that wrapper.
Warnings
- gotcha The `json` option in `opts` is obsoleted. Cardinal now automatically understands both JSON and JavaScript, making this option redundant and its usage potentially ignored or deprecated.
- gotcha JSX syntax support requires explicitly setting `jsx: true` in the options object. By default, JSX will not be highlighted, and `cardinal` will throw a parsing error if encountered.
- gotcha When using `cardinal` as part of a UNIX pipe, such as `cat file.js | grep console | cdl`, lines that are not parsable as valid JavaScript may be printed without highlighting.
- breaking The package `cardinal` has not been updated in approximately eight years. This means it is no longer actively maintained, and users should not expect bug fixes, security updates, or compatibility with newer JavaScript language features or Node.js versions beyond its last stable release. Relying on it in new projects might introduce unaddressed vulnerabilities or compatibility issues.
Install
-
npm install cardinal -
yarn add cardinal -
pnpm add cardinal
Imports
- highlight
import { highlight } from 'cardinal';const cardinal = require('cardinal'); const highlightedCode = cardinal.highlight(codeString); - highlightFileSync
import cardinal from 'cardinal'; const highlightedFile = cardinal.highlightFileSync('./myFile.js');const cardinal = require('cardinal'); const highlightedFile = cardinal.highlightFileSync('./myFile.js'); - highlightFile
import { highlightFile } from 'cardinal';const cardinal = require('cardinal'); cardinal.highlightFile('./myFile.js', (err, highlighted) => { if (err) console.error(err); else console.log(highlighted); });
Quickstart
const cardinal = require('cardinal');
const fs = require('fs');
const path = require('path');
const codeToHighlight = `
function greet(name) {
const message = 'Hello, ' + name + '!';
// A comment about the greeting
console.log(message);
}
greet('World');
`;
const filePath = path.join(__dirname, 'example.js');
fs.writeFileSync(filePath, codeToHighlight);
console.log('--- Synchronous String Highlight ---');
try {
const highlightedString = cardinal.highlight(codeToHighlight, { linenos: true });
console.log(highlightedString);
} catch (e) {
console.error('Error highlighting string:', e.message);
}
console.log('\n--- Synchronous File Highlight ---');
try {
const highlightedFile = cardinal.highlightFileSync(filePath, { theme: 'github', linenos: false });
console.log(highlightedFile);
} catch (e) {
console.error('Error highlighting file sync:', e.message);
}
console.log('\n--- Asynchronous File Highlight ---');
cardinal.highlightFile(filePath, { jsx: true }, (err, highlighted) => {
if (err) {
console.error('Error highlighting file async:', err.message);
} else {
console.log(highlighted);
}
fs.unlinkSync(filePath); // Clean up temp file
});