4.8 KiB
emphasize
ANSI syntax highlighting in for your terminal. Like highlight.js (through lowlight).
emphasize
supports all 190 syntaxes of highlight.js.
Install
npm:
npm install emphasize
Use
Say example.css
looks as follows:
@font-face {
font-family: Alpha;
src: url('Bravo.otf');
}
body, .charlie, #delta {
color: #bada55;
background-color: rgba(33, 33, 33, 0.33);
font-family: "Alpha", sans-serif;
}
@import url(echo.css);
@media print {
a[href^=http]::after {
content: attr(href)
}
}
And example.js
contains the following:
var fs = require('fs')
var emphasize = require('emphasize')
var doc = fs.readFileSync('./example.css', 'utf8')
var output = emphasize.highlightAuto(doc).value
console.log(output)
Now, running node example
yields:
@\u001b[32mfont-face\u001b[39m {
\u001b[33mfont-family\u001b[39m: Alpha;
\u001b[33msrc\u001b[39m: \u001b[31murl\u001b[39m(\u001b[36m'Bravo.otf'\u001b[39m);
}
\u001b[32mbody\u001b[39m, \u001b[34m.charlie\u001b[39m, \u001b[34m#delta\u001b[39m {
\u001b[33mcolor\u001b[39m: \u001b[36m#bada55\u001b[39m;
\u001b[33mbackground-color\u001b[39m: \u001b[31mrgba\u001b[39m(33, 33, 33, 0.33);
\u001b[33mfont-family\u001b[39m: \u001b[36m\"Alpha\"\u001b[39m, sans-serif;
}
@\u001b[32mimport\u001b[39m url(echo.css);
@\u001b[32mmedia\u001b[39m print {
\u001b[32ma\u001b[39m\u001b[35m[href^=http]\u001b[39m\u001b[35m::after\u001b[39m {
\u001b[33mcontent\u001b[39m: \u001b[31mattr\u001b[39m(href)
}
}
And looks as follows:
API
emphasize.registerLanguage(name, syntax)
Register a syntax.
Like low.registerLanguage()
.
emphasize.highlight(language, value[, sheet])
Highlight value
as a language
grammar.
Like low.highlight()
, but the return object’s value
property is
a string instead of hast nodes.
You can pass in a sheet
(Sheet?
, optional) to configure the theme.
emphasize.highlightAuto(value[, sheet | options])
Highlight value
by guessing its grammar.
Like low.highlightAuto()
, but the return object’s value
property is a string instead of hast nodes.
You can pass in a sheet
(Sheet?
, optional) directly or as
options.sheet
to configure the theme.
Sheet
A sheet is an object mapping highlight.js classes to functions.
The hljs-
prefix must not be used in those classes.
The “descendant selector” (a space) is supported.
Those functions receive a value (string
), which they should wrap in ANSI
sequences, and return.
For convenience, chalk’s chaining of styles is suggested.
An abbreviated example is as follows:
{
'comment': chalk.gray,
'meta meta-string': chalk.cyan,
'meta keyword': chalk.magenta,
'emphasis': chalk.italic,
'strong': chalk.bold,
'formula': chalk.inverse
};
Emphasize in the browser
Do not require emphasize
in the browser as that would include a very large
file.
Instead, require emphasize/lib/core
, and include only the used highlighters.
For example:
var emphasize = require('emphasize/lib/core')
var js = require('highlight.js/lib/languages/javascript')
emphasize.registerLanguage('javascript', js)
emphasize.highlight('js', '"use strict";').value
// '\u001b[35m"use strict"\u001b[39m;'