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

Commit

Permalink
feat: use cheerio to prase HTML instead of using regExp
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Mar 18, 2018
1 parent e8f736c commit 05a8e47
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

const _ = require('lodash');
const cheerio = require('cheerio');
const fs = require('hexo-fs');
const path = require('path');
const url = require('url');
Expand Down Expand Up @@ -183,16 +184,11 @@ if (config.enable) {
*/
hexo.extend.filter.register('after_render:html', (htmlContent) => {

const $ = cheerio.load(htmlContent);
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
let newHtmlContent = htmlContent;
if (/<\/body>/gi.test(htmlContent)) {

const lastIndex = htmlContent.lastIndexOf('</body>');
newHtmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;

}
return newHtmlContent;
$('body').append(contentToInject);
return $.html();

});

Expand Down
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"live2d-widget-model-wanko": "^1.0.5"
},
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"colors": "^1.2.1",
"hexo-fs": "^0.2.2",
"live2d-widget": "^3.0.5",
Expand Down

0 comments on commit 05a8e47

Please sign in to comment.