Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
fix: fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 14, 2018
1 parent 1894542 commit 1423cae
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ const onSiteRootPath = '/live2dw/';
const onSiteJsPath = `${onSiteRootPath}lib/`;
const onSiteModelPath = `${onSiteRootPath}assets/`;

const defaultConfig = _.merge({},
{
enable: true,
scriptFrom: 'local',
});
const defaultConfig = {
enable: true,
scriptFrom: 'local',
};

// Apply options with default
let config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);


function getScriptURL (scriptFrom) {
switch (scriptFrom) {
case 'local': {
Expand Down Expand Up @@ -63,7 +61,7 @@ function getScriptURL (scriptFrom) {

if (config.enable) {
_.unset(config, 'enable');
if(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)) {
Expand All @@ -75,7 +73,7 @@ if (config.enable) {
} = loadModelFrom(tryPath, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from live2d_models folder(2), '${url.pathname(modelJsonUrl)}' from '${tryPath}'`);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: 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)) {
Expand All @@ -87,14 +85,14 @@ if (config.enable) {
} = 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.pathname(modelJsonUrl)}' from '${tryPath}'`);
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
const packageJsonPath = path.resolve(getNodeModulePath(config.model.use), 'package.json');
const packageJsonObj = require(packageJsonPath); // eslint-disable-line global-require
const assetsDir = path.resolve(packageJsonPath, './assets/');
const assetsDir = path.resolve(getNodeModulePath(config.model.use), './assets/');
const {
modelGenerators,
modelJsonUrl: pkgModelJsonUrl,
Expand All @@ -109,6 +107,8 @@ if (config.enable) {
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);
}

/**
Expand All @@ -121,13 +121,14 @@ if (config.enable) {
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) {
const scriptFrom = config.scriptFrom;
config = _.unset(config, 'scriptFrom');
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${getScriptURL(scriptFrom)}"></script><script>${scriptToInject}</script>`;
const contentToInject = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
if (/<\/body>/gi.test(htmlContent)) {
const lastIndex = htmlContent.lastIndexOf('</body>');
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
Expand Down

0 comments on commit 1423cae

Please sign in to comment.