Skip to content

Commit

Permalink
Fix error when tag.render <code> (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and NoahDragon committed Apr 12, 2017
1 parent 30b5bc2 commit 5134cba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Tag.prototype.render = function(str, options, callback) {
var env = this.env;

return new Promise(function(resolve, reject) {
str = str.replace(/<pre><code.*>[\s\S]*?<\/code><\/pre>/gm, escapeContent);
str = str.replace(/(?:<pre>)?<code.*>[\s\S]*?<\/code>(?:<\/pre>)?/gm, escapeContent);
env.renderString(str, options, function(err, result) {
if (err) return reject(err);
resolve(result.replace(rPlaceholder, function() {
Expand Down
16 changes: 16 additions & 0 deletions test/scripts/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,20 @@ describe('Tag', () => {
result.should.eql('bar');
});
});

it('render() - ignore <pre><code>', function() {
var str = '<pre><code>{{ helper.json() }}</code></pre>';

return tag.render(str).then(function(result) {
result.should.eql(str);
});
});

it('render() - ignore <code>', function() {
var str = '<code>{{ helper.json() }}</code>';

return tag.render(str).then(function(result) {
result.should.eql(str);
});
});
});
4 changes: 2 additions & 2 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,15 @@ describe('Post', () => {
content,
engine: 'markdown'
}).then(data => {
data.content.trim().should.eql('<p><code>{{ test }}</code></p>');
data.content.trim().should.eql('<p><code>{% raw %}{{ test }}{% endraw %}</code></p>');
});
});

it('render() - recover escaped swig blocks which is html escaped before post_render', () => {
var content = '`{% raw %}{{ test }}{% endraw %}`';

var filter = sinon.spy(result => {
result.trim().should.eql('<p><code>{{ test }}</code></p>');
result.trim().should.eql('<p><code>{% raw %}{{ test }}{% endraw %}</code></p>');
});

hexo.extend.filter.register('after_render:html', filter);
Expand Down

0 comments on commit 5134cba

Please sign in to comment.