From d94e3ddc6cad93ec04b167afb28a48c52b564ff6 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 6 Feb 2020 14:03:23 +0800 Subject: [PATCH] refactor(injector): make codacy happy --- lib/extend/injector.js | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/extend/injector.js b/lib/extend/injector.js index fa1abfad06..ed25e1041c 100644 --- a/lib/extend/injector.js +++ b/lib/extend/injector.js @@ -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 '' + content + ''; }); // avoid unnesscary replace() for better performance if (!code.length) return data; - return data.replace(pattern, str => { return isBegin ? str + code : code + str; }); };