Skip to content

Commit 3b488da

Browse files
committed
add a per-post option
1 parent 2720e39 commit 3b488da

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/hexo/post.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ class Post {
413413
// If rendering is disabled, do nothing.
414414
return cacheObj.restoreAllSwigTags(content);
415415
}
416+
// Whether to allow async/concurrent rendering of tags within the post.
417+
// Enabling it can improve performance for slow async tags, but may produce
418+
// wrong results if tags within a post depend on each other.
419+
const async_tags = data.async_tags || false;
420+
if (!async_tags) {
421+
return tag.render(cacheObj.restoreAllSwigTags(content), data);
422+
}
416423
// We'd like to render tags concurrently, so we split `content`
417424
// by top-level HTML nodes that have swig tags into `split_content` array
418425
// (nodes that don't have swig tags don't need to be split).

test/scripts/hexo/post.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,36 @@ describe('Post', () => {
902902
].join('\n'));
903903
});
904904

905+
it('render() - multiple tags with async rendering', async () => {
906+
const content = [
907+
'{% blockquote %}',
908+
'test1',
909+
'{% quote test2 %}',
910+
'test3',
911+
'{% endquote %}',
912+
'test4',
913+
'{% endblockquote %}',
914+
'ASDF',
915+
'{% quote test5 %}',
916+
'test6',
917+
'{% endquote %}'
918+
].join('\n');
919+
920+
const data = await post.render(null, {
921+
content,
922+
async_tags: true
923+
});
924+
data.content.trim().should.eql([
925+
'<blockquote><p>test1</p>',
926+
'<blockquote><p>test3</p>',
927+
'<footer><strong>test2</strong></footer></blockquote>',
928+
'test4</blockquote>',
929+
'ASDF',
930+
'<blockquote><p>test6</p>',
931+
'<footer><strong>test5</strong></footer></blockquote>'
932+
].join('\n'));
933+
});
934+
905935
it('render() - shouln\'t break curly brackets', async () => {
906936
hexo.config.prismjs.enable = true;
907937
hexo.config.highlight.enable = false;
@@ -1203,6 +1233,15 @@ describe('Post', () => {
12031233
});
12041234

12051235
data.content.trim().should.eql(`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 2</p>`);
1236+
1237+
// Test that the async tags logic recognize the tags correctly.
1238+
const data_async = await post.render(null, {
1239+
content,
1240+
engine: 'markdown',
1241+
async_tags: true
1242+
});
1243+
1244+
data_async.content.trim().should.eql(`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 2</p>`);
12061245
});
12071246

12081247
// #3543

0 commit comments

Comments
 (0)