-
Notifications
You must be signed in to change notification settings - Fork 4
/
json.js
91 lines (77 loc) · 2.12 KB
/
json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* global location */
const escapeHtml = require('escape-html')
const renderPage = require('./template.js')
const URL_REGEX = /(^\w+:|\.\/|\.\.\/|\/)[^\s<>"]+$/
// Might only work on Chromium
const text = document.querySelector('pre').innerText
const parsed = JSON.parse(text)
const rendered = render(parsed)
const title = location.href
const content = `
<style>
ul {
padding: 0.5em;
}
dt {
color: var(--ag-theme-primary);
}
span {
color: var(--ag-theme-secondary);
}
li {
list-style: none;
padding-left: 1em;
}
dl {
margin: 0px;
}
</style>
${rendered}
`
renderPage(content, title)
function render (json, suffix = '') {
if (Array.isArray(json)) {
const values = json.map((value, index) => {
const isLast = index === (json.length - 1)
const suffix = isLast ? '' : ','
return `<li>${render(value, suffix)}</li>`
}).join('\n')
return `<ul>
<span>[</span>
${values}
<span>]</span>
</ul>`
} else if (typeof json === 'object' && json !== null) {
const keys = Object.keys(json)
// Special case for IPLD dag-json data with links
const isCID = keys.length === 1 && keys[0] === '/' && (typeof json['/'] === 'string')
const values = Object.keys(json).map((key, index) => {
const value = json[key]
const isLast = index === (keys.length - 1)
const suffix = isLast ? '' : ','
const renderedKey = escapeHtml(JSON.stringify(key))
const renderedValue = isCID ? makeLink(makeIPLDLink(value), suffix, value) : render(value, suffix)
return `<dt>${renderedKey}:</dt><dd>${renderedValue}</dd>`
}).join('\n')
return `<dl>
<span>{</span>
${values}
<span>}</span>${suffix}
</dl>`
} else {
if (isURL(json)) {
return makeLink(json, suffix)
}
const escaped = escapeHtml(JSON.stringify(json))
return `${escaped}${suffix}`
}
}
function isURL (value) {
return (typeof value === 'string') && value.match(URL_REGEX)
}
function makeLink (url, suffix, value = url) {
return `"<a href="${url}">${escapeHtml(value)}</a>"${suffix}`
}
function makeIPLDLink (cid) {
return `ipld://${cid}/`
}