hexo-component-inferno 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/migrate.js +10 -28
- package/lib/core/migrate.test.js +10 -12
- package/lib/core/schema.js +33 -109
- package/lib/core/schema.test.js +2 -6
- package/lib/core/view.js +3 -10
- package/lib/core/view.test.js +0 -7
- package/lib/hexo/filter/locals.js +3 -18
- package/lib/hexo/filter/locals.test.js +2 -19
- package/lib/hexo/generator/assets.js +2 -8
- package/lib/hexo/generator/assets.test.js +0 -4
- package/lib/hexo/generator/categories.test.js +0 -3
- package/lib/hexo/generator/category.js +1 -6
- package/lib/hexo/generator/category.test.js +0 -5
- package/lib/hexo/generator/insight.js +1 -9
- package/lib/hexo/generator/insight.test.js +0 -3
- package/lib/hexo/generator/manifest.js +0 -3
- package/lib/hexo/generator/manifest.test.js +0 -3
- package/lib/hexo/generator/tags.test.js +0 -3
- package/lib/hexo/helper/cdn.js +6 -19
- package/lib/hexo/helper/cdn.test.js +0 -3
- package/lib/hexo/helper/page.test.js +0 -3
- package/lib/hexo/tag/message.js +0 -8
- package/lib/hexo/tag/message.test.js +0 -7
- package/lib/hexo/tag/tabs.js +3 -35
- package/lib/hexo/tag/tabs.test.js +0 -8
- package/lib/util/cache.js +0 -11
- package/lib/util/cache.test.js +0 -4
- package/lib/util/classname.js +0 -3
- package/lib/util/classname.test.js +0 -1
- package/lib/util/yaml.js +2 -7
- package/lib/util/yaml.test.js +0 -4
- package/lib/view/comment/changyan.js +12 -31
- package/lib/view/comment/disqus.js +11 -32
- package/lib/view/comment/disqusjs.js +22 -43
- package/lib/view/comment/facebook.js +9 -28
- package/lib/view/comment/gitalk.js +28 -49
- package/lib/view/comment/gitment.js +15 -37
- package/lib/view/comment/isso.js +6 -27
- package/lib/view/comment/livere.js +6 -27
- package/lib/view/comment/twikoo.js +14 -33
- package/lib/view/comment/utterances.js +17 -45
- package/lib/view/comment/valine.js +39 -60
- package/lib/view/comment/waline.js +45 -66
- package/lib/view/common/article_media.js +11 -28
- package/lib/view/donate/afdian.js +9 -30
- package/lib/view/donate/alipay.js +9 -30
- package/lib/view/donate/buymeacoffee.js +9 -30
- package/lib/view/donate/patreon.js +9 -30
- package/lib/view/donate/paypal.js +10 -31
- package/lib/view/donate/wechat.js +9 -30
- package/lib/view/misc/article_licensing.js +21 -43
- package/lib/view/misc/meta.js +7 -34
- package/lib/view/misc/open_graph.js +25 -67
- package/lib/view/misc/paginator.js +12 -36
- package/lib/view/misc/structured_data.js +16 -43
- package/lib/view/misc/web_app.js +15 -49
- package/lib/view/plugin/baidu_analytics.js +7 -28
- package/lib/view/plugin/bing_webmaster.js +7 -28
- package/lib/view/plugin/busuanzi.js +6 -26
- package/lib/view/plugin/cnzz.js +9 -30
- package/lib/view/plugin/cookie_consent.js +28 -49
- package/lib/view/plugin/gallery.js +10 -31
- package/lib/view/plugin/google_analytics.js +7 -28
- package/lib/view/plugin/hotjar.js +7 -28
- package/lib/view/plugin/katex.js +11 -32
- package/lib/view/plugin/mathjax.js +7 -28
- package/lib/view/plugin/outdated_browser.js +10 -31
- package/lib/view/plugin/progressbar.js +7 -28
- package/lib/view/plugin/statcounter.js +10 -31
- package/lib/view/plugin/twitter_conversion_tracking.js +7 -28
- package/lib/view/search/algolia.js +14 -35
- package/lib/view/search/baidu.js +9 -28
- package/lib/view/search/google_cse.js +10 -30
- package/lib/view/search/insight.js +9 -28
- package/lib/view/share/addthis.js +6 -27
- package/lib/view/share/addtoany.js +6 -25
- package/lib/view/share/bdshare.js +6 -25
- package/lib/view/share/sharejs.js +8 -27
- package/lib/view/share/sharethis.js +6 -27
- package/lib/view/widget/adsense.js +11 -32
- package/lib/view/widget/archives.js +24 -51
- package/lib/view/widget/categories.js +23 -57
- package/lib/view/widget/followit.js +12 -31
- package/lib/view/widget/links.js +10 -32
- package/lib/view/widget/recent_posts.js +14 -36
- package/lib/view/widget/subscribe_email.js +12 -31
- package/lib/view/widget/tags.js +16 -39
- package/lib/view/widget/toc.js +26 -66
- package/package.json +1 -1
package/lib/core/schema.js
CHANGED
|
@@ -3,21 +3,18 @@
|
|
|
3
3
|
* @module core/schema
|
|
4
4
|
*/
|
|
5
5
|
const Ajv = require('ajv');
|
|
6
|
-
|
|
7
6
|
const path = require('path');
|
|
8
|
-
|
|
9
7
|
const yaml = require('../util/yaml');
|
|
8
|
+
|
|
10
9
|
/**
|
|
11
10
|
* A magic string for reformating comment lines in the YAML output.
|
|
12
11
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
12
|
const MAGIC = 'c823d4d4';
|
|
13
|
+
|
|
16
14
|
/**
|
|
17
15
|
* Default value for the primitive types.
|
|
18
16
|
* @access private
|
|
19
17
|
*/
|
|
20
|
-
|
|
21
18
|
const PRIMITIVE_DEFAULTS = {
|
|
22
19
|
null: null,
|
|
23
20
|
boolean: false,
|
|
@@ -27,19 +24,15 @@ const PRIMITIVE_DEFAULTS = {
|
|
|
27
24
|
array: [],
|
|
28
25
|
object: {}
|
|
29
26
|
};
|
|
30
|
-
|
|
31
27
|
const typeOf = value => {
|
|
32
28
|
if (value === undefined) {
|
|
33
29
|
return 'undefined';
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
return Object.prototype.toString.call(value).replace(/^\[object\s+([a-z]+)\]$/i, '$1').toLowerCase();
|
|
37
32
|
};
|
|
38
|
-
|
|
39
33
|
const hasOwnProperty = (obj, prop) => {
|
|
40
34
|
return obj === null || typeOf(obj) === 'undefined' ? false : Object.prototype.hasOwnProperty.call(obj, prop);
|
|
41
35
|
};
|
|
42
|
-
|
|
43
36
|
function traverseObj(obj, targetKey, handler) {
|
|
44
37
|
if (typeOf(obj) === 'array') {
|
|
45
38
|
for (const child of obj) {
|
|
@@ -55,11 +48,10 @@ function traverseObj(obj, targetKey, handler) {
|
|
|
55
48
|
}
|
|
56
49
|
}
|
|
57
50
|
}
|
|
51
|
+
|
|
58
52
|
/**
|
|
59
53
|
* The default value wrapper class.
|
|
60
54
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
55
|
class DefaultValue {
|
|
64
56
|
/**
|
|
65
57
|
* @param {any} value The wrapped default value.
|
|
@@ -70,6 +62,7 @@ class DefaultValue {
|
|
|
70
62
|
this.value = value;
|
|
71
63
|
this.description = description;
|
|
72
64
|
}
|
|
65
|
+
|
|
73
66
|
/**
|
|
74
67
|
* Duplicate the current default value.
|
|
75
68
|
* <p>
|
|
@@ -77,11 +70,8 @@ class DefaultValue {
|
|
|
77
70
|
*
|
|
78
71
|
* @returns {module:core/schema~DefaultValue} A new instance of the current default value.
|
|
79
72
|
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
73
|
clone() {
|
|
83
74
|
const result = new DefaultValue(this.value, this.description);
|
|
84
|
-
|
|
85
75
|
if (result.value instanceof DefaultValue) {
|
|
86
76
|
result.value = result.value.clone();
|
|
87
77
|
} else if (typeOf(result.value) === 'array') {
|
|
@@ -89,45 +79,38 @@ class DefaultValue {
|
|
|
89
79
|
} else if (typeOf(result.value) === 'object') {
|
|
90
80
|
result.value = Object.assign({}, result.value);
|
|
91
81
|
}
|
|
92
|
-
|
|
93
82
|
return result;
|
|
94
83
|
}
|
|
84
|
+
|
|
95
85
|
/**
|
|
96
86
|
* Unwrap current {module:core/schema~DefaultValue} if its wrapped value is also a
|
|
97
87
|
* {module:core/schema~DefaultValue}.
|
|
98
88
|
* <p>
|
|
99
89
|
* The description will also be replaced with wrapped value description, if exists.
|
|
100
90
|
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
91
|
flatten() {
|
|
104
92
|
if (this.value instanceof DefaultValue) {
|
|
105
93
|
this.value.flatten();
|
|
106
|
-
|
|
107
94
|
if (hasOwnProperty(this.value, 'description') && this.value.description) {
|
|
108
95
|
this.description = this.value.description;
|
|
109
96
|
}
|
|
110
|
-
|
|
111
97
|
this.value = this.value.value;
|
|
112
98
|
}
|
|
113
99
|
}
|
|
100
|
+
|
|
114
101
|
/**
|
|
115
102
|
* Merge current default value with another default value.
|
|
116
103
|
*
|
|
117
104
|
* @param {module:core/schema~DefaultValue} source The input object to be merged with. Should have the
|
|
118
105
|
* same wrapped value type as current wrapped default value.
|
|
119
106
|
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
107
|
merge(source) {
|
|
123
108
|
if (hasOwnProperty(source, 'value') && source.value !== null) {
|
|
124
109
|
this.flatten();
|
|
125
|
-
|
|
126
110
|
if (source.value instanceof DefaultValue) {
|
|
127
111
|
source = source.clone();
|
|
128
112
|
source.flatten();
|
|
129
113
|
}
|
|
130
|
-
|
|
131
114
|
if (typeOf(source.value) === typeOf(this.value)) {
|
|
132
115
|
if (typeOf(this.value) === 'array') {
|
|
133
116
|
this.value = this.value.concat(source.value);
|
|
@@ -142,11 +125,11 @@ class DefaultValue {
|
|
|
142
125
|
this.value = source.value;
|
|
143
126
|
}
|
|
144
127
|
}
|
|
145
|
-
|
|
146
128
|
if (hasOwnProperty(source, 'description') && source.description) {
|
|
147
129
|
this.description = source.description;
|
|
148
130
|
}
|
|
149
131
|
}
|
|
132
|
+
|
|
150
133
|
/**
|
|
151
134
|
* Create a new array with comments inserted as new elements right before each child element.
|
|
152
135
|
* <p>
|
|
@@ -158,25 +141,22 @@ class DefaultValue {
|
|
|
158
141
|
*
|
|
159
142
|
* @returns {Array} A new array with comments and the original elements in the commented form.
|
|
160
143
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
144
|
toCommentedArray() {
|
|
164
145
|
return [].concat(...this.value.map(item => {
|
|
165
146
|
if (item instanceof DefaultValue) {
|
|
166
147
|
if (typeOf(item.description) !== 'string' || !item.description.trim()) {
|
|
167
148
|
return [item.toCommented()];
|
|
168
149
|
}
|
|
169
|
-
|
|
170
150
|
return item.description.split('\n').map((line, i) => {
|
|
171
151
|
return MAGIC + i + ': ' + line;
|
|
172
152
|
}).concat(item.toCommented());
|
|
173
153
|
} else if (typeOf(item) === 'array' || typeOf(item) === 'object') {
|
|
174
154
|
return new DefaultValue(item).toCommented();
|
|
175
155
|
}
|
|
176
|
-
|
|
177
156
|
return [item];
|
|
178
157
|
}));
|
|
179
158
|
}
|
|
159
|
+
|
|
180
160
|
/**
|
|
181
161
|
* Create a new object with comments inserted as new properties right before every original properties.
|
|
182
162
|
* <p>
|
|
@@ -184,25 +164,19 @@ class DefaultValue {
|
|
|
184
164
|
*
|
|
185
165
|
* @returns {Object} A new object with comments and the original property values in the commented form.
|
|
186
166
|
*/
|
|
187
|
-
|
|
188
|
-
|
|
189
167
|
toCommentedObject() {
|
|
190
168
|
if (this.value instanceof DefaultValue) {
|
|
191
169
|
return this.value.toCommented();
|
|
192
170
|
}
|
|
193
|
-
|
|
194
171
|
const result = {};
|
|
195
|
-
|
|
196
172
|
for (const key in this.value) {
|
|
197
173
|
const item = this.value[key];
|
|
198
|
-
|
|
199
174
|
if (item instanceof DefaultValue) {
|
|
200
175
|
if (typeOf(item.description) === 'string' && item.description.trim()) {
|
|
201
176
|
item.description.split('\n').forEach((line, i) => {
|
|
202
177
|
result[MAGIC + key + i] = line;
|
|
203
178
|
});
|
|
204
179
|
}
|
|
205
|
-
|
|
206
180
|
result[key] = item.toCommented();
|
|
207
181
|
} else if (typeOf(item) === 'array' || typeOf(item) === 'object') {
|
|
208
182
|
result[key] = new DefaultValue(item).toCommented();
|
|
@@ -210,9 +184,9 @@ class DefaultValue {
|
|
|
210
184
|
result[key] = item;
|
|
211
185
|
}
|
|
212
186
|
}
|
|
213
|
-
|
|
214
187
|
return result;
|
|
215
188
|
}
|
|
189
|
+
|
|
216
190
|
/**
|
|
217
191
|
* Call {@link module:core/schema~DefaultValue#toCommentedArray} or
|
|
218
192
|
* {@link module:core/schema~DefaultValue#toCommentedObject} based on the type of the wrapped value.
|
|
@@ -221,37 +195,30 @@ class DefaultValue {
|
|
|
221
195
|
*
|
|
222
196
|
* @returns {any} The commented object/array, or the original wrapped value.
|
|
223
197
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
226
198
|
toCommented() {
|
|
227
199
|
if (typeOf(this.value) === 'array') {
|
|
228
200
|
return this.toCommentedArray();
|
|
229
201
|
} else if (typeOf(this.value) === 'object') {
|
|
230
202
|
return this.toCommentedObject();
|
|
231
203
|
}
|
|
232
|
-
|
|
233
204
|
return this.value;
|
|
234
205
|
}
|
|
206
|
+
|
|
235
207
|
/**
|
|
236
208
|
* Create the YAML string with all the comments from current default value.
|
|
237
209
|
*
|
|
238
210
|
* @returns {string} The formatted YAML string.
|
|
239
211
|
*/
|
|
240
|
-
|
|
241
|
-
|
|
242
212
|
toYaml() {
|
|
243
213
|
const regex = new RegExp("^(\\s*)(?:-\\s*\\')?" + MAGIC + ".*?:\\s*\\'?(.*?)\\'*$", 'mg');
|
|
244
214
|
return yaml.stringify(this.toCommented()).replace(regex, '$1# $2'); // restore comments
|
|
245
215
|
}
|
|
246
|
-
|
|
247
216
|
}
|
|
248
|
-
/* eslint-disable no-use-before-define */
|
|
249
217
|
|
|
218
|
+
/* eslint-disable no-use-before-define */
|
|
250
219
|
/**
|
|
251
220
|
* A class that can resolving referenced JSON schemas and create {@link module:core/schema~DefaultValue}s.
|
|
252
221
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
222
|
class Schema {
|
|
256
223
|
/**
|
|
257
224
|
* @param {module:core/schema~SchemaLoader} loader JSON schema loader.
|
|
@@ -261,30 +228,27 @@ class Schema {
|
|
|
261
228
|
if (!(loader instanceof SchemaLoader)) {
|
|
262
229
|
throw new Error('loader must be an instance of SchemaLoader');
|
|
263
230
|
}
|
|
264
|
-
|
|
265
231
|
if (typeOf(def) !== 'object') {
|
|
266
232
|
throw new Error('schema definition must be an object');
|
|
267
233
|
}
|
|
268
|
-
|
|
269
234
|
this.loader = loader;
|
|
270
235
|
this.def = def;
|
|
271
236
|
this.compiledSchema = null;
|
|
272
237
|
}
|
|
238
|
+
|
|
273
239
|
/**
|
|
274
240
|
* Validate an object against the JSON schema.
|
|
275
241
|
*
|
|
276
242
|
* @param {any} obj Object to be validated.
|
|
277
243
|
* @returns {boolean|Object} True if Object is valid, or validation errors.
|
|
278
244
|
*/
|
|
279
|
-
|
|
280
|
-
|
|
281
245
|
validate(obj) {
|
|
282
246
|
if (!this.compiledSchema) {
|
|
283
247
|
this.compiledSchema = this.loader.compileValidator(this.def.$id);
|
|
284
248
|
}
|
|
285
|
-
|
|
286
249
|
return this.compiledSchema(obj) ? true : this.compiledSchema.errors;
|
|
287
250
|
}
|
|
251
|
+
|
|
288
252
|
/**
|
|
289
253
|
* Create the {@link module:core/schema~DefaultValue} for an array-typed JSON schema.
|
|
290
254
|
* <p>
|
|
@@ -295,16 +259,14 @@ class Schema {
|
|
|
295
259
|
* @returns {@link module:core/schema~DefaultValue} The {@link module:core/schema~DefaultValue} for the
|
|
296
260
|
* array definition.
|
|
297
261
|
*/
|
|
298
|
-
|
|
299
|
-
|
|
300
262
|
getArrayDefaultValue(def) {
|
|
301
|
-
const defaultValue = new DefaultValue(null, def.description);
|
|
302
|
-
|
|
263
|
+
const defaultValue = new DefaultValue(null, def.description);
|
|
264
|
+
// all array elements have the same type, return the default value for that type
|
|
303
265
|
if (typeOf(def.items) === 'object') {
|
|
304
266
|
const items = Object.assign({}, def.items);
|
|
305
267
|
delete items.oneOf;
|
|
306
|
-
let value = this.getDefaultValue(items);
|
|
307
|
-
|
|
268
|
+
let value = this.getDefaultValue(items);
|
|
269
|
+
// additionally, if each array element can be in one of the provided types in <code>oneOf</code>
|
|
308
270
|
if (typeOf(def.items.oneOf) === 'array') {
|
|
309
271
|
// for each <code>oneOf</code> element, return a {@link module:core/schema~DefaultValue}
|
|
310
272
|
defaultValue.value = def.items.oneOf.map(one => {
|
|
@@ -318,13 +280,12 @@ class Schema {
|
|
|
318
280
|
if (typeOf(value) !== 'array') {
|
|
319
281
|
value = [value];
|
|
320
282
|
}
|
|
321
|
-
|
|
322
283
|
defaultValue.value = value;
|
|
323
284
|
}
|
|
324
285
|
}
|
|
325
|
-
|
|
326
286
|
return defaultValue;
|
|
327
287
|
}
|
|
288
|
+
|
|
328
289
|
/**
|
|
329
290
|
* Create the {@link module:core/schema~DefaultValue} for an object-typed JSON schema.
|
|
330
291
|
* <p>
|
|
@@ -336,27 +297,23 @@ class Schema {
|
|
|
336
297
|
* @returns {@link module:core/schema~DefaultValue} The {@link module:core/schema~DefaultValue} for the
|
|
337
298
|
* object definition.
|
|
338
299
|
*/
|
|
339
|
-
|
|
340
|
-
|
|
341
300
|
getObjectDefaultValue(def) {
|
|
342
301
|
const value = {};
|
|
343
|
-
|
|
344
302
|
if (typeOf(def.properties) === 'object') {
|
|
345
303
|
for (const property in def.properties) {
|
|
346
304
|
value[property] = this.getDefaultValue(def.properties[property]);
|
|
347
305
|
}
|
|
348
306
|
}
|
|
349
|
-
|
|
350
|
-
|
|
307
|
+
const defaultValue = new DefaultValue(value, def.description);
|
|
308
|
+
// only the first one of the possible types will be merged with {@link module:core/schema~DefaultValue}
|
|
351
309
|
// for the <code>properties</code> definition.
|
|
352
|
-
|
|
353
310
|
if (typeOf(def.oneOf) === 'array' && def.oneOf.length) {
|
|
354
311
|
defaultValue.merge(this.getDefaultValue(def.oneOf[0]));
|
|
355
312
|
defaultValue.description = def.description;
|
|
356
313
|
}
|
|
357
|
-
|
|
358
314
|
return defaultValue;
|
|
359
315
|
}
|
|
316
|
+
|
|
360
317
|
/**
|
|
361
318
|
* Create the {@link module:core/schema~DefaultValue} for any typed JSON schema that is not purely defined
|
|
362
319
|
* by its <code>$ref</code>.
|
|
@@ -374,12 +331,9 @@ class Schema {
|
|
|
374
331
|
* definition.
|
|
375
332
|
* @throws {Error} If <code>type</code> is undefined or it is 'array', 'object', or primitive types.
|
|
376
333
|
*/
|
|
377
|
-
|
|
378
|
-
|
|
379
334
|
getTypedDefaultValue(def) {
|
|
380
335
|
let defaultValue;
|
|
381
336
|
const type = typeOf(def.type) === 'array' ? def.type[0] : def.type;
|
|
382
|
-
|
|
383
337
|
if (type === 'array') {
|
|
384
338
|
defaultValue = this.getArrayDefaultValue(def);
|
|
385
339
|
} else if (type === 'object') {
|
|
@@ -392,17 +346,16 @@ class Schema {
|
|
|
392
346
|
}
|
|
393
347
|
} else {
|
|
394
348
|
throw new Error(`Cannot get default value for type ${type}`);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
|
|
349
|
+
}
|
|
350
|
+
// referred default value always get overwritten by its parent default value
|
|
398
351
|
if (hasOwnProperty(def, '$ref') && def.$ref) {
|
|
399
352
|
const refDefaultValue = this.getReferredDefaultValue(def);
|
|
400
353
|
refDefaultValue.merge(defaultValue);
|
|
401
354
|
defaultValue = refDefaultValue;
|
|
402
355
|
}
|
|
403
|
-
|
|
404
356
|
return defaultValue;
|
|
405
357
|
}
|
|
358
|
+
|
|
406
359
|
/**
|
|
407
360
|
* Create the {@link module:core/schema~DefaultValue} for any JSON schema that is purely defined by its
|
|
408
361
|
* <code>$ref</code>.
|
|
@@ -411,21 +364,18 @@ class Schema {
|
|
|
411
364
|
* @returns {@link module:core/schema~DefaultValue} The {@link module:core/schema~DefaultValue} for the
|
|
412
365
|
* definition.
|
|
413
366
|
*/
|
|
414
|
-
|
|
415
|
-
|
|
416
367
|
getReferredDefaultValue(def) {
|
|
417
368
|
const schema = this.loader.getSchema(def.$ref);
|
|
418
|
-
|
|
419
369
|
if (!schema) {
|
|
420
370
|
throw new Error(`Schema ${def.$ref} is not loaded`);
|
|
421
371
|
}
|
|
422
|
-
|
|
423
372
|
const defaultValue = this.getDefaultValue(schema.def);
|
|
424
373
|
defaultValue.merge({
|
|
425
374
|
description: def.description
|
|
426
375
|
});
|
|
427
376
|
return defaultValue;
|
|
428
377
|
}
|
|
378
|
+
|
|
429
379
|
/**
|
|
430
380
|
* Create the {@link module:core/schema~DefaultValue} for any JSON schema.
|
|
431
381
|
*
|
|
@@ -433,43 +383,33 @@ class Schema {
|
|
|
433
383
|
* @returns {@link module:core/schema~DefaultValue} The {@link module:core/schema~DefaultValue} for the
|
|
434
384
|
* definition.
|
|
435
385
|
*/
|
|
436
|
-
|
|
437
|
-
|
|
438
386
|
getDefaultValue(def = null) {
|
|
439
387
|
if (!def) {
|
|
440
388
|
def = this.def;
|
|
441
389
|
}
|
|
442
|
-
|
|
443
390
|
if (hasOwnProperty(def, 'const')) {
|
|
444
391
|
return new DefaultValue(def.const, def.description);
|
|
445
392
|
}
|
|
446
|
-
|
|
447
393
|
if (hasOwnProperty(def, 'default')) {
|
|
448
394
|
return new DefaultValue(def.default, def.description);
|
|
449
395
|
}
|
|
450
|
-
|
|
451
396
|
if (hasOwnProperty(def, 'examples') && typeOf(def.examples) === 'array' && def.examples.length) {
|
|
452
397
|
return new DefaultValue(def.examples[0], def.description);
|
|
453
398
|
}
|
|
454
|
-
|
|
455
399
|
if (hasOwnProperty(def, 'type') && def.type) {
|
|
456
400
|
return this.getTypedDefaultValue(def);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
|
|
401
|
+
}
|
|
402
|
+
// $ref only schemas
|
|
460
403
|
if (hasOwnProperty(def, '$ref') && def.$ref) {
|
|
461
404
|
return this.getReferredDefaultValue(def);
|
|
462
405
|
}
|
|
463
|
-
|
|
464
406
|
throw new Error('The following schema definition must have at least one of the ' + '["const", "default", "examples", "type", "$ref"] fields:\n' + JSON.stringify(def, null, 2));
|
|
465
407
|
}
|
|
466
|
-
|
|
467
408
|
}
|
|
409
|
+
|
|
468
410
|
/**
|
|
469
411
|
* Class for loading JSON schema files from filesystems and creating validator.
|
|
470
412
|
*/
|
|
471
|
-
|
|
472
|
-
|
|
473
413
|
class SchemaLoader {
|
|
474
414
|
constructor() {
|
|
475
415
|
this.schemas = {};
|
|
@@ -477,57 +417,52 @@ class SchemaLoader {
|
|
|
477
417
|
allowUnionTypes: true
|
|
478
418
|
});
|
|
479
419
|
}
|
|
420
|
+
|
|
480
421
|
/**
|
|
481
422
|
* Get JSON schema definition by its <code>$id</code>.
|
|
482
423
|
*
|
|
483
424
|
* @param {string} $id JSON schema <code>$id</code>.
|
|
484
425
|
* @returns {Object} JSON schema definition.
|
|
485
426
|
*/
|
|
486
|
-
|
|
487
|
-
|
|
488
427
|
getSchema($id) {
|
|
489
428
|
return this.schemas[$id];
|
|
490
429
|
}
|
|
430
|
+
|
|
491
431
|
/**
|
|
492
432
|
* Add a JSON schema definition to the collection.
|
|
493
433
|
*
|
|
494
434
|
* @param {Object} JSON schema definition.
|
|
495
435
|
* @throws {Error} If JSON schema definition does not have an <code>$id</code>.
|
|
496
436
|
*/
|
|
497
|
-
|
|
498
|
-
|
|
499
437
|
addSchema(def) {
|
|
500
438
|
if (!hasOwnProperty(def, '$id')) {
|
|
501
439
|
throw new Error('The schema definition does not have an $id field');
|
|
502
440
|
}
|
|
503
|
-
|
|
504
441
|
this.ajv.addSchema(def);
|
|
505
442
|
this.schemas[def.$id] = new Schema(this, def);
|
|
506
443
|
}
|
|
444
|
+
|
|
507
445
|
/**
|
|
508
446
|
* Remove a JSON schema definition to the collection.
|
|
509
447
|
*
|
|
510
448
|
* @param {string} $id JSON schema <code>$id</code>.
|
|
511
449
|
*/
|
|
512
|
-
|
|
513
|
-
|
|
514
450
|
removeSchema($id) {
|
|
515
451
|
this.ajv.removeSchema($id);
|
|
516
452
|
delete this.schemas[$id];
|
|
517
453
|
}
|
|
454
|
+
|
|
518
455
|
/**
|
|
519
456
|
* Create a JSON schema validation function for a given schema identified by its <code>$id</code>.
|
|
520
457
|
*
|
|
521
458
|
* @param {*} $id $id JSON schema <code>$id</code>.
|
|
522
459
|
* @returns {Function} JSON schema validation function.
|
|
523
460
|
*/
|
|
524
|
-
|
|
525
|
-
|
|
526
461
|
compileValidator($id) {
|
|
527
462
|
return this.ajv.compile(this.getSchema($id).def);
|
|
528
463
|
}
|
|
529
|
-
|
|
530
464
|
}
|
|
465
|
+
|
|
531
466
|
/**
|
|
532
467
|
* Create a {@link module:core/schema~SchemaLoader} and load all referred JSON schema from the resolve
|
|
533
468
|
* directories.
|
|
@@ -538,47 +473,36 @@ class SchemaLoader {
|
|
|
538
473
|
* @returns {@link module:core/schema~SchemaLoader} The schema loader with all referred schemas loaded.
|
|
539
474
|
* @throws {Error} Referred schema not found in any of the resolve directories.
|
|
540
475
|
*/
|
|
541
|
-
|
|
542
|
-
|
|
543
476
|
SchemaLoader.load = (rootSchemaDef, resolveDirs = []) => {
|
|
544
477
|
if (typeOf(resolveDirs) !== 'array') {
|
|
545
478
|
resolveDirs = [resolveDirs];
|
|
546
479
|
}
|
|
547
|
-
|
|
548
480
|
resolveDirs.push(path.join(__dirname, '../schema/'));
|
|
549
481
|
const loader = new SchemaLoader();
|
|
550
482
|
loader.addSchema(rootSchemaDef);
|
|
551
|
-
|
|
552
483
|
function handler($ref) {
|
|
553
484
|
if (loader.getSchema($ref)) {
|
|
554
485
|
return;
|
|
555
486
|
}
|
|
556
|
-
|
|
557
487
|
for (const dir of resolveDirs) {
|
|
558
488
|
let def;
|
|
559
|
-
|
|
560
489
|
try {
|
|
561
490
|
def = require(path.join(dir, $ref));
|
|
562
491
|
} catch (e) {
|
|
563
492
|
continue;
|
|
564
493
|
}
|
|
565
|
-
|
|
566
494
|
if (typeOf(def) !== 'object' || def.$id !== $ref) {
|
|
567
495
|
continue;
|
|
568
496
|
}
|
|
569
|
-
|
|
570
497
|
loader.addSchema(def);
|
|
571
498
|
traverseObj(def, '$ref', handler);
|
|
572
499
|
return;
|
|
573
500
|
}
|
|
574
|
-
|
|
575
501
|
throw new Error('Cannot find schema definition ' + $ref + '.\n' + 'Please check if the file exists and its $id is correct');
|
|
576
502
|
}
|
|
577
|
-
|
|
578
503
|
traverseObj(rootSchemaDef, '$ref', handler);
|
|
579
504
|
return loader;
|
|
580
505
|
};
|
|
581
|
-
|
|
582
506
|
module.exports = {
|
|
583
507
|
MAGIC,
|
|
584
508
|
Schema,
|
package/lib/core/schema.test.js
CHANGED
|
@@ -4,7 +4,6 @@ const {
|
|
|
4
4
|
Schema,
|
|
5
5
|
SchemaLoader
|
|
6
6
|
} = require('./schema');
|
|
7
|
-
|
|
8
7
|
const PRIMARY_INPUTS = [null, false, true, 0, 1, '', '1'];
|
|
9
8
|
describe('DefaultValue class tests', () => {
|
|
10
9
|
describe('#clone()', () => {
|
|
@@ -86,7 +85,6 @@ describe('DefaultValue class tests', () => {
|
|
|
86
85
|
const target = new DefaultValue(val1);
|
|
87
86
|
const source = new DefaultValue(val2);
|
|
88
87
|
target.merge(source);
|
|
89
|
-
|
|
90
88
|
if (typeof val1 === typeof val2 || val1 === null) {
|
|
91
89
|
expect(target.value).toBe(val2);
|
|
92
90
|
} else {
|
|
@@ -426,17 +424,15 @@ describe('Schema class tests', () => {
|
|
|
426
424
|
nullable: nullable
|
|
427
425
|
});
|
|
428
426
|
let defaultValue = schema.getTypedDefaultValue(schema.def);
|
|
429
|
-
|
|
430
427
|
if (nullable) {
|
|
431
428
|
expect(defaultValue.value).toBe(null);
|
|
432
429
|
} else {
|
|
433
430
|
expect(defaultValue.value).toBe(defaultValues[i]);
|
|
434
|
-
}
|
|
435
|
-
|
|
431
|
+
}
|
|
436
432
|
|
|
433
|
+
// ref schema does not override default value of primitive types
|
|
437
434
|
schema.$ref = '/child1.json';
|
|
438
435
|
defaultValue = schema.getTypedDefaultValue(schema.def);
|
|
439
|
-
|
|
440
436
|
if (nullable) {
|
|
441
437
|
expect(defaultValue.value).toBe(null);
|
|
442
438
|
} else {
|
package/lib/core/view.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* @module core/view
|
|
4
4
|
*/
|
|
5
5
|
const path = require('path');
|
|
6
|
-
|
|
7
6
|
let themeDir = null;
|
|
7
|
+
|
|
8
8
|
/**
|
|
9
9
|
* Resolve the full path of a given view file name.
|
|
10
10
|
* <p>
|
|
@@ -18,20 +18,16 @@ let themeDir = null;
|
|
|
18
18
|
* or the lib/view/ directory in this directory.
|
|
19
19
|
* @returns {string} Resolved full path of the given view file.
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
21
|
function _resolve(filename) {
|
|
23
22
|
if (typeof themeDir !== 'string') {
|
|
24
23
|
throw new Error('Hexo theme directory is not defined. Please use init(hexo) first.');
|
|
25
24
|
}
|
|
26
|
-
|
|
27
25
|
if (path.isAbsolute(filename)) {
|
|
28
26
|
return require.resolve(filename);
|
|
29
27
|
}
|
|
30
|
-
|
|
31
28
|
const resolved = [path.join(themeDir, '/layout/', filename), path.join(__dirname, '../view/', filename)].find(filepath => {
|
|
32
29
|
try {
|
|
33
30
|
require.resolve(filepath);
|
|
34
|
-
|
|
35
31
|
return true;
|
|
36
32
|
} catch (e) {
|
|
37
33
|
return false;
|
|
@@ -39,6 +35,7 @@ function _resolve(filename) {
|
|
|
39
35
|
});
|
|
40
36
|
return require.resolve(resolved ? resolved : filename);
|
|
41
37
|
}
|
|
38
|
+
|
|
42
39
|
/**
|
|
43
40
|
* Resolve and require a view module.
|
|
44
41
|
*
|
|
@@ -49,13 +46,11 @@ function _resolve(filename) {
|
|
|
49
46
|
* @param {string} filename The file path of the view to be required.
|
|
50
47
|
* @returns {Object} The view module.
|
|
51
48
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
49
|
function _require(filename) {
|
|
55
50
|
return require(_resolve(filename));
|
|
56
51
|
}
|
|
57
|
-
|
|
58
52
|
_require.resolve = _resolve;
|
|
53
|
+
|
|
59
54
|
/**
|
|
60
55
|
* Initialize module global variables, including the theme directory variable.
|
|
61
56
|
* <p>
|
|
@@ -64,11 +59,9 @@ _require.resolve = _resolve;
|
|
|
64
59
|
* @static
|
|
65
60
|
* @param {string} hexo Hexo instance.
|
|
66
61
|
*/
|
|
67
|
-
|
|
68
62
|
function init(hexo) {
|
|
69
63
|
themeDir = hexo.theme_dir;
|
|
70
64
|
}
|
|
71
|
-
|
|
72
65
|
module.exports = {
|
|
73
66
|
init,
|
|
74
67
|
require: _require
|
package/lib/core/view.test.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
-
|
|
3
2
|
const view = require('./view');
|
|
4
|
-
|
|
5
3
|
const DisqusLibraryView = require('../view/comment/disqus');
|
|
6
|
-
|
|
7
4
|
const OpenGraphLibraryView = require('../view/misc/open_graph');
|
|
8
|
-
|
|
9
5
|
const themeDir = path.join(__dirname, '../../fixture');
|
|
10
|
-
|
|
11
6
|
const ExampleFixtureView = require(themeDir + '/layout/example');
|
|
12
|
-
|
|
13
7
|
const DisqusFixtureView = require(themeDir + '/layout/comment/disqus');
|
|
14
|
-
|
|
15
8
|
beforeAll(() => {
|
|
16
9
|
view.init({
|
|
17
10
|
theme_dir: themeDir
|