This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 471
/
index.js
executable file
·248 lines (180 loc) · 6.94 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
/**
* @description The live2d-widget generator for hexo
*/
/* global hexo */
const _ = require('lodash');
const fs = require('hexo-fs');
const path = require('path');
const url = require('url');
const buildGeneratorsFromManifest = require('./lib/buildGeneratorsFromManifest');
const getFileMD5 = require('./lib/getFileMD5');
const getNodeModulePath = require('./lib/getNodeModulePath');
const loadModelFrom = require('./lib/loadModelFrom');
const print = require('./lib/print');
const generators = [];
const manifest = require('live2d-widget/lib/manifest');
const mainfestPath = require.resolve('live2d-widget/lib/manifest');
const coreScriptName = manifest['main.js'];
const thisPkgInfo = require('./package');
const widgetVer = thisPkgInfo.dependencies['live2d-widget'];
const localWidgetVer = require(path.resolve(require.resolve('live2d-widget'), '../../', 'package')).version;
const blogRoot = hexo.config.root || '/';
const defaultConfig = {
'enable': true,
'log': false,
'pluginJsPath': 'lib/',
'pluginModelPath': 'assets/',
'pluginRootPath': 'live2dw/',
'scriptFrom': 'local',
'tagMode': false,
};
// Apply options with default
let config = _.defaultsDeep({
}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
/**
* Get entry point script URL according to type of source
* @param {String} scriptFrom The type of source
* @return {String} URL of entry point
*/
function getScriptURL (scriptFrom) { // eslint-disable-line max-lines-per-function
if (config.log) {
print.log(`hexo-helper-live2d@${thisPkgInfo.version}, using live2d-widget@${widgetVer}.`);
}
switch (scriptFrom) {
case 'local': {
/*
* Is local(1)
* Use local
*/
if (config.log) {
print.log(`use local live2d-widget@${localWidgetVer}`);
}
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), `${config.pluginRootPath}${config.pluginJsPath}`);
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
generators.push(...scriptGenerators);
return `${blogRoot}${url.resolve(`${config.pluginRootPath}${config.pluginJsPath}`, coreScriptName)}?${useHash}`;
}
case 'jsdelivr':
/*
* Is jsdelivr online CDN(2)
* Use jsdelivr
*/
return `https://cdn.jsdelivr.net/npm/live2d-widget@${widgetVer}/lib/${coreScriptName}`;
case 'unpkg':
/*
* Is unpkg online CDN(3)
* Use unpkg
*/
return `https://unpkg.com/live2d-widget@${widgetVer}/lib/${coreScriptName}`;
default:
/*
* Is custom(4)
* Use custom
*/
return scriptFrom;
}
}
if (config.enable) {
_.unset(config, 'enable');
if (_.hasIn(config, 'model.use')) {
let modelJsonUrl = null;
let tryPath = path.resolve(hexo.base_dir, './live2d_models/', config.model.use);
if (fs.existsSync(tryPath)) { // eslint-disable-line no-sync
/*
* Is in live2d_models(2)
* LoadModelFrom
*/
const {
modelGenerators,
'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(tryPath, `${config.pluginRootPath}${config.pluginModelPath}`);
modelJsonUrl = `${blogRoot}${pkgModelJsonUrl}`;
generators.push(...modelGenerators);
if (config.log) {
print.log(`Loaded model from live2d_models folder(2), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
}
} else {
tryPath = path.resolve(hexo.base_dir, config.model.use);
if (fs.existsSync(tryPath)) { // eslint-disable-line no-sync
/*
* Is in hexo base releated path(3)
* LoadModelFrom
*/
const {
modelGenerators,
'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(tryPath, `${config.pluginRootPath}${config.pluginModelPath}`);
modelJsonUrl = `${blogRoot}${pkgModelJsonUrl}`;
generators.push(...modelGenerators);
if (config.log) { // eslint-disable-line max-depth
print.log(`Loaded model from hexo base releated path(3), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
}
} else if (getNodeModulePath(config.model.use) === null) {
/*
* Is custom(4)
* Use custom
*/
modelJsonUrl = config.model.use;
if (config.log) { // eslint-disable-line max-depth
print.log(`Loaded Model from custom(4), at '${modelJsonUrl}'`);
}
} else {
/*
* Is npm-module(1)
* Convert path to assets folder
* LoadModelFrom
*/
const packageJsonPath = path.resolve(getNodeModulePath(config.model.use), 'package.json');
const packageJsonObj = require(packageJsonPath); // eslint-disable-line global-require
const assetsDir = path.resolve(getNodeModulePath(config.model.use), './assets/');
const {
modelGenerators,
'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(assetsDir, `${config.pluginRootPath}${config.pluginModelPath}`);
modelJsonUrl = `${blogRoot}${pkgModelJsonUrl}`;
generators.push(...modelGenerators);
if (config.log) { // eslint-disable-line max-depth
print.log(`Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${assetsDir}'`);
}
}
}
if (modelJsonUrl === null) {
print.error('Did not found model json');
}
_.unset(config, 'model.use');
config = _.set(config, 'model.jsonPath', modelJsonUrl);
}
const scriptUrlToInject = getScriptURL(config.scriptFrom);
_.unset(config, 'scriptFrom');
if (config.tagMode) {
hexo.extend.helper.register('live2d', () => {
if (config.log) {
print.log('live2d tag detected, use tagMode.');
}
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
return contentToInject;
});
} else {
hexo.extend.helper.register('live2d', () => {
print.warn('live2d tag detected, but won\'t be use. Make sure \'tagMode\' config is expected. See #36, #122.');
});
}
/*
* Injector borrowed form here:
* https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
*/
if (!config.tagMode) {
hexo.extend.filter.register('after_render:html', (htmlContent) => {
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
let newHtmlContent = htmlContent;
if ((/([\n\r\s\t]*<\/body>)/i).test(htmlContent)) {
const lastIndex = htmlContent.lastIndexOf('</body>');
newHtmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`; // eslint-disable-line no-magic-numbers
}
return newHtmlContent;
});
}
hexo.extend.generator.register('live2d', () => generators);
}