Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(post): simplify codeblock escape #4254

Merged
merged 2 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class PostRenderCache {
this.cache = [];
}

escapeContent(str) {
const rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
return str.replace(rEscapeContent, (_, content) => _escapeContent(this.cache, content));
}

loadContent(str) {
if (str.includes('hexoPostRenderEscape')) {
str = str.replace(/<!--hexoPostRenderEscape:/g, '').replace(/:hexoPostRenderEscape-->/g, '');
}

const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
const restored = str.replace(rPlaceholder, (_, index) => {
assert(this.cache[index]);
Expand Down Expand Up @@ -254,8 +253,6 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function backtickCodeBlock(data) {
.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');

return `${start}<escape>${content}</escape>${end}`;
return `${start}<!--hexoPostRenderEscape:${content}:hexoPostRenderEscape-->${end}`;
});
}

Expand Down
38 changes: 19 additions & 19 deletions test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('<escape>' + highlight(code, {lang: 'js'}) + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + highlight(code, {lang: 'js'}) + ':hexoPostRenderEscape-->');
});

it('without language name', () => {
Expand All @@ -82,7 +82,7 @@ describe('Backtick code block', () => {
const expected = highlight(code);

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('without language name - ignore tab character', () => {
Expand All @@ -97,7 +97,7 @@ describe('Backtick code block', () => {
const expected = highlight(code);

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('title', () => {
Expand All @@ -115,7 +115,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('url', () => {
Expand All @@ -133,7 +133,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('link text', () => {
Expand All @@ -151,7 +151,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('indent', () => {
Expand All @@ -171,7 +171,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number false', () => {
Expand All @@ -191,7 +191,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number false, don`t first_line_number always1', () => {
Expand All @@ -212,7 +212,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number false, don`t care first_line_number inilne', () => {
Expand All @@ -233,7 +233,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number true', () => {
Expand All @@ -253,7 +253,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number, first_line_number always1, js=', () => {
Expand All @@ -275,7 +275,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number, first_line_number inline, js', () => {
Expand All @@ -297,7 +297,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number, first_line_number inline, js=1', () => {
Expand All @@ -319,7 +319,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('line number, first_line_number inline, js=2', () => {
Expand All @@ -341,7 +341,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('tab replace', () => {
Expand All @@ -367,7 +367,7 @@ describe('Backtick code block', () => {
});

codeBlock(data);
data.content.should.eql('<escape>' + expected + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + expected + ':hexoPostRenderEscape-->');
});

it('wrap', () => {
Expand All @@ -382,7 +382,7 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('<escape>' + highlight(code, { lang: 'js', wrap: false }) + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + highlight(code, { lang: 'js', wrap: false }) + ':hexoPostRenderEscape-->');

hexo.config.highlight.wrap = true;
});
Expand All @@ -400,7 +400,7 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('```foo```\n\n<escape>' + highlight(code, {}) + '</escape>');
data.content.should.eql('```foo```\n\n<!--hexoPostRenderEscape:' + highlight(code, {}) + ':hexoPostRenderEscape-->');
});

// test for Issue #4190
Expand All @@ -418,6 +418,6 @@ describe('Backtick code block', () => {
};

codeBlock(data);
data.content.should.eql('<escape>' + highlight(code + '\nfoo```\n\nbar```\nbaz', {}) + '</escape>');
data.content.should.eql('<!--hexoPostRenderEscape:' + highlight(code + '\nfoo```\n\nbar```\nbaz', {}) + ':hexoPostRenderEscape-->');
});
});