-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
syntax highlighting in action logger #118
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>'; | ||
}); | ||
} |
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); | ||
}); | ||
}); |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we can use a NPM module for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. couldn't find a module that did only this. Most of them were overkill as they had separate CSS and were made to parse different languages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. Then that's fine. |
||
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>`; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we write an additional test case targetted for syntax highlighting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure