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

Commit

Permalink
feat: add tagMode.
Browse files Browse the repository at this point in the history
fix #122
  • Loading branch information
xiazeyu committed Jul 14, 2018
1 parent dd3d5da commit e2ac803
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,26 @@ npm install --save hexo-helper-live2d

<details><summary>Still using legacy version?</summary><br>

- If you want to use new injector, which will inject to all pages:

Please delete `{{ live2d() }}` or `<%- live2d() %>` before `</body>` 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.

</details>

<details><summary>Tag mode</summary><br>

Please insert `{{ live2d() }}`(swig) or `<%- live2d() %>`(ejs) before `</body>` 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.

</details>

### Others, for jekyll, wordpress, etc

See [live2d-widget.js](https://github.com/xiazeyu/live2d-widget.js) WIP.
Expand All @@ -67,6 +79,7 @@ live2d:
pluginRootPath: live2dw/
pluginJsPath: lib/
pluginModelPath: assets/
tagMode: false
model:
use: live2d-widget-model-wanko
display:
Expand Down Expand Up @@ -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/
Expand Down
14 changes: 14 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,25 @@ npm install --save hexo-helper-live2d

<details><summary>仍在使用老版本?</summary><br>

- 如果您想使用最近的注入模式(将会在每个页面上显示):

请从 `layout/layout.ejs``layout/_layout.swig` 中删掉 `</body>` 前的
`{{ live2d() }}``<%- live2d() %>`.

- 如果您想使用旧的标签模式(仅替换 `live2d` 标签):

请保留`{{ live2d() }}``<%- live2d() %>`, 并将 `tagMode` 设置为 `true`.

我们推荐您使用 `npm install --save hexo-helper-live2d@3.x` 来强制安装最新版本.

</details>

<details><summary>标签模式</summary><br>

请在您想插入的页面的 `</body>` 前插入 `{{ live2d() }}`(swig) 或 `<%- live2d() %>`(ejs). 将 `tagMode` 设置为 `true`, 然后插件将只会在拥有live2d标签的页面出现.

</details>

### 其他的, for jekyll, wordpress, etc

参阅 [live2d-widget.js](https://github.com/xiazeyu/live2d-widget.js) 仍在编写中.
Expand All @@ -66,6 +78,7 @@ live2d:
pluginRootPath: live2dw/
pluginJsPath: lib/
pluginModelPath: assets/
tagMode: false
model:
use: live2d-widget-model-wanko
display:
Expand Down Expand Up @@ -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/ 下的目录名
Expand Down
53 changes: 33 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const defaultConfig = {
'pluginModelPath': 'assets/',
'pluginRootPath': 'live2dw/',
'scriptFrom': 'local',
'tagMode': false,
};

// Apply options with default
Expand Down Expand Up @@ -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 = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
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 = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
let newHtmlContent = htmlContent;
if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) {
hexo.extend.filter.register('after_render:html', (htmlContent) => {

const lastIndex = htmlContent.lastIndexOf('</body>');
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 = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
let newHtmlContent = htmlContent;
if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) {

}
return newHtmlContent;
const lastIndex = htmlContent.lastIndexOf('</body>');
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);

Expand Down

0 comments on commit e2ac803

Please sign in to comment.