2017-10-12 00:01:37 +03:00
|
|
|
let level = 1;
|
2016-01-24 00:35:44 +03:00
|
|
|
const COLLAPSE_LEVEL = 2;
|
|
|
|
|
2017-10-12 00:01:37 +03:00
|
|
|
export function jsonToHTML(json) {
|
|
|
|
level = 1;
|
|
|
|
var output = '';
|
|
|
|
output += '<div class="redoc-json">';
|
|
|
|
output += valueToHTML(json);
|
|
|
|
output += '</div>';
|
|
|
|
return output;
|
2016-01-24 00:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function htmlEncode(t) {
|
2017-10-12 00:01:37 +03:00
|
|
|
return t != undefined
|
|
|
|
? t
|
|
|
|
.toString()
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
: '';
|
2016-01-24 00:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function decorateWithSpan(value, className) {
|
|
|
|
return '<span class="' + className + '">' + htmlEncode(value) + '</span>';
|
|
|
|
}
|
|
|
|
|
|
|
|
function valueToHTML(value) {
|
2017-10-12 00:01:37 +03:00
|
|
|
var valueType = typeof value,
|
|
|
|
output = '';
|
2016-06-13 20:54:24 +03:00
|
|
|
if (value == undefined) {
|
2016-01-24 00:35:44 +03:00
|
|
|
output += decorateWithSpan('null', 'type-null');
|
2016-06-13 20:54:24 +03:00
|
|
|
} else if (value && value.constructor === Array) {
|
2016-01-24 00:35:44 +03:00
|
|
|
level++;
|
|
|
|
output += arrayToHTML(value);
|
|
|
|
level--;
|
2017-08-01 19:16:17 +03:00
|
|
|
} else if (value && value.constructor === Date) {
|
|
|
|
output += decorateWithSpan('"' + value.toISOString() + '"', 'type-string');
|
2016-06-13 20:54:24 +03:00
|
|
|
} else if (valueType === 'object') {
|
2016-01-24 00:35:44 +03:00
|
|
|
level++;
|
|
|
|
output += objectToHTML(value);
|
|
|
|
level--;
|
2016-06-13 20:54:24 +03:00
|
|
|
} else if (valueType === 'number') {
|
2016-01-24 00:35:44 +03:00
|
|
|
output += decorateWithSpan(value, 'type-number');
|
2016-06-13 20:54:24 +03:00
|
|
|
} else if (valueType === 'string') {
|
2017-05-24 10:24:37 +03:00
|
|
|
if (/^(http|https):\/\/[^\s]+$/.test(value)) {
|
2017-10-12 00:01:37 +03:00
|
|
|
output +=
|
|
|
|
decorateWithSpan('"', 'type-string') +
|
|
|
|
'<a href="' +
|
|
|
|
value +
|
|
|
|
'">' +
|
|
|
|
htmlEncode(value) +
|
|
|
|
'</a>' +
|
2016-06-13 20:54:24 +03:00
|
|
|
decorateWithSpan('"', 'type-string');
|
2016-01-24 00:35:44 +03:00
|
|
|
} else {
|
|
|
|
output += decorateWithSpan('"' + value + '"', 'type-string');
|
|
|
|
}
|
|
|
|
} else if (valueType === 'boolean') {
|
|
|
|
output += decorateWithSpan(value, 'type-boolean');
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayToHTML(json) {
|
|
|
|
var collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
|
|
|
|
var i, length;
|
2017-10-12 00:01:37 +03:00
|
|
|
var output =
|
|
|
|
'<div class="collapser"></div>[<span class="ellipsis"></span><ul class="array collapsible">';
|
2016-01-24 00:35:44 +03:00
|
|
|
var hasContents = false;
|
|
|
|
for (i = 0, length = json.length; i < length; i++) {
|
|
|
|
hasContents = true;
|
|
|
|
output += '<li><div class="hoverable ' + collapsed + '">';
|
|
|
|
output += valueToHTML(json[i]);
|
|
|
|
if (i < length - 1) {
|
|
|
|
output += ',';
|
|
|
|
}
|
|
|
|
output += '</div></li>';
|
|
|
|
}
|
|
|
|
output += '</ul>]';
|
|
|
|
if (!hasContents) {
|
|
|
|
output = '[ ]';
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function objectToHTML(json) {
|
|
|
|
var collapsed = level > COLLAPSE_LEVEL ? 'collapsed' : '';
|
2017-10-12 00:01:37 +03:00
|
|
|
var i,
|
|
|
|
key,
|
|
|
|
length,
|
|
|
|
keys = Object.keys(json);
|
|
|
|
var output =
|
|
|
|
'<div class="collapser"></div>{<span class="ellipsis"></span><ul class="obj collapsible">';
|
2016-01-24 00:35:44 +03:00
|
|
|
var hasContents = false;
|
|
|
|
for (i = 0, length = keys.length; i < length; i++) {
|
|
|
|
key = keys[i];
|
|
|
|
hasContents = true;
|
|
|
|
output += '<li><div class="hoverable ' + collapsed + '">';
|
2017-10-12 00:01:37 +03:00
|
|
|
output += '<span class="property">"' + htmlEncode(key) + '"</span>: ';
|
2016-01-24 00:35:44 +03:00
|
|
|
output += valueToHTML(json[key]);
|
|
|
|
if (i < length - 1) {
|
|
|
|
output += ',';
|
|
|
|
}
|
|
|
|
output += '</div></li>';
|
|
|
|
}
|
|
|
|
output += '</ul>}';
|
|
|
|
if (!hasContents) {
|
|
|
|
output = '{ }';
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|