From 28f25b45d6d65de3497af0f4d447340151b0e0ad Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 21 Apr 2020 17:09:06 +0800 Subject: [PATCH] perf(post): simplify codeblock escape --- lib/hexo/post.js | 11 ++---- .../before_post_render/backtick_code_block.js | 2 +- test/scripts/filters/backtick_code_block.js | 38 +++++++++---------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 1d62074eca..c63a777c29 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -22,12 +22,11 @@ class PostRenderCache { this.cache = []; } - escapeContent(str) { - const rEscapeContent = /]*)>([\s\S]*?)<\/escape>/g; - return str.replace(rEscapeContent, (_, content) => _escapeContent(this.cache, content)); - } - loadContent(str) { + if (str.includes('hexoPostRenderEscape')) { + str = str.replace(//g, ''); + } + const rPlaceholder = /(?:<|<)!--\uFFFC(\d+)--(?:>|>)/g; const restored = str.replace(rPlaceholder, (_, index) => { assert(this.cache[index]); @@ -256,8 +255,6 @@ class Post { // Run "before_post_render" filters return ctx.execFilter('before_post_render', data, {context: ctx}); }).then(() => { - data.content = cacheObj.escapeContent(data.content); - if (isSwig) { // Render with Nunjucks if this is a swig file return tag.render(data.content, data); diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index 5e852d2f09..ca608692ad 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -63,7 +63,7 @@ function backtickCodeBlock(data) { .replace(/{/g, '{') .replace(/}/g, '}'); - return `${start}${content}${end}`; + return `${start}${end}`; }); } diff --git a/test/scripts/filters/backtick_code_block.js b/test/scripts/filters/backtick_code_block.js index 0f0660a232..278c22b48c 100644 --- a/test/scripts/filters/backtick_code_block.js +++ b/test/scripts/filters/backtick_code_block.js @@ -67,7 +67,7 @@ describe('Backtick code block', () => { }; codeBlock(data); - data.content.should.eql('' + highlight(code, {lang: 'js'}) + ''); + data.content.should.eql(''); }); it('without language name', () => { @@ -82,7 +82,7 @@ describe('Backtick code block', () => { const expected = highlight(code); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('without language name - ignore tab character', () => { @@ -97,7 +97,7 @@ describe('Backtick code block', () => { const expected = highlight(code); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('title', () => { @@ -115,7 +115,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('url', () => { @@ -133,7 +133,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('link text', () => { @@ -151,7 +151,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('indent', () => { @@ -171,7 +171,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number false', () => { @@ -191,7 +191,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number false, don`t first_line_number always1', () => { @@ -212,7 +212,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number false, don`t care first_line_number inilne', () => { @@ -233,7 +233,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number true', () => { @@ -253,7 +253,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number, first_line_number always1, js=', () => { @@ -275,7 +275,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number, first_line_number inline, js', () => { @@ -297,7 +297,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number, first_line_number inline, js=1', () => { @@ -319,7 +319,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('line number, first_line_number inline, js=2', () => { @@ -341,7 +341,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('tab replace', () => { @@ -367,7 +367,7 @@ describe('Backtick code block', () => { }); codeBlock(data); - data.content.should.eql('' + expected + ''); + data.content.should.eql(''); }); it('wrap', () => { @@ -382,7 +382,7 @@ describe('Backtick code block', () => { }; codeBlock(data); - data.content.should.eql('' + highlight(code, { lang: 'js', wrap: false }) + ''); + data.content.should.eql(''); hexo.config.highlight.wrap = true; }); @@ -400,7 +400,7 @@ describe('Backtick code block', () => { }; codeBlock(data); - data.content.should.eql('```foo```\n\n' + highlight(code, {}) + ''); + data.content.should.eql('```foo```\n\n'); }); // test for Issue #4190 @@ -418,6 +418,6 @@ describe('Backtick code block', () => { }; codeBlock(data); - data.content.should.eql('' + highlight(code + '\nfoo```\n\nbar```\nbaz', {}) + ''); + data.content.should.eql(''); }); });