-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjson-formatter.js
206 lines (155 loc) · 6.76 KB
/
json-formatter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
$(document).ready(function() {
//formatJSON();
});
function formatJSON() {
console.log("okay formatting now");
console.log($("body"));
if ($("#dev-info").length > 0) {
// test if content is json
if (jsonChecker($("#dev-info").text())) {
init("#dev-info");
}
}
//check if string is valid json
function jsonChecker(string) {
if (/^[\],:{}\s]*$/.test(string.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
console.log("valid json");
return true;
} else {
console.log("json not detected");
return false;
}
}
function init(ele) {
//get the original and re-parse as JSON
//var json = $.parseJSON($(ele).text());
var json = $.parseJSON($(ele).text());
//re enconded as string with 4 spaces TAB.
$(ele).text(JSON.stringify(json, undefined, 4));
json = $(ele).text(); //update json var to parse in case its in a pre element(it will be removed).
//if it's in a pre element swap for a div
if ("pre".indexOf(ele)) {
$("body").addClass("json-formatted");
$(ele).remove();
} else {
$(ele).addClass("json-formatted");
}
//final format
$(".json-formatted").html(syntaxHighlight(json));
//attach events
$(".json-formatted").find(".minimize-me").click(function() {
$(this).parent("ul").toggleClass("minimized");
});
copySnippetInit(".json-formatted");
}
function copySnippetInit(ele) {
//attach event
$(ele + " .key").click(function() {
//reset active nodes
$(ele + " .active").removeClass("active");
//activate nodes
$(this).parents("li").addClass("active");
var snippet = generateSnippet(ele);
$(this).attr("data-clipboard-text", snippet);
copy(snippet);
});
//copy value
$(ele + " .number," + ele + " .string").click(function() {
//console.log($(this).text() );
copy(String($(this).text()));
$(this).attr("data-clipboard-text", $(this).text());
});
}
function copy(string) {
var clipboard = new ClipboardJS(".key, .number, .string", {
text: function() {
return string;
}
});
}
function generateSnippet(ele) {
var snippet = "{{ ";
var separator = "";
var option = "for";
$(ele + " .active").each(function(index, value) {
//index == number of iterations
//ul parent
$parent = $(this).closest("ul");
if ($parent.hasClass("array")) {
if (index > 0) {
separator = ".";
}
snippet += separator + $(this).find("> .key").text();
} else if ($parent.hasClass("list")) {
snippet += "[" + $parent.find("> .active").index() + "]";
}
});
snippet += " }}";
snippet = snippet.replace(/"/g, "").replace(/:/g, "");
//console.log(snippet);
return snippet;
}
function syntaxHighlight(json) {
//if I get the val from the DOM need to replace spaces and line breaks too
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '" data-clipboard-text="">' + match + '</span>';
});
//add separator between each 8 spaces
json = json.replace(/ +/g, ' <span class="separator"></span> ');
//match new lines
json = json.replace(/(?<!}|\])\n+/g, '\n<li>');
//match end lines
json = json.replace(/(?<!}|\])\r+/g, '</li>\r');
//open & close container for each array
/*look for { not followed by } same for [ ] but scapped with \ because its a special char. */
json = json
//.replace( /{(?!})/g, '<ul class="array"><li class="minimize-me"><li class="open-bracket">{</li>')
//.replace( /\[(?!])/g, '<ul class="list"><li class="minimize-me"></li><li class="open-bracket">[</li>');
//test with select open bracket only followed with new line \n
.replace(/{\n/g, '<ul class="array"><li class="minimize-me"><li class="open-bracket">{</li>')
.replace(/\[\n/g, '<ul class="list"><li class="minimize-me"></li><li class="open-bracket">[</li>')
//look for close elements if they are not prev. by its opener, so they ignore {}
//.replace( /(?<!{)}(?!,)/g, '<li class="close-bracket">}</li></ul>')
//.replace( /(?<!\[)\](?!,)/g, '<li class="close-bracket">]</li></ul>')
//look for }, or ], and close the ul after the comma.
//.replace( /(?<!{)},/g, '<li class="close-bracket">},</li></ul>')
//.replace( /(?<!\[)\],/g, '<li class="close-bracket">],</li></ul>')
//for special cases where there are two commas (end of array at the end of a list etc.)
.replace(/(?<!{)},,/g, '<li class="close-bracket">},</li><br>,</ul>')
.replace(/(?<!\[)\],,/g, '<li class="close-bracket">],</li><br>,</ul>')
//TEST selecting closing tags with and without comma followed by new line
.replace(/(?<!{)},\n/g, '<li class="close-bracket">},</li></ul>')
.replace(/(?<!\[)\],\n/g, '<li class="close-bracket">],</li></ul>')
.replace(/(?<!{)}\n/g, '<li class="close-bracket">}</li></ul>')
.replace(/(?<!\[)\]\n/g, '<li class="close-bracket">]</li></ul>')
return json;
}
}
// Set up context menu at install time.
/*
chrome.runtime.onInstalled.addListener(function() {
console.log("listener added.");
var context = "page";
var title = "Format JSON";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context,
"onclick": formatJSON()
});
});
*/
// add click event
//console.log("adding click event listener");
//chrome.contextMenus.onClicked.addListener(formatJSON());