-
Notifications
You must be signed in to change notification settings - Fork 602
/
cdn.js
137 lines (130 loc) · 4.36 KB
/
cdn.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
const { version } = require('../../../package.json');
const theme_version = version;
const path = require('path')
const site_root = hexo.config.root;
// _cdn.yml
const cdn_info = hexo.render.renderSync({ path: path.join(hexo.theme_dir, '/_cdn.yml'), engine: 'yaml' })
function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
function merge(target, source) {
for (const key in source) {
if (isObject(target[key]) && isObject(source[key])) {
merge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
// volantis_cdn_system_prefix
// 在本配置文件中 匹配以 "volantis-local/npm/static/cdnjs" 开头的链接路径替换为该格式的前缀开头 prefix
function volantis_cdn_system_prefix(source, hexo) {
const prefix_list = ["local", "npm", "static", "cdnjs"];
for (const key in source) {
if (isObject(source[key])) {
volantis_cdn_system_prefix(source[key], hexo);
} else if (source[key] && typeof source[key] == "string") {
for (const prefix of prefix_list) {
if (source[key].match(new RegExp(`^volantis-${prefix}\/`, "g"))) {
const my_prefix = hexo.theme.config.cdn_system.system_config[prefix].prefix.replace(/\$\{site_root\}/g, site_root)
source[key] = my_prefix + source[key].replace(new RegExp(`^volantis-${prefix}`, "g"), "")
break;
}
}
} else if (source[key] && Array.isArray(source[key]) && source[key].length > 0) {
source[key].forEach((item, index) => {
if (item && typeof item == "string") {
for (const prefix of prefix_list) {
if (item.match(new RegExp(`^volantis-${prefix}\/`, "g"))) {
const my_prefix = hexo.theme.config.cdn_system.system_config[prefix].prefix.replace(/\$\{site_root\}/g, site_root)
source[key][index] = my_prefix + item.replace(new RegExp(`^volantis-${prefix}`, "g"), "")
break;
}
}
} else if (isObject(item)) {
volantis_cdn_system_prefix(item, hexo);
}
})
}
}
}
const minFile = (file) => {
return file.replace(/(?<!\.min)\.(js|css)$/g, ext => '.min' + ext)
}
// 匹配 CDN 资源
function match_cdn_source(key) {
if (key && cdn_info[key]) {
// _cdn.yml item
const info = cdn_info[key];
const system_config = hexo.theme.config.cdn_system.system_config;
for (const iterator of system_config.priority) {
// priority item
if (iterator === "custom") {
if (system_config.custom) {
const r = system_config.custom[info.name];
if (r) {
return r;
}
}
} else {
if (info[iterator] === true) {
const prefix = system_config[iterator].prefix?.replace(/\$\{site_root\}/g, site_root);
const format = system_config[iterator].format;
let name = info.name;
let file = info.file;
if (iterator === "cdnjs") {
if (info.cdnjs_name) {
name = info.cdnjs_name;
}
if (info.cdnjs_file) {
file = info.cdnjs_file;
} else {
file = file.replace(/^[lib|dist]*\/|browser\//g, '');
}
}
if (info.local === true) {
file = file.replace(/^source\//g, '')
}
let min_file = minFile(file)
if (iterator === "cdnjs") {
if (info.cdnjs_no_min_file) {
min_file = file;
}
}
let version = info.version;
if (info.local === true) {
version = theme_version
}
const value = {
version,
name,
file,
min_file,
prefix,
}
return format?.replace(/\$\{(.+?)\}/g, (match, $1) => value[$1]) || null
}
}
}
}
return null;
}
// 收集 CDN 资源
function collect_cdn_source() {
hexo.theme.config.cdn = {}
Object.keys(cdn_info).forEach(e => {
hexo.theme.config.cdn[e] = match_cdn_source(e)
})
if(hexo.theme.config.debug)
console.log(hexo.theme.config.cdn);
}
hexo.on('generateBefore', () => {
volantis_cdn_system_prefix(hexo.theme.config, hexo);
// 可以在 source/_data/cdn.yml 覆盖 theme/_cdn.yml
const data = hexo.locals.get('data');
if (data.cdn) {
merge(cdn_info, data.cdn);
}
collect_cdn_source();
});