diff --git a/README.md b/README.md
index 7365d2b..80a1af9 100644
--- a/README.md
+++ b/README.md
@@ -67,9 +67,9 @@ live2d:
## https://github.com/EYHN/hexo-helper-live2d
live2d:
enable: true
- jsPath: local # 'local'||'jsdelivr'||'unpkg'||{Your own path, String}
+ jsPath: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
model:
- use: live2d-widget-model-miku # {npm-module name}||{folder name in live2d_models/}||{Your own path, String}
+ use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
```
> To see Chinese explainations, please have a look at [Chinese document](./README.zh-CN.md).
diff --git a/index.js b/index.js
index 259c649..43d00de 100755
--- a/index.js
+++ b/index.js
@@ -9,32 +9,25 @@ const fs = require('hexo-fs'),
path = require('path'),
url = require('url'),
_ = require('lodash'),
- localJsPath = '/live2dw/lib/',
- localModelPath = '/live2dw/assets/',
+ onSiteRootPath = '/live2dw/'
+ onSiteJsPath = onSiteRootPath + 'lib/',
+ onSiteModelPath = onSiteRootPath + 'assets/',
+ pkgInfo = require('./package'),
+ coreJsName = 'L2Dwidget.min.js',
coreJsList = require('live2d-widget/lib/manifest'),
- coreJsDepPath = require('live2d-widget/lib'),
+ coreJsPath = path.dirname(require.resolve('live2d-widget/lib/manifest')),
+ coreJsDepVer = pkgInfo.dependencies['live2d-widget'],
defaultConfig = require('live2d-widget/src/config/defaultConfig');
let fileArr = new Array(),
- modelPath,
- jsPath,
- coreJsNameLength,
- modelJsonPath,
config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
-function getCoreJs(where){
- let fileName = path.parse(where).name;
- if((fileName.length < coreJsNameLength) || (coreJsNameLength === undefined)){
- jsPath = where;
- coreJsNameLength = fileName.length;
- }
-}
-
-function getModelJson(where){
- let fileName = path.parse(where).name.split('.');
- if(fileName[1] === 'model'){
- modelJsonPath = where;
+// Check if enabled
+if(_.hasIn(config, 'enable')){
+ if(!config.enable){
+ return;
}
+ _.unset(config, 'enable');
}
function addFile(destPath, sourceFile){
@@ -43,104 +36,105 @@ function addFile(destPath, sourceFile){
data: () => fs.createReadStream(sourceFile),
});
}
-
-function addDir(destPath, sourceDir, func) {
- let lsDir = fs.readdirSync(sourceDir)
- lsDir.forEach(function (file) {
- if (fs.statSync(path.resolve(sourceDir, file)).isDirectory()) {
- addDir(path.resolve(destPath, file), path.resolve(sourceDir, file), func)
- } else {
- addFile(path.resolve(destPath, file), path.resolve(sourceDir, file));
- if(func !== undefined) func(path.resolve(destPath, file));
- }
- }, this);
+/**
+function addFile(destPath, sourceFile){
+ console.log({
+ "dest": destPath,
+ "sour": sourceFile,
+ });
}
-
-// Check if enabled
-if(_.hasIn(config, 'enable')){
- if(!config.enable){
- return;
+**/
+
+function process_modelFileIsOnLocal(where, from = onSiteModelPath) {
+ if(fs.statSync(where).isDirectory()){
+ let lsDir = fs.readdirSync(where),
+ modelJsonName;
+ for(let item of lsDir){
+ modelJsonName = modelJsonName || process_modelFileIsOnLocal(path.resolve(where, item), url.resolve(from, item + (fs.statSync(where).isDirectory() ? '/' : '')));
+ }
+ return modelJsonName;
+ }else{
+ addFile(url.resolve(from, path.basename(where)), where);
+ let fileName = path.basename(where);
+ if(fileName.split('.')[1] === 'model'){
+ return fileName;
+ }
}
- _.unset(config, 'enable');
}
-// Preprocess jsPath for injecting and file copying
-// Copy file and apply real_jsPath only if jsPath === jsOnLocalPath
-if(_.hasIn(config, 'jsPath')){
- switch(config.jsPath){
- case 'local':
- jsPath = localJsPath;
- break;
- case 'jsdelivr':
- jsPath = 'https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/clL2D.min.js';
- break;
- case 'unpkg':
- jsPath = 'https://unpkg.com/live2d-widget@3.x/lib/clL2D.min.js';
- break;
- default:
- jsPath = config.jsPath;
+function process_jsPathIsLocal(){
+ for(let f of Object.keys(coreJsList)){
+ addFile(url.resolve(onSiteJsPath, coreJsList[f]), path.resolve(coreJsPath, coreJsList[f]));
}
- _.unset(config, 'jsPath');
-}else{
- jsPath = jsOnLocalPath;
+ return url.resolve(onSiteJsPath, coreJsName);
}
-// Preprocess modelPath for injecting and file copying
-// 1. Unset model.jsonPath
-if(_.hasIn(config, 'model.jsonPath')){
- _.unset(config, 'model.jsonPath');
-}
-// 2. Process config.model.use
-// Set modelPath and config.model.use in some case
-// Copy file and apply config.model.jsonPath only if !_.hasIn(config, 'model.jsonPath')
-if(_.hasIn(config, 'model.use')){
- try{
- // 2.a is a npm-module
- modelPath = path.resolve(path.dirname(require.resolve(config.model.use + '/package')), './assets/');
- }catch(e){
- // 2.b is in live2d_models/ folder
- let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
- fs.exists(tryPath, function(exists){
- if(exists){
- // 2.b founded in live2d_models/
- // 3.b Apply config.model.jsonPath
- modelPath = tryPath;
- }else{
- // 2.c maybe an url or something, let it go~
- // 3.c Apply config.model.jsonPath
- config.model.jsonPath = config.model.use;
- }
- })
+function getJsPath(){
+ if(_.hasIn(config, 'jsPath')){
+ // a. have user modified config.jsPath
+ switch(config.jsPath){
+ case 'local':
+ // a.1 is local
+ // use local(1)
+ return process_jsPathIsLocal();
+ case 'jsdelivr':
+ // a.2 is jsdelivr online CDN
+ // use jsdelivr(2)
+ return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
+ case 'unpkg':
+ // a.3 is unpkg online CDN
+ // use unpkg(3)
+ return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
+ default:
+ // a.4 is custom url or path, etc.
+ // use custom(4), let it go~
+ return config.jsPath;
+ }
+ _.unset(config, 'jsPath');
+ }else{
+ // b. don't have user modified config.jsPath
+ // use local(1)
+ return process_jsPathIsLocal();
}
- _.unset(config, 'model.use');
-}else{
- // 2.d doesn't have config.model.use use default
- // 3.d Apply config.model.jsonPath
- config.model.jsonPath = defaultConfig.model.jsonPath;
-}
-
-// Process config.model.jsonPath
-// and copy files
-if(!_.hasIn(config, 'model.jsonPath')){
- addDir(localModelPath, modelPath, getModelJson);
- config.model.jsonPath = modelJsonPath;
- console.log(config.model.jsonPath)
}
+function getModelJsonPath(){
+ // Unset model.jsonPath
+ if(_.hasIn(config, 'model.jsonPath')){
+ _.unset(config, 'model.jsonPath');
+ }
-// Process jsPath with jsPath(processed)
-// and copy files
-if(jsPath === localJsPath){
- // a. is local
- // copy coreJs
- for(let f of Object.keys(coreJsList)){
- addFile(localJsPath + f, path.resolve(coreJsDepPath, coreJsList[f]));
- getCoreJs(localJsPath + f);
+ // Process config.model.use
+ if(_.hasIn(config, 'model.use')){
+ // a. have user modified config.model.use
+ try{
+ // a.1 is a npm-module(1)
+ let tryModulePath = path.dirname(require.resolve(config.model.use + '/package'));
+ let modelPath = path.resolve(tryModulePath, './assets/');
+ return process_modelFileIsOnLocal(modelPath);
+ }catch(e){
+ let tryFolderPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
+ fs.exists(tryFolderPath, function(exists){
+ if(exists){
+ // a.2 founded in live2d_models/
+ let modelPath = path.resolve(tryFolderPath, './assets/');
+ return process_modelFileIsOnLocal(modelPath);
+ }else{
+ // a.3 is custom url or path, etc.
+ // use custom(3), let it go~
+ return config.model.use;
+ }
+ })
+ }
+ _.unset(config, 'model.use');
+ }else{
+ // b. don't have user modified config.model.use
+ // use default
+ return defaultConfig.model.jsonPath;
}
-}else{
- // b. is a url or something, let it go ~
}
+config.model.jsonPath = getModelJsonPath();
/**
* Deprecated version support
@@ -156,7 +150,7 @@ hexo.extend.helper.register('live2d', function(){
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function(htmlContent){
let launcherScript = `L2Dwidget.init(${JSON.stringify(config)});`;
- let injectExtraScript = ``
+ let injectExtraScript = ``
if(/<\/body>/gi.test(htmlContent)){
let lastIndex = htmlContent.lastIndexOf('