Skip to content

Commit

Permalink
refactor(injector): make codacy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Feb 6, 2020
1 parent 628ccb1 commit d94e3dd
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions lib/extend/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,29 @@ class Injector {
}

exec(data, locals = { page: {} }) {
const current = () => {
const { page } = locals;

if (page.__index) return 'home';
if (page.__post) return 'post';
if (page.__page) return 'page';
if (page.archive) return 'archive';
if (page.category) return 'category';
if (page.tag) return 'tag';
if (page.layout) return page.layout;
return 'default';
};
let currentType = 'default';
const { page } = locals;

if (page.__index) currentType = 'home';
if (page.__post) currentType = 'post';
if (page.__page) currentType = 'page';
if (page.archive) currentType = 'archive';
if (page.category) currentType = 'category';
if (page.tag) currentType = 'tag';
if (page.layout) currentType = page.layout;

const injector = (data, pattern, flag, isBegin = true) => {
if (data.includes(`hexo injector ${flag}`)) return data;

const currentType = current();
const code = this.cache.apply(`${flag}-${currentType}-code`, () => {
const content = currentType === 'default' ? this.getText(flag, 'default') : this.getText(flag, currentType) + this.getText(flag, 'default');

if (!content.length) return '';

return '<!-- hexo injector ' + flag + ' start -->' + content + '<!-- hexo injector ' + flag + ' end -->';
});

// avoid unnesscary replace() for better performance
if (!code.length) return data;

return data.replace(pattern, str => { return isBegin ? str + code : code + str; });
};

Expand Down

0 comments on commit d94e3dd

Please sign in to comment.