Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Highlight JSON

The json option allows JSON to be highlighted.
  • Loading branch information...
commit 94f3afee9c5150256897c2b680334c7dbd7d8f75 1 parent 64ed9c4
Tim Schaub tschaub authored
1  README.md
View
@@ -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))
10 examples/highlight-json.js
View
@@ -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 lib/highlight.js
View
@@ -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;
41 test/cardinal-highlight-json.js
View
@@ -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();
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.