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

Commit

Permalink
perf: better RegExp from hexo-inject
Browse files Browse the repository at this point in the history
fix #96
  • Loading branch information
xiazeyu committed Mar 30, 2018
1 parent 8e81a5b commit 7e25de7
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,13 @@ if (config.enable) {
/*
* Injector borrowed form here:
* https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
* TODO:
* use cheerio to modify DOM
* failed in 2018.3.18
* https://github.com/cheeriojs/cheerio/issues/1031
*/
hexo.extend.filter.register('after_render:html', (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)) {
if (/([\n\r\s\t]*<\/body>)/i.test(htmlContent)) {

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

0 comments on commit 7e25de7

Please sign in to comment.