Skip to content

Commit ff0cfca

Browse files
committed
Async rendering of tags within a post
1 parent afc4d7f commit ff0cfca

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

lib/hexo/post.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const assert = require('assert');
44
const moment = require('moment');
5+
const parse5 = require('parse5');
56
const Promise = require('bluebird');
67
const { join, extname, basename } = require('path');
78
const { magenta } = require('picocolors');
@@ -414,18 +415,21 @@ class Post {
414415
text: data.content,
415416
path: source,
416417
engine: data.engine,
417-
toString: true,
418-
onRenderEnd(content) {
419-
// Replace cache data with real contents
420-
data.content = cacheObj.restoreAllSwigTags(content);
421-
422-
// Return content after replace the placeholders
423-
if (disableNunjucks) return data.content;
424-
425-
// Render with Nunjucks
426-
return tag.render(data.content, data);
427-
}
418+
toString: true
428419
}, options);
420+
}).then(content => {
421+
const doc = parse5.parseFragment(content);
422+
// Indepedently render the tags in each top-level node
423+
// asynchronously. This can significantly speed up rendering of slow tags
424+
const results = doc.childNodes.map(async node => {
425+
// Replace cache data with real contents
426+
const content = cacheObj.restoreAllSwigTags(parse5.serializeOuter(node));
427+
// If disabled, return content after replacing the placeholders
428+
if (disableNunjucks) return content;
429+
// Render with Nunjucks
430+
return await tag.render(content, data);
431+
});
432+
return Promise.all(results).then(x => x.join(''));
429433
}).then(content => {
430434
data.content = cacheObj.restoreCodeBlocks(content);
431435

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"moment-timezone": "^0.5.34",
5757
"picocolors": "^1.0.0",
5858
"nunjucks": "^3.2.3",
59+
"parse5": "^7.0.0",
5960
"pretty-hrtime": "^1.0.3",
6061
"resolve": "^1.22.0",
6162
"strip-ansi": "^6.0.0",

test/scripts/hexo/post.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { join } = require('path');
44
const moment = require('moment');
55
const { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } = require('hexo-fs');
6-
const { highlight, escapeHTML } = require('hexo-util');
6+
const { highlight } = require('hexo-util');
77
const { spy, useFakeTimers } = require('sinon');
88
const { parse: yfm } = require('hexo-front-matter');
99
const fixture = require('../../fixtures/post_render');
@@ -1215,7 +1215,7 @@ describe('Post', () => {
12151215
engine: 'markdown'
12161216
});
12171217

1218-
data.content.trim().should.contains(`<pre><code class="sh">${escapeHTML('echo "Hi"')}\n</code></pre>`);
1218+
data.content.trim().should.contains('<pre><code class="sh">echo "Hi"\n</code></pre>');
12191219
data.content.trim().should.contains('<script src="//gist.github.com/gist_id.js"></script>');
12201220
data.content.trim().should.contains('<script src="//gist.github.com/gist_id_2.js"></script>');
12211221
});

0 commit comments

Comments
 (0)