Skip to content

Commit

Permalink
refactor/perf(tag): use escape instead of reserve
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 19, 2020
1 parent 6f15416 commit f439327
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
14 changes: 4 additions & 10 deletions lib/extend/tag.js
Expand Up @@ -4,9 +4,8 @@ const { stripIndent } = require('hexo-util');
const { cyan } = require('chalk');
const { Environment } = require('nunjucks');
const Promise = require('bluebird');
const placeholder = '\uFFFC';
const rPlaceholder = /<!--\uFFFC(\d+)-->/g;
const rSwigRawFullBlock = /{% *raw *%}[\s\S]+{% *endraw *%}/;
const escapeSwigTag = str => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');

class NunjucksTag {
constructor(name, fn) {
Expand Down Expand Up @@ -229,20 +228,15 @@ class Tag {
options = {};
}

const cache = [];

const escapeContent = str => {
str = str.replace(/<code[^<>]*>[\s\S]+?<\/code>/gm, str => {
// https://hexo.io/docs/tag-plugins#Raw
// Only escape code block when there is no raw tag included
if (rSwigRawFullBlock.test(str)) return str;
return `<!--${placeholder}${cache.push(str) - 1}-->`;
};

str = str.replace(/<code[^<>]*>[\s\S]+?<\/code>/gm, escapeContent);
return escapeSwigTag(str);
});

return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); })
.catch(err => Promise.reject(formatNunjucksError(err, str)))
.then(result => result.replace(rPlaceholder, (_, index) => cache[index]))
.asCallback(callback);
}
}
Expand Down
11 changes: 6 additions & 5 deletions test/scripts/hexo/post.js
Expand Up @@ -8,6 +8,7 @@ const { highlight, escapeHTML } = require('hexo-util');
const { spy, useFakeTimers } = require('sinon');
const frontMatter = require('hexo-front-matter');
const fixture = require('../../fixtures/post_render');
const escapeSwigTag = str => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');

describe('Post', () => {
const Hexo = require('../../../lib/hexo');
Expand Down Expand Up @@ -1015,7 +1016,7 @@ describe('Post', () => {
engine: 'markdown'
});

data.content.trim().should.eql('<p>In Go’s templates, blocks look like this: <code>{{block "template name" .}} (content) {{end}}</code>.</p>');
data.content.trim().should.eql(`<p>In Go’s templates, blocks look like this: <code>${escapeSwigTag('{{block "template name" .}} (content) {{end}}')}</code>.</p>`);
});

// test for https://github.com/hexojs/hexo/issues/3346#issuecomment-595497849
Expand All @@ -1027,7 +1028,7 @@ describe('Post', () => {
engine: 'markdown'
});

data.content.trim().should.eql('<p><code>{{ 1 + 1 }}</code> 2</p>');
data.content.trim().should.eql(`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 2</p>`);
});

// https://github.com/hexojs/hexo/issues/4317
Expand Down Expand Up @@ -1099,7 +1100,7 @@ describe('Post', () => {
});

data.content.trim().should.eql([
'<p><code>{{ 1 + 1 }}</code> 3 <code>{{ 2 + 2 }}</code><br>Text</p>',
`<p><code>${escapeSwigTag('{{ 1 + 1 }}')}</code> 3 <code>${escapeSwigTag('{{ 2 + 2 }}')}</code><br>Text</p>`,
'',
'Raw 1',
'',
Expand Down Expand Up @@ -1136,13 +1137,13 @@ describe('Post', () => {
});

// indented pullquote
data.content.trim().should.contains('<pre><code>{% pullquote %}foo foo foo{% endpullquote %}</code></pre>');
data.content.trim().should.contains(`<pre><code>${escapeSwigTag('{% pullquote %}foo foo foo{% endpullquote %}')}</code></pre>`);
data.content.trim().should.contains('<p>test001</p>');
// pullquote tag
data.content.trim().should.contains('<blockquote class="pullquote"><p>bar bar bar</p>\n</blockquote>');
data.content.trim().should.contains('<p>test002</p>');
// indented youtube tag
data.content.trim().should.contains('<pre><code>{% youtube https://example.com/demo.mp4 %}</code></pre>');
data.content.trim().should.contains(`<pre><code>${escapeSwigTag('{% youtube https://example.com/demo.mp4 %}')}</code></pre>`);
// youtube tag
data.content.trim().should.contains('<div class="video-container"><iframe src="https://www.youtube.com/embed/https://example.com/sample.mp4" frameborder="0" loading="lazy" allowfullscreen></iframe></div>');
});
Expand Down

0 comments on commit f439327

Please sign in to comment.