stylized-json
Version:
Format and style a json object to display it in HTML
22 lines • 870 B
JavaScript
//Source: http://jsfiddle.net/unLSJ/
function replacer(match, pIndent, pKey, pVal, pEnd) {
var key = '<span class=json-key>',
val = '<span class=json-value>',
str = '<span class=json-string>',
r = pIndent || '';
if (pKey) r = r + key + pKey.replace(/[": ]/g, '') + '</span>: ';
if (pVal) r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
return r + (pEnd || '');
}
module.exports = {
prettyPrint: function (obj) {
var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg,
self = this;
return '<pre class="json-pre"><code>' +
JSON.stringify(obj, null, 3)
.replace(/&/g, '&').replace(/\\"/g, '"')
.replace(/</g, '<').replace(/>/g, '>')
.replace(jsonLine, replacer) +
'</code></pre>';
}
};