-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
341 lines (325 loc) · 13.6 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
(function () {
'use strict';
/*!
# consolemd -- echomd conversion tool for browsers and console
#
# echomd <https://github.com/WebReflection/echomd>
#
# Fully inspired by the work of John Gruber
# <http://daringfireball.net/projects/markdown/>
#
# ────────────────────────────────────────────────────────────────────────
# The MIT License (MIT)
# Copyright (c) 2016 Andrea Giammarchi - @WebReflection
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
# THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ────────────────────────────────────────────────────────────────────────
*/
for (var
isNodeJS = typeof process === 'object' && !process.browser,
parse = isNodeJS ?
// on NodeJS simply fallback to echomd
(function (echomd, map) {
function parse(value) {
return typeof value === 'string' ?
echomd(value) : value;
}
return function () {
return map.call(arguments, parse);
};
}(require('echomd').raw, [].map)) :
// on browsers implement some %cmagic%c
// The current algorithm is based on two passes:
// 1. collect all info ordered by string index
// 2. transform surrounding with special %c chars
// Info are grouped together whenever is possible
// since the console does not support one style per %c
(function () {
return function (txt) {
var
code = (Object.create || Object)(null),
multiLineCode = transform.multiLineCode.re,
singleLineCode = transform.singleLineCode.re,
storeAndHide = function ($0, $1, $2, $3) {
$3 = $3.replace(/%c/g, '%%c');
return $1 + $2 + (code[$3] = md5Base64($3)) + $2;
},
restoreHidden = function ($0, $1, $2, $3) {
return $1 + '%c' + getSource($3, code) + '%c';
},
out = [],
args, i, j, length, css, key, re
;
// match and hide possible code (which should not be parsed)
match(txt, 'multiLineCode', out);
txt = txt.replace(multiLineCode, storeAndHide);
match(txt, 'singleLineCode', out);
txt = txt.replace(singleLineCode, storeAndHide);
// find all special cases preserving the order
// in which are these found
match(txt, 'header2', out);
match(txt, 'header1', out);
match(txt, 'bold', out);
match(txt, 'underline', out);
match(txt, 'italic', out);
match(txt, 'strike', out);
match(txt, 'color', out);
// transform using all info
// - - - or ___ or * * * with or without space in between
txt = txt.replace(/^[ ]{0,2}([ ]?[*_-][ ]?){3,}[ \t]*$/gm, line);
// ## Header
txt = replace(txt, 'header2');
// # Header
txt = replace(txt, 'header1');
// *bold* =italic= _underline_ ~strike~
txt = replace(txt, 'bold');
txt = replace(txt, 'underline');
txt = replace(txt, 'italic');
txt = replace(txt, 'strike');
// * list bullets
txt = txt.replace(/^([ \t]{2,})[*+-]([ \t]{1,})/gm, '$1•$2');
// > simple quotes
txt = txt.replace(/^[ \t]*>([ \t]?)/gm, function ($0, $1) {
return Array($1.length + 1).join('▌') + $1;
});
// #RGBA(color) and !#RGBA(background-color)
txt = replace(txt, 'color');
// cleanup duplicates
txt = txt.replace(/(%c)+/g, '%c');
// put back code
txt = txt.replace(singleLineCode, restoreHidden);
txt = txt.replace(multiLineCode, restoreHidden);
// create list of arguments to style the console
args = [txt];
length = out.length;
for (i = 0; i < length; i++) {
css = '';
key = '';
// group styles by type (start/end)
for (j = i; j < length; j++) {
i = j; // update the i to move fast-forward
if (j in out) {
// first match or same kind of operation (start/end)
if (!key || (key === out[j].k)) {
key = out[j].k;
css += out[j].v;
} else {
i--; // if key changed, next loop should update
break;
}
}
}
if (css) args.push(css);
}
return args;
};
}())
,
line = Array(33).join('─'),
// just using same name used in echomd, not actual md5
md5Base64 = function (txt) {
for (var out = [], i = 0; i < txt.length; i++) {
out[i] = txt.charCodeAt(i).toString(32);
}
return out.join('').slice(0, txt.length);
},
getSource = function (hash, code) {
for (var source in code) {
if (code[source] === hash) {
return source;
}
}
},
commonReplacer = function ($0, $1, $2, $3) {
return '%c' + $2 + $3 + '%c';
},
match = function (txt, what, stack) {
var info = transform[what], i, match;
while (match = info.re.exec(txt)) {
i = match.index;
stack[i] = {
k: 'start',
v: typeof info.start === 'string' ?
info.start : info.start(match)
};
i = i + match[0].length - 1;
stack[i] = {
k: 'end',
v: typeof info.end === 'string' ?
info.end : info.end(match)
};
}
},
replace = function (txt, what) {
var info = transform[what];
return txt.replace(info.re, info.place);
},
transform = {
bold: {
re: /(\*{1,2})(?=\S)(.*?)(\S)\1/g,
place: commonReplacer,
start: 'font-weight:bold;',
end: 'font-weight:default;'
},
header1: {
re: /^(\#[ \t]+)(.+?)[ \t]*\#*([\r\n]+|$)/gm,
place: commonReplacer,
start: 'font-weight:bold;font-size:1.6em;',
end: 'font-weight:default;font-size:default;'
},
header2: {
re: /^(\#{2,6}[ \t]+)(.+?)[ \t]*\#*([\r\n]+|$)/gm,
place: commonReplacer,
start: 'font-weight:bold;font-size:1.3em;',
end: 'font-weight:default;font-size:default;'
},
italic: {
re: /(={1})(?=\S)(.*?)(\S)\1/g,
place: commonReplacer,
start: 'font-style: italic;',
end: 'font-style: unset;'
},
underline: {
re: /(_{2})(?=\S)(.*?)(\S)\1/g,
place: commonReplacer,
start: 'border-bottom:1px solid;',
end: 'border-bottom:default;'
},
strike: {
re: /(~{1,2})(?=\S)(.*?)(\S)\1/g,
place: commonReplacer,
start: 'text-decoration:line-through;',
end: 'text-decoration:default;'
},
multiLineCode: {
re: /(^|[^\\])(`{2,})([\s\S]+?)\2(?!`)/g,
start: 'font-family:monospace;',
end: 'font-family:default;'
},
singleLineCode: {
re: /(^|[^\\])(`)(.+?)\2/gm,
start: 'font-family:monospace;',
end: 'font-family:default;'
},
color: {
re: /(!?)#([a-zA-Z0-9]{3,8})\((.+?)\)(?!\))/g,
place: function ($0, bg, rgb, txt) {
return '%c' + txt + '%c';
},
start: function (match) {
return (match[1] ? 'background-' : '') + 'color:' +
(/^[a-fA-F0-9]{3,8}$/.test(match[2]) ? '#' : '') +
match[2] + ';';
},
end: function (match) {
return (match[1] ? 'background-' : '') + 'color:initial;';
}
}
},
// 'error', 'info', 'log', 'warn' are overwritten
// it is possible to use original method at any time
// simply accessing console.methodName.raw( ... ) instead
overwrite = function (method) {
var original = console[method];
if (original) (consolemd[method] = isNodeJS ?
function () {
return original.apply(console, parse.apply(null, arguments));
} :
function () {
if (arguments.length === 1 && typeof arguments[0] === 'string') {
var result = parse(arguments[0]);
result[0] = `%c${result[0]}`;
var defaultStyle = 'font-size: 16px; font-family: sans-serif; font-weight: 300; line-height: 1.6;';
for (var i = 1; i < result.length; i++) {
result[i] = `${defaultStyle}${result[i]}`
}
result.splice(1, 0, defaultStyle)
return original.apply(console, result);
} else {
return original.apply(console, arguments);
}
}).raw = function () {
return original.apply(console, arguments);
};
},
consolemd = {},
methods = ['error', 'info', 'log', 'warn'],
key,
i = 0; i < methods.length; i++
) {
overwrite(methods[i]);
}
// if this is a CommonJS module
try {
// export consolemd fake object
module.exports = consolemd;
overwrite = function (original) {
return function () {
return original.apply(console, arguments);
};
};
for (key in console) {
if (!consolemd.hasOwnProperty(key)) {
consolemd[key] = overwrite(console[key]);
}
}
} catch (e) {
// otherwise replace global console methods
for (i = 0; i < methods.length; i++) {
key = methods[i];
if (!console[key].raw) {
console[key] = consolemd[key];
}
}
}
}());
/* Big thanks to Andrea Giammarchi for his console.md library */
console.blog = function (title, message, date, by) {
var titleStyles = `
font-size: 24px;
line-height: 2;
font-family: sans-serif;
font-weight: 700;
display: inline-block;
color: #2c3e50;
`;
var dateStyles = `
font-size: 12px;
font-family: sans-serif;
color: #3300ff;
line-height: 2;
font-weight: 300;
`
var byStyles = `
font-size: 12px;
font-family: sans-serif;
color: #0066cc;
line-height: 2;
font-weight: 300;
`
console.log("------------------------------");
console.log(`%c${title}`, titleStyles);
if (date) {
console.log(`%c${date}`, dateStyles);
}
if (by) {
console.log(`by %c${by}`, byStyles);
}
console.log(`${message}`);
}