-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 3 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,32 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = highlight; | ||
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,26 @@ | ||
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