diff --git a/lib/extend/injector.js b/lib/extend/injector.js index 5c468f4176..34effb37d2 100644 --- a/lib/extend/injector.js +++ b/lib/extend/injector.js @@ -18,7 +18,7 @@ class Injector { return Array.from(this.store[entry]); } - register(entry, value, to = '') { + register(entry, value, to = 'default') { if (!entry) throw new TypeError('entry is required'); if (typeof value === 'function') value = value(); diff --git a/lib/plugins/filter/after_render/injector.js b/lib/plugins/filter/after_render/injector.js index 477f344dad..b772ad0d34 100644 --- a/lib/plugins/filter/after_render/injector.js +++ b/lib/plugins/filter/after_render/injector.js @@ -1,53 +1,38 @@ 'use strict'; +const { Cache } = require('hexo-util'); +const cache = new Cache(); + function injectFilter(data, locals = { page: {} }) { const { injector: _Injector } = this.extend; - const is = input => { - let result = true; + const current = () => { const { page } = locals; - switch (input) { - case 'home': - result = Boolean(page.__index); - break; - - case 'post': - result = Boolean(page.__post); - break; - - case 'page': - result = Boolean(page.__page); - break; - - case 'archive': - result = Boolean(page.archive); - break; - - case 'category': - result = Boolean(page.category); - break; - - case 'tag': - result = Boolean(page.tag); - break; - - default: - result = true; - } - - return result; + 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'; + return 'default'; }; function injector(data, pattern, flag, isBegin = true) { if (data.includes(`hexo injector ${flag}`)) return data; - const arr = _Injector.get(flag).filter(i => is(i.to)).map(i => i.value); + const arr = cache.apply( + `${flag}-${current()}-arr`, + _Injector.get(flag).filter(i => i.to === current()).map(i => i.value) + ); if (!arr.length) return data; return data.replace(pattern, str => { - const code = arr.reduce((a, c) => a + c, ''); + const code = cache.apply( + `${flag}-${current()}-code`, + arr.reduce((a, c) => a + c, '') + ); if (isBegin) { return str