From 5605c1460597552ce8169b660467212b3428b80a Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 11 Aug 2020 19:47:59 +0800 Subject: [PATCH] fix(#4460): refactor post escape --- lib/hexo/post.js | 10 ++++++---- test/fixtures/post_render.js | 8 ++++++++ test/scripts/hexo/post.js | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 6508e8fec4..618ae7231c 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -12,6 +12,7 @@ const { parse: yfmParse, split: yfmSplit, stringify: yfmStringify } = require('h const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content']; const rPlaceholder = /(?:<|<)!--\uFFFC(\d+)--(?:>|>)/g; +const rHexoPostRenderEscape = //g; const rSwigVarAndComment = /{[{#][\s\S]+?[}#]}/g; const rSwigFullBlock = /{% *(\S+?)(?: *| +.+?)%}[\s\S]+?{% *end\1 *%}/g; const rSwigBlock = /{%[\s\S]+?%}/g; @@ -24,10 +25,6 @@ class PostRenderCache { } loadContent(str) { - if (str.includes('hexoPostRenderEscape')) { - str = str.replace(//g, ''); - } - const restored = str.replace(rPlaceholder, (_, index) => { assert(this.cache[index]); const value = this.cache[index]; @@ -38,6 +35,10 @@ class PostRenderCache { return this.loadContent(restored); // self-recursive for nexted escaping } + escapeContent(str) { + return str.replace(rHexoPostRenderEscape, (_, content) => _escapeContent(this.cache, content)); + } + escapeAllSwigTags(str) { const escape = _str => _escapeContent(this.cache, _str); return str.replace(rSwigVarAndComment, escape) // Remove swig comment first to reduce string size being matched next @@ -247,6 +248,7 @@ class Post { // Run "before_post_render" filters return ctx.execFilter('before_post_render', data, { context: ctx }); }).then(() => { + data.content = cacheObj.escapeContent(data.content); // Escape all Nunjucks/Swig tags if (!disableNunjucks) { data.content = cacheObj.escapeAllSwigTags(data.content); diff --git a/test/fixtures/post_render.js b/test/fixtures/post_render.js index d8f4cca4c5..5e7d9b938f 100644 --- a/test/fixtures/post_render.js +++ b/test/fixtures/post_render.js @@ -90,3 +90,11 @@ exports.content_for_issue_4317 = [ 'echo "Hi"', '```' ].join('\n'); + +exports.content_for_issue_4460 = [ + '```html', + '', + '', + '', + '```' +].join('\n'); diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index de412cd06b..973e0c5bce 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -1169,4 +1169,22 @@ describe('Post', () => { data.content.should.not.contains('&#123'); data.content.should.not.contains('&#125'); }); + + it('render() - issue #4460', async () => { + hexo.config.prismjs.enable = true; + hexo.config.highlight.enable = false; + + const content = fixture.content_for_issue_4460; + // console.log(content); + + const data = await post.render(null, { + content, + engine: 'markdown' + }); + + data.content.should.not.include('hexoPostRenderEscape'); + + hexo.config.prismjs.enable = false; + hexo.config.highlight.enable = true; + }); });