Skip to content

Commit

Permalink
add a per-post option
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwwyyxx committed Nov 29, 2022
1 parent 2720e39 commit 3b488da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ class Post {
// If rendering is disabled, do nothing.
return cacheObj.restoreAllSwigTags(content);
}
// Whether to allow async/concurrent rendering of tags within the post.
// Enabling it can improve performance for slow async tags, but may produce
// wrong results if tags within a post depend on each other.
const async_tags = data.async_tags || false;
if (!async_tags) {
return tag.render(cacheObj.restoreAllSwigTags(content), data);
}
// We'd like to render tags concurrently, so we split `content`
// by top-level HTML nodes that have swig tags into `split_content` array
// (nodes that don't have swig tags don't need to be split).
Expand Down
39 changes: 39 additions & 0 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,36 @@ describe('Post', () => {
].join('\n'));
});

it('render() - multiple tags with async rendering', async () => {
const content = [
'{% blockquote %}',
'test1',
'{% quote test2 %}',
'test3',
'{% endquote %}',
'test4',
'{% endblockquote %}',
'ASDF',
'{% quote test5 %}',
'test6',
'{% endquote %}'
].join('\n');

const data = await post.render(null, {
content,
async_tags: true
});
data.content.trim().should.eql([
'<blockquote><p>test1</p>',
'<blockquote><p>test3</p>',
'<footer><strong>test2</strong></footer></blockquote>',
'test4</blockquote>',
'ASDF',
'<blockquote><p>test6</p>',
'<footer><strong>test5</strong></footer></blockquote>'
].join('\n'));
});

it('render() - shouln\'t break curly brackets', async () => {
hexo.config.prismjs.enable = true;
hexo.config.highlight.enable = false;
Expand Down Expand Up @@ -1203,6 +1233,15 @@ describe('Post', () => {
});

data.content.trim().should.eql(`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 2</p>`);

// Test that the async tags logic recognize the tags correctly.
const data_async = await post.render(null, {
content,
engine: 'markdown',
async_tags: true
});

data_async.content.trim().should.eql(`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 2</p>`);
});

// #3543
Expand Down

0 comments on commit 3b488da

Please sign in to comment.