Skip to content

Commit

Permalink
fix(#4460): refactor post escape
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 11, 2020
1 parent ec1e39f commit 5605c14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { parse: yfmParse, split: yfmSplit, stringify: yfmStringify } = require('h
const preservedKeys = ['title', 'slug', 'path', 'layout', 'date', 'content'];

const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
const rHexoPostRenderEscape = /<!--hexoPostRenderEscape:([\s\S]+?):hexoPostRenderEscape-->/g;
const rSwigVarAndComment = /{[{#][\s\S]+?[}#]}/g;
const rSwigFullBlock = /{% *(\S+?)(?: *| +.+?)%}[\s\S]+?{% *end\1 *%}/g;
const rSwigBlock = /{%[\s\S]+?%}/g;
Expand All @@ -24,10 +25,6 @@ class PostRenderCache {
}

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

const restored = str.replace(rPlaceholder, (_, index) => {
assert(this.cache[index]);
const value = this.cache[index];
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/post_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ exports.content_for_issue_4317 = [
'echo "Hi"',
'```'
].join('\n');

exports.content_for_issue_4460 = [
'```html',
'<body>',
'<!-- here goes the rest of the page -->',
'</body>',
'```'
].join('\n');
18 changes: 18 additions & 0 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,4 +1169,22 @@ describe('Post', () => {
data.content.should.not.contains('&amp;#123');
data.content.should.not.contains('&amp;#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;
});
});

0 comments on commit 5605c14

Please sign in to comment.