UNPKG

stylized-json

Version:

Format and style a json object to display it in HTML

22 lines 870 B
//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, '&amp;').replace(/\\"/g, '&quot;') .replace(/</g, '&lt;').replace(/>/g, '&gt;') .replace(jsonLine, replacer) + '</code></pre>'; } };