11/**
22 * @description The live2d-widget generator for hexo
33 */
4+ /*global hexo*/
45
5-
6- 'use strict'
6+ 'use strict' ;
77
88const _ = require ( 'lodash' ) ;
99const colors = require ( 'colors' ) ;
@@ -16,20 +16,6 @@ const getFileMD5 = require('./lib/getFileMD5');
1616const getNodeModulePath = require ( './lib/getNodeModulePath' ) ;
1717const loadModelFrom = require ( './lib/loadModelFrom' ) ;
1818
19- const defaultConfig = _ . merge ( { } ,
20- {
21- enable : true ,
22- scriptFrom : 'local'
23- } )
24-
25- // apply options with default
26- const config = _ . defaultsDeep ( { } , hexo . config . live2d , hexo . theme . config . live2d , defaultConfig ) ;
27-
28- // Check if enabled
29- if ( ! config . enable ) {
30- return ;
31- }
32-
3319const generators = [ ] ;
3420
3521const manifest = require ( 'live2d-widget/lib/manifest' ) ;
@@ -42,50 +28,59 @@ const onSiteRootPath = '/live2dw/';
4228const onSiteJsPath = `${ onSiteRootPath } lib/` ;
4329const onSiteModelPath = `${ onSiteRootPath } assets/` ;
4430
45- let scriptURL ;
31+ const defaultConfig = _ . merge ( { } ,
32+ {
33+ enable : true ,
34+ scriptFrom : 'local' ,
35+ } ) ;
4636
47- switch ( config . scriptFrom ) {
48- case 'local' :
37+ // apply options with default
38+ let config = _ . defaultsDeep ( { } , hexo . config . live2d , hexo . theme . config . live2d , defaultConfig ) ;
39+
40+
41+ function getScriptURL ( scriptFrom ) {
42+ switch ( scriptFrom ) {
43+ case 'local' :{
4944 // a.1 is local
5045 // use local(1)
5146 const scriptGenerators = buildGeneratorsFromManifest ( manifest , path . dirname ( mainfestPath ) , onSiteJsPath ) ;
5247 const useHash = getFileMD5 ( path . resolve ( path . dirname ( mainfestPath ) , coreScriptName ) ) ;
5348 generators . push ( ...scriptGenerators ) ;
54- scriptURL = `${ url . resolve ( onSiteJsPath , coreScriptName ) } ?${ useHash } ` ;
55- break ;
49+ return `${ url . resolve ( onSiteJsPath , coreScriptName ) } ?${ useHash } ` ;
50+ }
5651 case 'jsdelivr' :
5752 // a.2 is jsdelivr online CDN
5853 // use jsdelivr(2)
59- scriptURL = `https://cdn.jsdelivr.net/npm/live2d-widget@${ coreJsDepVer } /lib/${ coreScriptName } ` ;
60- break ;
54+ return `https://cdn.jsdelivr.net/npm/live2d-widget@${ coreJsDepVer } /lib/${ coreScriptName } ` ;
6155 case 'unpkg' :
6256 // a.3 is unpkg online CDN
6357 // use unpkg(3)
64- scriptURL = `https://unpkg.com/live2d-widget@${ coreJsDepVer } /lib/${ coreScriptName } ` ;
65- break ;
58+ return `https://unpkg.com/live2d-widget@${ coreJsDepVer } /lib/${ coreScriptName } ` ;
6659 default :
6760 // a.4 is custom
6861 // use custom(4)
69- scriptURL = config . scriptFrom ;
70- break ;
62+ return scriptFrom ;
63+ }
7164}
7265
73- if ( config . model . use ) {
74- const modelInHexoBaseDir = [
66+ if ( config . enable ) {
67+ _ . unset ( config , 'enable' ) ;
68+ if ( config . model . use ) {
69+ const modelInHexoBaseDir = [
7570 path . resolve ( hexo . base_dir , './live2d_models/' , config . model . use ) ,
7671 path . resolve ( hexo . base_dir , config . model . use ) ,
7772 ]
78- . reduce ( ( p , path ) => {
79- if ( ! p && fs . existsSync ( path ) )
80- return path ;
81- else
82- return p ;
83- } , undefined ) ;
84- const modelPaths = modelInHexoBaseDir || getNodeModulePath ( config . model . use ) || config . model . use ; // Search paths
85- const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom ( modelPaths , onSiteModelPath ) ;
86- generators . push ( ...modelGenerators ) ;
87- config = _ . set ( config , 'model.jsonPath' , modelJsonUrl ) ;
88- switch ( type ) {
73+ . reduce ( ( p , path ) => {
74+ if ( ! p && fs . existsSync ( path ) )
75+ return path ;
76+ else
77+ return p ;
78+ } , undefined ) ;
79+ const modelPaths = modelInHexoBaseDir || getNodeModulePath ( config . model . use ) || config . model . use ; // Search paths
80+ const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom ( modelPaths , onSiteModelPath ) ;
81+ generators . push ( ...modelGenerators ) ;
82+ config = _ . set ( config , 'model.jsonPath' , modelJsonUrl ) ;
83+ switch ( type ) {
8984 case 1 :
9085 console . log ( `${ colors . green ( 'hexo-helper-live2d' . toUpperCase ( ) ) } : Loaded model from npm-module(1), ${ packageJsonObj . name } @${ packageJsonObj . version } from '${ modelPaths } '` ) ;
9186 break ;
@@ -95,31 +90,34 @@ if (config.model.use) {
9590 case 4 :
9691 console . log ( `${ colors . green ( 'hexo-helper-live2d' . toUpperCase ( ) ) } : Loaded Model from online(4), at '${ modelJsonUrl } '` ) ;
9792 break ;
93+ }
9894 }
99- }
100-
101- /**
102- * Deprecated version support
103- * since 3.0
104- * Don't manually add live2d tag into your site template
105- */
106-
107- hexo . extend . helper . register ( 'live2d' , function ( ) {
108- 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.` ) ;
109- } ) ;
110-
111- // injector borrowed form here:
112- // https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
113- hexo . extend . filter . register ( 'after_render:html' , function ( htmlContent ) {
114- const scriptToInject = `L2Dwidget.init(${ JSON . stringify ( config ) } );` ;
115- const contentToInject = `<script src="${ scriptURL } "></script><script>${ scriptToInject } </script>` ;
116- if ( / < \/ b o d y > / gi. test ( htmlContent ) ) {
117- let lastIndex = htmlContent . lastIndexOf ( '</body>' ) ;
118- htmlContent = `${ htmlContent . substring ( 0 , lastIndex ) } ${ contentToInject } ${ htmlContent . substring ( lastIndex , htmlContent . length ) } ` ;
119- }
120- return htmlContent ;
121- } ) ;
12295
123- hexo . extend . generator . register ( 'live2d' , function ( locals ) {
124- return generators ;
125- } ) ;
96+ /**
97+ * Deprecated version support
98+ * since 3.0
99+ * Don't manually add live2d tag into your site template
100+ */
101+
102+ hexo . extend . helper . register ( 'live2d' , function ( ) {
103+ 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.` ) ;
104+ } ) ;
105+
106+ // injector borrowed form here:
107+ // https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
108+ hexo . extend . filter . register ( 'after_render:html' , function ( htmlContent ) {
109+ const scriptFrom = config . scriptFrom ;
110+ _ . unset ( config , 'scriptFrom' ) ;
111+ const scriptToInject = `L2Dwidget.init(${ JSON . stringify ( config ) } );` ;
112+ const contentToInject = `<script src="${ getScriptURL ( scriptFrom ) } "></script><script>${ scriptToInject } </script>` ;
113+ if ( / < \/ b o d y > / gi. test ( htmlContent ) ) {
114+ let lastIndex = htmlContent . lastIndexOf ( '</body>' ) ;
115+ htmlContent = `${ htmlContent . substring ( 0 , lastIndex ) } ${ contentToInject } ${ htmlContent . substring ( lastIndex , htmlContent . length ) } ` ;
116+ }
117+ return htmlContent ;
118+ } ) ;
119+
120+ hexo . extend . generator . register ( 'live2d' , function ( ) {
121+ return generators ;
122+ } ) ;
123+ }
0 commit comments