What is cardinal?
The cardinal npm package is a syntax highlighter for JavaScript code. It can be used to display JavaScript code with syntax highlighting in terminal applications. It uses the redeyed package to handle the actual syntax highlighting.
What are cardinal's main functionalities?
Syntax highlighting for JavaScript code
This feature allows you to pass JavaScript code as a string to the `highlight` function, which returns the code with terminal-compatible syntax highlighting.
const cardinal = require('cardinal');
const highlightedCode = cardinal.highlight('const x = 123;');
console.log(highlightedCode);
Syntax highlighting from a file
This feature allows you to read JavaScript code from a file and then use the `highlight` function to display it with syntax highlighting in the terminal.
const cardinal = require('cardinal');
const fs = require('fs');
fs.readFile('example.js', 'utf8', function(err, code) {
if (err) throw err;
const highlightedCode = cardinal.highlight(code);
console.log(highlightedCode);
});
Custom theme support
Cardinal allows you to specify a custom theme for syntax highlighting by passing a theme object as an option to the `highlight` function.
const cardinal = require('cardinal');
const customTheme = require('./myCustomTheme.json');
const highlightedCode = cardinal.highlight('const x = 123;', { theme: customTheme });
console.log(highlightedCode);
Other packages similar to cardinal
chalk
Chalk is a popular npm package for styling terminal strings. Unlike cardinal, which is focused on syntax highlighting, chalk provides a more general approach to styling strings with colors, background colors, and text styles. It does not parse code for syntax highlighting.
highlight.js
Highlight.js is a syntax highlighter for the web, but it can also be used in Node.js environments. It supports a wide range of languages and has many themes available. It is more versatile than cardinal as it is not limited to JavaScript and can be used in both browser and server contexts.
prismjs
Prism is another syntax highlighting library that works both in the browser and on the server. It is similar to highlight.js in terms of functionality and also supports a wide range of programming languages and comes with a variety of themes.
cardinal
car·di·nal (kärdn-l, kärdnl) - A North American finch (Cardinalis cardinalis) having a crested head, a short thick
bill, and bright red plumage
var cardinal = require('cardinal');
function highlight () {
cardinal.highlightFile(__filename, function (err, res) {
if (err) return console.error(err);
console.log(res);
});
}
highlight();
Features
- highlights JavaScript code with ANSI colors to improve terminal output
- theming support, see custom color themes
- API and command line interface
.cardinalrc
config to customize settings- supports UNIX pipes
Table of Contents generated with DocToc
Installation
As library
npm install cardinal
As Commandline Tool
[sudo] npm install -g cardinal
Commandline
Highlight a file
cardinal file.js
As part of a UNIX pipe
cat file.js | grep console | cardinal
Note:
Not all code lines may be parsable JavaScript. In these cases the line is printed to the terminal without
highlighting it.
Theme
The default theme will be used for highlighting.
To use a different theme, include a .cardinalrc
file in your HOME
directory.
This is a JSON file of the following form:
{
"theme": "hide-semicolons"
}
theme
can be the name of any of the built-in themes or the
full path to a custom theme anywhere on your computer.
API
highlight(code[, theme])
- returns the highlighted version of the passed code ({String}) or throws an error if it was not able to parse it
theme
({Object}) is used to optionally override the theme used to highlight
highlightFileSync(fullPath[, theme])
- returns the highlighted version of the file whose fullPath ({String}) was passed or throws an error if it was not able
to parse it
theme
({Object}) is used to optionally override the theme used to highlight
highlightFile(fullPath[, theme], callback)
- calls back with the highlighted version of the file whose fullPath ({String}) was passed or with an error if it was not able
to parse it
theme
({Object}) is used to optionally override the theme used to highlightcallback
({Function}) has the following signature: function (err, highlighted) { .. }