diff --git a/.eslintrc.js b/.eslintrc.js
index d199283..223795c 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -3,6 +3,7 @@ module.exports = {
'es6': true,
'node': true,
},
+ // 'extends': 'eslint:all',
'extends': 'eslint:recommended',
'parserOptions': {
'sourceType': 'module',
@@ -188,5 +189,21 @@ module.exports = {
'space-unary-ops': [
'error',
],
+ 'spaced-comment': [
+ 'error',
+ ],
+ 'quote-props': [
+ 'error',
+ ],
+ 'require-jsdoc': [
+ 'error',
+ ],
+ 'func-style': [
+ 'warn',
+ 'declaration',
+ ],
+ 'padded-blocks': [
+ 'warn',
+ ],
},
};
diff --git a/index.js b/index.js
index 4b8c48f..8747e9a 100755
--- a/index.js
+++ b/index.js
@@ -1,7 +1,7 @@
/**
* @description The live2d-widget generator for hexo
*/
-/*global hexo*/
+/* global hexo */
const _ = require('lodash');
const colors = require('colors');
@@ -27,88 +27,131 @@ const onSiteJsPath = `${onSiteRootPath}lib/`;
const onSiteModelPath = `${onSiteRootPath}assets/`;
const defaultConfig = {
- enable: true,
- scriptFrom: 'local',
+ 'enable': true,
+ 'scriptFrom': 'local',
};
// 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) {
+
switch (scriptFrom) {
+
case 'local': {
- // Is local(1)
- // Use local
+
+ /*
+ * Is local(1)
+ * Use local
+ */
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath);
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
generators.push(...scriptGenerators);
return `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
+
}
case 'jsdelivr':
- // Is jsdelivr online CDN(2)
- // Use jsdelivr
+
+ /*
+ * Is jsdelivr online CDN(2)
+ * Use jsdelivr
+ */
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
case 'unpkg':
- // Is unpkg online CDN(3)
- // Use unpkg
+
+ /*
+ * Is unpkg online CDN(3)
+ * Use unpkg
+ */
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
default:
- // Is custom(4)
- // Use custom
+
+ /*
+ * Is custom(4)
+ * Use custom
+ */
return scriptFrom;
+
}
+
}
if (config.enable) {
+
_.unset(config, 'enable');
- if(_.hasIn(config, 'model.use')) {
+ if (_.hasIn(config, 'model.use')) {
+
let modelJsonUrl;
let tryPath = path.resolve(hexo.base_dir, './live2d_models/', config.model.use);
- if(fs.existsSync(tryPath)) {
- // Is in live2d_models(2)
- // LoadModelFrom
+ if (fs.existsSync(tryPath)) {
+
+ /*
+ * Is in live2d_models(2)
+ * LoadModelFrom
+ */
const {
modelGenerators,
- modelJsonUrl: pkgModelJsonUrl,
+ 'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(tryPath, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from live2d_models folder(2), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
- }else{
+
+ } else {
+
tryPath = path.resolve(hexo.base_dir, config.model.use);
- if(fs.existsSync(tryPath)) {
- // Is in hexo base releated path(3)
- // LoadModelFrom
+ if (fs.existsSync(tryPath)) {
+
+ /*
+ * Is in hexo base releated path(3)
+ * LoadModelFrom
+ */
const {
modelGenerators,
- modelJsonUrl: pkgModelJsonUrl,
+ 'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(tryPath, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from hexo base releated path(3), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
- }else if(getNodeModulePath(config.model.use) !== '') {
- // Is npm-module(1)
- // Convert path to assets folder
- // LoadModelFrom
+
+ } else if (getNodeModulePath(config.model.use) === '') {
+
+ /*
+ * Is custom(4)
+ * Use custom
+ */
+ modelJsonUrl = config.model.use;
+ console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: 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,
+ 'modelJsonUrl': pkgModelJsonUrl,
} = loadModelFrom(assetsDir, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${assetsDir}'`);
- }else{
- // Is custom(4)
- // Use custom
- modelJsonUrl = config.model.use;
- console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from custom(4), at '${modelJsonUrl}'`);
+
}
+
}
_.unset(config, 'model.use');
config = _.set(config, 'model.jsonPath', modelJsonUrl);
+
}
/**
@@ -117,26 +160,34 @@ if (config.enable) {
* Don't manually add live2d tag into your site template
*/
- hexo.extend.helper.register('live2d', function deprecatedWarning () {
+ hexo.extend.helper.register('live2d', () => {
+
console.warn(`${colors.green('hexo-helper-live2d'.toUpperCase())}: live2d tag was deprecated since 3.0. See #36. PLEASE REMOVE live2d TAG IN YOUR TEMPLATE FILE.`);
+
});
const scriptUrlToInject = getScriptURL(config.scriptFrom);
_.unset(config, 'scriptFrom');
- // Injector borrowed form here:
- // https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
- hexo.extend.filter.register('after_render:html', function HTMLInjector (htmlContent) {
+ /*
+ * Injector borrowed form here:
+ * https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
+ */
+ hexo.extend.filter.register('after_render:html', (htmlContent) => {
+
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = ``;
+ let newHtmlContent = htmlContent;
if (/<\/body>/gi.test(htmlContent)) {
+
const lastIndex = htmlContent.lastIndexOf('