forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
syntax highlighting in action logger (storybookjs#118)
* feat(highlight):added syntax highlighting in the action logger * fix lint error * fixed tests * added tests for highlight
- Loading branch information
1 parent
0a8d9bf
commit 6b8a395
Showing
6 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = highlight; | ||
/** | ||
* Parses the JSON string and adds styling and class based on whether | ||
* a part is string, number, undefined, null or key. Also removes quotes | ||
* from keys. | ||
* | ||
* @param data A stringified JSON | ||
* @returns {string} String with styling | ||
*/ | ||
function highlight(data) { | ||
var json = data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||
var regex = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g; // eslint-disable-line | ||
return json.replace(regex, function (match) { | ||
var className = 'number'; | ||
var style = void 0; | ||
var result = match; | ||
if (/^"/.test(result)) { | ||
if (/:$/.test(result)) { | ||
className = 'key'; | ||
style = 'color:#800080'; | ||
result = match.replace(/"/g, ''); | ||
} else { | ||
className = 'string'; | ||
style = 'color:#a31515'; | ||
} | ||
} else if (/true|false/.test(result)) { | ||
className = 'boolean'; | ||
style = 'color:#066066'; | ||
} else if (/null|undefined/.test(result)) { | ||
className = 'null'; | ||
style = 'color:#a31515'; | ||
} | ||
return '<span class="' + className + '" style="' + style + '">' + result + '</span>'; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { describe, it } = global; | ||
import { expect } from 'chai'; | ||
import highlight from '../highlight'; | ||
|
||
describe('highlight', function () { | ||
it('should remove quotes from keys and add correct colour', function () { | ||
const data = '{ "name": "react-storybook" }'; | ||
const expected = '{ <span class="key" style="color:#800080">name:</span> <span class="string" style="color:#a31515">"react-storybook"</span> }'; // eslint-disable-line | ||
expect(highlight(data)).to.equal(expected); | ||
}); | ||
|
||
it('should preserve new lines also', function () { | ||
const data = '{\n "name": "test action",\n "args": "things"\n}'; | ||
const expected = '{\n ' + | ||
'<span class="key" style="color:#800080">name:</span> ' + | ||
'<span class="string" style="color:#a31515">"test action"</span>,\n ' + | ||
'<span class="key" style="color:#800080">args:</span> ' + | ||
'<span class="string" style="color:#a31515">"things"</span>\n}'; | ||
expect(highlight(data)).to.equal(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Parses the JSON string and adds styling and class based on whether | ||
* a part is string, number, undefined, null or key. Also removes quotes | ||
* from keys. | ||
* | ||
* @param data A stringified JSON | ||
* @returns {string} String with styling | ||
*/ | ||
export default function highlight(data) { | ||
const json = data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); | ||
const regex = /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g; // eslint-disable-line | ||
return json.replace(regex, (match) => { | ||
let className = 'number'; | ||
let style; | ||
let result = match; | ||
if (/^"/.test(result)) { | ||
if (/:$/.test(result)) { | ||
className = 'key'; | ||
style = 'color:#800080'; | ||
result = match.replace(/"/g, ''); | ||
} else { | ||
className = 'string'; | ||
style = 'color:#a31515'; | ||
} | ||
} else if (/true|false/.test(result)) { | ||
className = 'boolean'; | ||
style = 'color:#066066'; | ||
} else if (/null|undefined/.test(result)) { | ||
className = 'null'; | ||
style = 'color:#a31515'; | ||
} | ||
return `<span class="${className}" style="${style}">${result}</span>`; | ||
}); | ||
} |