/**
* Module to compose output JSON preview
*/
const cPreview = (function (module) {
/**
* Shows JSON in pretty preview
* @param {object} output - what to show
* @param {Element} holder - where to show
*/
module.show = function(output, holder) {
/** Make JSON pretty */
output = JSON.stringify( output, null, 4 );
/** Encode HTML entities */
output = encodeHTMLEntities( output );
/** Stylize! */
output = stylize( output );
holder.innerHTML = output;
};
/**
* Converts '>', '<', '&' symbols to entities
*/
function encodeHTMLEntities(string) {
return string.replace(/&/g, '&').replace(//g, '>');
}
/**
* Some styling magic
*/
function stylize(string) {
/** Stylize JSON keys */
string = string.replace( /"(\w+)"\s?:/g, '"$1" :');
/** Stylize tool names */
string = string.replace( /"(text|quote|list|header|link|code|image|delimiter)"/g, '"$1"');
/** Stylize HTML tags */
string = string.replace( /(<[\/a-z]+(>)?)/gi, '$1' );
/** Stylize strings */
string = string.replace( /"([^"]+)"/gi, '"$1"' );
/** Boolean/Null */
string = string.replace( /\b(true|false|null)\b/gi, '$1' );
return string;
}
return module;
})({});