From 17565f7b1af33a8b71faab5ccb5b185125b4df32 Mon Sep 17 00:00:00 2001 From: Yuxin Wu Date: Sun, 27 Mar 2022 17:55:51 -0700 Subject: [PATCH] Update post.js --- lib/hexo/post.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 7f0db61bbe..68c576123a 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -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'); @@ -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 = []; @@ -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);