From e2ac803d9552a7f8dc413656c245d28d1f8deb07 Mon Sep 17 00:00:00 2001 From: xiazeyu_2011 Date: Sat, 14 Jul 2018 18:22:49 +0800 Subject: [PATCH] feat: add tagMode. fix #122 --- README.md | 14 +++++++++++++ README.zh-CN.md | 14 +++++++++++++ index.js | 53 ++++++++++++++++++++++++++++++------------------- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2177ac3..c5a1d88 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,26 @@ npm install --save hexo-helper-live2d
Still using legacy version?
+- If you want to use new injector, which will inject to all pages: + Please delete `{{ live2d() }}` or `<%- live2d() %>` before `` in `layout/layout.ejs` or `layout/_layout.swig`. +- If you want to use the old replace mode, which only replace `live2d` tag: + +Keep `{{ live2d() }}` or `<%- live2d() %>`, and turn the `tagMode` config to `true`. + We recommend you to use `npm install --save hexo-helper-live2d@3.x` to force install the latest version.
+
Tag mode
+ +Please insert `{{ live2d() }}`(swig) or `<%- live2d() %>`(ejs) before `` in whichever pages you want to insert. And turn `tagMode` config to `true`, and then live2dwidget will only be on those who have `live2d` tag. + +
+ ### Others, for jekyll, wordpress, etc See [live2d-widget.js](https://github.com/xiazeyu/live2d-widget.js) WIP. @@ -67,6 +79,7 @@ live2d: pluginRootPath: live2dw/ pluginJsPath: lib/ pluginModelPath: assets/ + tagMode: false model: use: live2d-widget-model-wanko display: @@ -97,6 +110,7 @@ live2d: # scriptFrom: jsdelivr # jsdelivr CDN # scriptFrom: unpkg # unpkg CDN # scriptFrom: https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js # Your custom url + tagMode: false # Specifies whether only to replace live2d tag instead of inject to all pages model: use: live2d-widget-model-wanko # npm-module package name # use: wanko # folder name in (hexo base dir)/live2d_models/ diff --git a/README.zh-CN.md b/README.zh-CN.md index 30505f4..8d2c5c5 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -42,13 +42,25 @@ npm install --save hexo-helper-live2d
仍在使用老版本?
+- 如果您想使用最近的注入模式(将会在每个页面上显示): + 请从 `layout/layout.ejs` 或 `layout/_layout.swig` 中删掉 `` 前的 `{{ live2d() }}` 或 `<%- live2d() %>`. +- 如果您想使用旧的标签模式(仅替换 `live2d` 标签): + +请保留`{{ live2d() }}` 或 `<%- live2d() %>`, 并将 `tagMode` 设置为 `true`. + 我们推荐您使用 `npm install --save hexo-helper-live2d@3.x` 来强制安装最新版本.
+
标签模式
+ +请在您想插入的页面的 `` 前插入 `{{ live2d() }}`(swig) 或 `<%- live2d() %>`(ejs). 将 `tagMode` 设置为 `true`, 然后插件将只会在拥有live2d标签的页面出现. + +
+ ### 其他的, for jekyll, wordpress, etc 参阅 [live2d-widget.js](https://github.com/xiazeyu/live2d-widget.js) 仍在编写中. @@ -66,6 +78,7 @@ live2d: pluginRootPath: live2dw/ pluginJsPath: lib/ pluginModelPath: assets/ + tagMode: false model: use: live2d-widget-model-wanko display: @@ -95,6 +108,7 @@ live2d: # scriptFrom: jsdelivr # jsdelivr CDN # scriptFrom: unpkg # unpkg CDN # scriptFrom: https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js # 你的自定义 url + tagMode: false # 标签模式, 是否仅替换 live2d tag标签而非插入到所有页面中 model: use: live2d-widget-model-wanko # npm-module package name # use: wanko # 博客根目录/live2d_models/ 下的目录名 diff --git a/index.js b/index.js index 3536f5e..0ca2af4 100755 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ const defaultConfig = { 'pluginModelPath': 'assets/', 'pluginRootPath': 'live2dw/', 'scriptFrom': 'local', + 'tagMode': false, }; // Apply options with default @@ -166,39 +167,51 @@ if (config.enable) { } - /** - * Deprecated version support - * since 3.0 - * Don't manually add live2d tag into your site template - */ + const scriptUrlToInject = getScriptURL(config.scriptFrom); + _.unset(config, 'scriptFrom'); - hexo.extend.helper.register('live2d', () => { + if (config.tagMode) { - print.warn('live2d tag was deprecated since 3.0. See #36. PLEASE REMOVE live2d TAG IN YOUR TEMPLATE FILE.'); + hexo.extend.helper.register('live2d', () => { - }); + const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`; + const contentToInject = ``; + return contentToInject; - const scriptUrlToInject = getScriptURL(config.scriptFrom); - _.unset(config, 'scriptFrom'); + }); + + } else { + + hexo.extend.helper.register('live2d', () => { + + print.warn('live2d tag was 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 */ - hexo.extend.filter.register('after_render:html', (htmlContent) => { + if (!config.tagMode) { - const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`; - const contentToInject = ``; - let newHtmlContent = htmlContent; - if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) { + hexo.extend.filter.register('after_render:html', (htmlContent) => { - const lastIndex = htmlContent.lastIndexOf(''); - newHtmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`; // eslint-disable-line no-magic-numbers + const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`; + const contentToInject = ``; + let newHtmlContent = htmlContent; + if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) { - } - return newHtmlContent; + const lastIndex = htmlContent.lastIndexOf(''); + 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);