diff --git a/lib/extend/injector.js b/lib/extend/injector.js index 2cab5514a5..74689d3202 100644 --- a/lib/extend/injector.js +++ b/lib/extend/injector.js @@ -28,6 +28,10 @@ class Injector { return arr.join(''); } + getSize(entry) { + return this.cache.apply(`${entry}-size`, Object.keys(this.store[entry]).length); + } + register(entry, value, to = 'default') { if (!entry) throw new TypeError('entry is required'); if (typeof value === 'function') value = value(); @@ -38,41 +42,58 @@ class Injector { entryMap[to] = valueSet; } - exec(data, locals = { page: {} }) { + _getPageType(pageLocals) { let currentType = 'default'; - const { page } = locals; + if (pageLocals.__index) currentType = 'home'; + if (pageLocals.__post) currentType = 'post'; + if (pageLocals.__page) currentType = 'page'; + if (pageLocals.archive) currentType = 'archive'; + if (pageLocals.category) currentType = 'category'; + if (pageLocals.tag) currentType = 'tag'; + if (pageLocals.layout) currentType = pageLocals.layout; + + return currentType; + } - 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; + _injector(input, pattern, flag, isBegin = true, currentType) { + if (input.includes(`hexo injector ${flag}`)) return input; - const injector = (data, pattern, flag, isBegin = true) => { - if (data.includes(`hexo injector ${flag}`)) return data; + const code = this.cache.apply(`${flag}-${currentType}-code`, () => { + const content = currentType === 'default' ? this.getText(flag, 'default') : this.getText(flag, currentType) + this.getText(flag, 'default'); - 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 + ''; + }); - if (!content.length) return ''; - return '' + content + ''; - }); + // avoid unnecessary replace() for better performance + if (!code.length) return input; - // avoid unnecessary replace() for better performance - if (!code.length) return data; - return data.replace(pattern, str => { return isBegin ? str + code : code + str; }); - }; + return input.replace(pattern, str => { return isBegin ? str + code : code + str; }); + } - // Inject head_begin - data = injector(data, /