Socket
Socket
Sign inDemoInstall

cardinal

Package Overview
Dependencies
3
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cardinal

Syntax highlights JavaScript code with ANSI colors to be printed to the terminal.


Version published
Weekly downloads
3.2M
decreased by-0.28%
Maintainers
1
Install size
443 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

cardinal Build Status

become a patron

NPM

car·di·nal (kärdn-l, kärdnl) - crested thick-billed North American finch having bright red plumage in the male.

screenshot

Features

  • highlights JavaScript code with ANSI colors to improve terminal output
  • theming support, see custom color themes
  • optionally print line numbers
  • API and command line interface (cdl)
  • .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

Note:

When installed globally, cardinal exposes itself as the cdl command.

Commandline

Highlight a file

cdl <file.js> [options]

options:

  • --nonum: turns off line number printing (relevant if it is turned on inside ~/.cardinalrc

As part of a UNIX pipe

cat file.js | grep console | cdl

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",
  "linenos": true|false
}
  • theme can be the name of any of the built-in themes or the full path to a custom theme anywhere on your computer.
  • linenos toggles line number printing

API

highlight(code[, opts])

  • returns the highlighted version of the passed code ({String}) or throws an error if it was not able to parse it
  • opts (see below)

highlightFileSync(fullPath[, opts])

  • returns the highlighted version of the file whose fullPath ({String}) was passed or throws an error if it was not able to parse it
  • opts (see below)

highlightFile(fullPath[, opts], 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
  • opts (see below)
  • callback ({Function}) has the following signature: function (err, highlighted) { .. }

opts

opts is an {Object} with the following properties:

  • theme {Object} is used to optionally override the theme used to highlight
  • linenos {Boolean} if true line numbers are included in the highlighted code
  • firstline {Integer} sets line number of the first line when line numbers are printed
  • jsx {Boolean} if true JSX syntax is supported, otherwise cardinal will raise an error when encountering JSX (default: false)

Note The json option is obsoleted and not necessary anymore as cardinal properly understands both JSON and JavaScript.

Examples (browse)

Keywords

FAQs

Last updated on 22 May 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc