source-highlight
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

node-source-highlight

GNU source-highlight is a library to highlight source code in several different languages and output formats. This package provides Node.js bindings to the C++ library; currently, it requires you to have the library preinstalled.

Installation 📦

npm install --save source-highlight

Example

// Load the source code into a UTF-8 buffer
const input = Buffer.from(`
  console.log('Hello world!');
`);

// Create the SourceHighlight instance. "esc.outlang" will output colored characters to the console. You can
// use "html.outlang" too.
const sourceHighlight = new SourceHighlight('esc.outlang');

// Highlight the source code.
const output = sourceHighlight.highlight(input, 'javascript.lang', '').toString();

console.log(output);

API Documentation - SourceHighlight

import { SourceHighlight } from 'source-highlight';

source-highlight@1.0.0 exports just the SourceHighlight class. The API documentation for the GNU source-highlight can be found here. The methods of SourceHighlight provided by this package are enlisted below.

Constructor

constructor(outLangDef: string)

Create a SourceHighlight instance that will output into the format specified, e.g. html.outlang.

highlight

highlight(input: string, output: string, inputLang: string): void

Highlight the input file and write into the output file. inputLang is the name of the language definition file you want to use. (This cannot be a path, e.g. html.lang is valid not /path/to/html.lang)

highlight(input: Buffer, inputLang: string, inputFileName: string): Buffer

Highlight the source code in the UTF-8 buffer using the language definition file inputLang (again only a filename in the data directory, not a path). You can provide inputFileName optionally OR pass a blank string.

checkLangDef

checkLangDef(langFile: string): void

Check if the language definition file (in the data directory) is valid. It will throw an error if it is invalid.

checkOutLangDef

checkOutLangDef(outLangFile: string): void

Check if the output format specification file (in the data directory) is valid. It will throw an error if it is invalid.

setDataDir

setDataDir(dataDir: string): void

Set the data directory. By default, this will be a directory with all the language definition files and output formats that ship with GNU source-highlight.

setStyleFile

setStyleFile(styleFile: string): void

Set the style file.

setStyleCssFile

setStyleCssFile(cssFile: string): void

Use a CSS style file.

setStyleDefaultFile

setStyleDefaultFile(styleFile: string): void

Set the default style file.

setTitle

setTitle(title: string): void

setInputLang

setInputLang(inputLang: string): void

Set the input language to use when it can't be inferred.

setCss

setCss(css: string): void

setHeaderFileName

setHeaderFileName(headerFileName: string): void

Set the header file to be prepended to the output document.

setFooterFileName

setFooterFileName(footerFileName: string): void

Set the footer file to be appended to the output document.

setOutputDir

setOutputDir(outputDir: string): void

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    2
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    2

Package Sidebar

Install

npm i source-highlight

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

19.2 kB

Total Files

8

Last publish

Collaborators

  • shukantpal