Skip to content

Commit

Permalink
Async rendering of tags within a post
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwwyyxx committed May 29, 2022
1 parent afc4d7f commit ff0cfca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
26 changes: 15 additions & 11 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const moment = require('moment');
const parse5 = require('parse5');
const Promise = require('bluebird');
const { join, extname, basename } = require('path');
const { magenta } = require('picocolors');
Expand Down Expand Up @@ -414,18 +415,21 @@ class Post {
text: data.content,
path: source,
engine: data.engine,
toString: true,
onRenderEnd(content) {
// Replace cache data with real contents
data.content = cacheObj.restoreAllSwigTags(content);

// Return content after replace the placeholders
if (disableNunjucks) return data.content;

// Render with Nunjucks
return tag.render(data.content, data);
}
toString: true
}, options);
}).then(content => {
const doc = parse5.parseFragment(content);
// Indepedently render the tags in each top-level node
// asynchronously. This can significantly speed up rendering of slow tags
const results = doc.childNodes.map(async node => {
// Replace cache data with real contents
const content = cacheObj.restoreAllSwigTags(parse5.serializeOuter(node));
// If disabled, return content after replacing the placeholders
if (disableNunjucks) return content;
// Render with Nunjucks
return await tag.render(content, data);
});
return Promise.all(results).then(x => x.join(''));
}).then(content => {
data.content = cacheObj.restoreCodeBlocks(content);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"moment-timezone": "^0.5.34",
"picocolors": "^1.0.0",
"nunjucks": "^3.2.3",
"parse5": "^7.0.0",
"pretty-hrtime": "^1.0.3",
"resolve": "^1.22.0",
"strip-ansi": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { join } = require('path');
const moment = require('moment');
const { readFile, mkdirs, unlink, rmdir, writeFile, exists, stat, listDir } = require('hexo-fs');
const { highlight, escapeHTML } = require('hexo-util');
const { highlight } = require('hexo-util');
const { spy, useFakeTimers } = require('sinon');
const { parse: yfm } = require('hexo-front-matter');
const fixture = require('../../fixtures/post_render');
Expand Down Expand Up @@ -1215,7 +1215,7 @@ describe('Post', () => {
engine: 'markdown'
});

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

0 comments on commit ff0cfca

Please sign in to comment.