Skip to content

Commit

Permalink
Update post.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwwyyxx committed Mar 28, 2022
1 parent ba81258 commit 17565f7
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('assert');
const moment = require('moment');
const Promise = require('bluebird');

const { join, extname, basename } = require('path');
const { magenta } = require('picocolors');
const { load } = require('js-yaml');
Expand All @@ -29,6 +30,22 @@ const isNonWhiteSpaceChar = char => char !== '\r'
&& char !== '\v'
&& char !== ' ';

function replaceAsync(string, searchValue, replacer) {
// 1. Run fake pass of `replace`, collect values from `replacer` calls
// 2. Resolve them with `Promise.all`
// 3. Run `replace` with resolved values
var values = [];
String.prototype.replace.call(string, searchValue, function () {
values.push(replacer.apply(undefined, arguments));
return "";
});
return Promise.all(values).then(function (resolvedValues) {
return String.prototype.replace.call(string, searchValue, function () {
return resolvedValues.shift();
});
});
}

class PostRenderEscape {
constructor() {
this.stored = [];
Expand Down Expand Up @@ -416,16 +433,16 @@ class Post {
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);
return content;
}
}, options);
}).then(content => {
return replaceAsync(content, rSwigPlaceHolder, async(match, name) => {
let ret = PostRenderEscape.restoreContent(cacheObj.stored)(match, name);
if (disableNunjucks) return ret;
ret = await tag.render(ret, data);
return ret;
});
}).then(content => {
data.content = cacheObj.restoreCodeBlocks(content);

Expand Down

0 comments on commit 17565f7

Please sign in to comment.