highlight source code syntax with html
Works in node and the browser. Supported languages:
Very small:
$ browserify . | uglifyjs -cm 2>/dev/null | gzip | wc -c
1776
$ browserify all.js | uglifyjs -cm 2>/dev/null | gzip | wc -c
3391
You can load all the languages:
var highlight = require('highlight-syntax/all')
var fs = require('fs')
var src = fs.readFileSync(process.argv[3],'utf8')
console.log(highlight(src, { lang: process.argv[2] }))
Or you can load a subset of languages. This will make the bundle you deliver to the browser smaller.
var highlight = require('highlight-syntax')([
require('highlight-syntax/c'), require('highlight-syntax/sh')
])
var fs = require('fs')
var src = fs.readFileSync(process.argv[3],'utf8')
console.log(highlight(src, { lang: process.argv[2] }))
You can pass this module to marked to highlight code in a markdown file:
var highlight = require('highlight-syntax')
var marked = require('marked')
var fs = require('fs')
var src = fs.readFileSync('readme.md','utf8')
console.log(marked(src, { highlight: highlight }))
var highlighter = require('highlight-syntax')
var highlight = require('highlight-syntax/all')
The css files in this distribution are:
Create a highlighter function from an array of rules
. You can load the rules
explicitly from:
require('highlight-syntax/c')
- crequire('highlight-syntax/sh')
- bashrequire('highlight-syntax/js')
- javascriptTurn a string of code src
written in opts.lang
to a syntax-highlighted
string of html. If opts
is a string, it is interpreted as the opts.lang
.
opts.lang
- string language name or file extensionhighlight-syntax FILES... {OPTIONS}
-o --outfile Write output to a file. Default: - (stdout)
-l --lang Set a language explicitly for stdin input.
-t --theme Include an inline css theme. Example: dark
To get the library:
npm install highlight-syntax
To get the command:
npm install -g highlight-syntax
BSD