-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
44 lines (41 loc) · 1.07 KB
/
utils.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
'use strict';
const { mergeWith } = require('lodash');
const { join } = require('path');
const { readFileSync } = require('fs');
const { Cache } = require('hexo-util');
const cache = new Cache();
const utils = {
htmlTag: (tag, attrs, text) => {
let attrText = '';
if (attrs) {
attrText = ' ' + Object.keys(attrs).map(key => `${key}="${attrs[key]}"`).join(' ');
}
let end = '/>';
if (text) {
end = `>${text}</${tag}>`;
}
return `<${tag}${attrText} ${end}`;
},
copy: (object, sources) => {
return mergeWith(object, sources, (objValue, srcValue) => {
if (Array.isArray(objValue)) {
return srcValue;
}
});
},
readJsonFile: path => {
return cache.apply(path, () => {
const data = readFileSync(path, 'utf-8');
return JSON.parse(data);
});
},
loadUtil: hexo => {
const deps = utils.readJsonFile(join(hexo.base_dir, 'package.json')).dependencies;
return name => {
if (!deps[name]) {
return hexo.loadPlugin(hexo.resolvePlugin(name));
}
};
}
};
module.exports = utils;