Skip to content

Commit

Permalink
Highlight JSON
Browse files Browse the repository at this point in the history
The json option allows JSON to be highlighted.
  • Loading branch information
tschaub committed Jul 30, 2013
1 parent 64ed9c4 commit 94f3afe
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -110,6 +110,7 @@ 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
- `json` {Boolean} if `true` highlights JSON in addition to JavaScript

## Examples ([*browse*](https://github.com/thlorenz/cardinal/tree/master/examples))

Expand Down
10 changes: 10 additions & 0 deletions examples/highlight-json.js
@@ -0,0 +1,10 @@
// This file will highlight the passed code using the custom theme when run via: "node highlight-json"

var cardinal = require('..');

var json = JSON.stringify({
foo: 'bar',
baz: 'quux'
});

console.log(cardinal.highlight(json, {json: true}));
7 changes: 7 additions & 0 deletions lib/highlight.js
Expand Up @@ -66,11 +66,18 @@ function addLinenos (highlightedCode, firstline) {

module.exports = function highlight (code, opts) {
opts = opts || { };
if (opts.json) {
code = '!\n' + code;
}
try {

var result = redeyed(code, opts.theme || theme)
, firstline = opts.firstline && !isNaN(opts.firstline) ? opts.firstline : 1;

if (opts.json) {
result.code = result.code.split('\n').slice(1).join('\n');
}

return opts.linenos ? addLinenos(result.code, firstline) : result.code;
} catch (e) {
e.message = 'Unable to perform highlight. The code contained syntax errors: ' + e.message;
Expand Down
41 changes: 41 additions & 0 deletions test/cardinal-highlight-json.js
@@ -0,0 +1,41 @@
'use strict';
/*jshint asi: true*/

var test = require('tap').test
, util = require('util')
, customTheme = require('./fixtures/custom')
, cardinal = require('..')


function inspect (obj) {
return console.log(util.inspect(obj, false, 5, false))
}

var json = JSON.stringify({
foo: 'bar',
baz: 'quux',
bam: null
});

test('supplying custom theme', function (t) {
var highlighted = cardinal.highlight(json, { json: true, theme: customTheme });

t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[92m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
t.end();
});

test('not supplying custom theme', function (t) {
var highlighted = cardinal.highlight(json, { json: true });

t.equals(highlighted, '\u001b[33m{\u001b[39m\u001b[92m"foo"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"bar"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"baz"\u001b[39m\u001b[93m:\u001b[39m\u001b[92m"quux"\u001b[39m\u001b[32m,\u001b[39m\u001b[92m"bam"\u001b[39m\u001b[93m:\u001b[39m\u001b[90mnull\u001b[39m\u001b[33m}\u001b[39m')
t.end();
});

test('without json option', function (t) {
try {
cardinal.highlight(json);
} catch (e) {
t.similar(e.message, /Unable to perform highlight. The code contained syntax errors.* Line 1: Unexpected token /)
t.end();
}
});

0 comments on commit 94f3afe

Please sign in to comment.