Cardinal JavaScript Syntax Highlighter

2.1.1 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `cardinal` to highlight both a JavaScript code string and a JavaScript file, using both synchronous and asynchronous API calls. It also shows how to apply options like line numbers and themes, and includes basic error handling for parsing failures.

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
});

view raw JSON →