Skip to content

Commit

Permalink
feat(injector): utilize cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 5, 2020
1 parent 0213f5f commit af6c27f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/extend/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
53 changes: 19 additions & 34 deletions lib/plugins/filter/after_render/injector.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit af6c27f

Please sign in to comment.