Skip to content

Commit

Permalink
feat(filter): add new after_route_render filter (hexojs#4051)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtj authored and Thomas Parisot committed Jan 17, 2020
1 parent 06984f6 commit 1972646
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
19 changes: 12 additions & 7 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ const createLoadThemeRoute = function(generatorResult, locals, ctx) {

if (view) {
log.debug(`Rendering HTML ${name}: ${magenta(path)}`);
return view.render(locals).tap(result => {
if (useCache) {
routeCache.set(generatorResult, result);
}
}).tapCatch(err => {
log.error({ err }, `Render HTML failed: ${magenta(path)}`);
});
return view.render(locals)
.then(result => ctx.execFilter('after_route_render', result, {
context: ctx,
args: [locals]
}))
.tap(result => {
if (useCache) {
routeCache.set(generatorResult, result);
}
}).tapCatch(err => {
log.error({ err }, `Render HTML failed: ${magenta(path)}`);
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/filter/after_render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module.exports = ctx => {
const { filter } = ctx.extend;

filter.register('after_render:html', require('./external_link'));
filter.register('after_render:html', require('./meta_generator'));
filter.register('after_route_render', require('./external_link'));
filter.register('after_route_render', require('./meta_generator'));
};
13 changes: 13 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,19 @@ describe('Hexo', () => {
});
});

it('_generate() - after_route_render filter', () => {
const hook = sinon.spy(result => result.replace('foo', 'bar'));
hexo.extend.filter.register('after_route_render', hook);
hexo.theme.setView('test.swig', 'foo');
hexo.extend.generator.register('test', () => ({
path: 'test',
layout: 'test'
}));
return hexo._generate()
.then(() => checkStream(route.get('test'), 'bar'))
.then(() => hook.called.should.be.true);
});

it('_generate() - return nothing in generator', () => {
hexo.extend.generator.register('test_nothing', () => {
//
Expand Down

0 comments on commit 1972646

Please sign in to comment.