Skip to content

Commit

Permalink
Fixed post.render
Browse files Browse the repository at this point in the history
- Escape braces in code block in order to avoid rendering errors.
- Wrap placeholders with `<!-- -->` so marked won't generate <p> tags
- Escape Swig tags after before_post_render filters are executed
- Don't add raw tags automatically. It may cause more problems.
- Respect canonical_path in is_home helper
  • Loading branch information
tommy351 committed Feb 3, 2015
1 parent 1621afd commit 683fd0a
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 44 deletions.
1 change: 0 additions & 1 deletion assets/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ title: Hexo
subtitle:
description:
author: John Doe
email:
language:
timezone:

Expand Down
2 changes: 1 addition & 1 deletion assets/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ db.json
*.log
node_modules/
public/
.deploy/
.deploy*/
2 changes: 1 addition & 1 deletion lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ NunjucksBlock.prototype.run = function(context, args, body){
};

function trimBody(body){
return stripIndent(body().trim());
return stripIndent(body()).trim();
}

function NunjucksAsyncTag(name, fn){
Expand Down
1 change: 0 additions & 1 deletion lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
subtitle: '',
description: '',
author: 'John Doe',
email: '',
language: '',
timezone: '',
// URL
Expand Down
2 changes: 0 additions & 2 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,6 @@ Hexo.prototype._generate = function(options){
Hexo.prototype.exit = function(err){
var self = this;

this.log.debug('About to exit');

if (err){
this.log.fatal(
{err: err},
Expand Down
31 changes: 13 additions & 18 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ var escapeRegExp = util.escapeRegExp;

var rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
var rSwigVar = /\{\{[\s\S]*?\}\}/g;
var rSwigVarRaw = /\{% *raw * %\}\{\{[\s\S]*?\}\}\{% *endraw *%\}/g;
var rSwigComment = /\{#[\s\S]*?#\}/g;
var rSwigBlock = /\{%[\s\S]*?%\}/g;
var rSwigFullBlock = /\{% *(.*?) *.*?%\}[\s\S]+?\{% *end\1 *%\}/g;
var rSwigFullBlock = /\{% *(.+?)(?: *| +.*)%\}[\s\S]+?\{% *end\1 *%\}/g;
var placeholder = '\uFFFC';
var rPlaceholder = /(?:<p>)?\uFFFC(\d+)(?:<\/p>)?/g;
var rPlaceholder = /(?:<|&lt;)\!--\uFFFC(\d+)--(?:>|&gt;)/g;

var preservedKeys = {
title: true,
Expand Down Expand Up @@ -269,7 +268,8 @@ Post.prototype.render = function(source, data, callback){
data = data || {};

function escapeContent(str){
return placeholder + (cache.push(str) - 1);
// return placeholder + (cache.push(str) - 1);
return '<!--' + placeholder + (cache.push(str) - 1) + '-->';
}

return new Promise(function(resolve, reject){
Expand All @@ -279,30 +279,25 @@ Post.prototype.render = function(source, data, callback){
// Read content from files
fs.readFile(source).then(resolve, reject);
}).then(function(content){
if (isSwig){
data.content = content;
} else {
// Escape all Swig tags
data.content = content
.replace(rSwigComment, '')
.replace(rSwigVarRaw, escapeContent)
.replace(rSwigVar, function(str){
return escapeContent('{% raw %}' + str + '{% endraw %}');
})
.replace(rSwigFullBlock, escapeContent)
.replace(rSwigBlock, escapeContent);
}
data.content = content;

// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, {context: ctx}).then(function(){
data.content = data.content.replace(rEscapeContent, function(match, content){
return placeholder + (cache.push(content) - 1);
return escapeContent(content);
});
});
}).then(function(){
// Skip rendering if this is a swig file
if (isSwig) return data.content;

// Escape all Swig tags
data.content = data.content
.replace(rSwigFullBlock, escapeContent)
.replace(rSwigBlock, escapeContent)
.replace(rSwigComment, '')
.replace(rSwigVar, escapeContent);

var options = data.markdown || {};
if (!config.highlight.enable) options.highlight = null;

Expand Down
2 changes: 1 addition & 1 deletion lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function pickID(data){

module.exports = function(ctx){
var Post = new Schema({
id: Number,
id: String,
title: {type: String, default: ''},
date: {
type: Moment,
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function generateConsole(args){

if (!cache) cache = Cache.new({_id: id});
cache.shasum = shasum;
cache.modified = Date.now();

return cache.save().thenReturn(stream);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function backtickCodeBlock(data){
}
}

content = highlight(stripIndent(content), options);
content = highlight(stripIndent(content), options)
.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');

return start + '<escape>' + content + '</escape>' + (end ? '\n\n' : '');
});
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/helper/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isCurrentHelper(path, strict){

function isHomeHelper(){
/* jshint validthis: true */
var path = this.path;
var path = this.page.canonical_path || this.path;

if (!path || path === 'index.html') return true;

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/renderer/swig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var swig = require('swig');
var forTag = require('swig/lib/tags/for');

swig.setDefaults({
cache: false
cache: false,
autoescape: false
});

// Hack: Override for tag of Swig
Expand Down
9 changes: 7 additions & 2 deletions lib/plugins/tag/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ module.exports = function(ctx){
caption = '<span>' + match[1] + '</span>';
}

content = stripIndent(content.trim());
content = stripIndent(content).trim();

return highlight(content, {
content = highlight(content, {
lang: lang,
caption: caption,
gutter: config.line_number,
tab: config.tab_replace
});

content = content.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');

return content;
};
};
9 changes: 4 additions & 5 deletions test/fixtures/post_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
var util = require('hexo-util');

var code = [
'if (tired && night){',
' sleep();',
'}',
'if tired && night:',
' sleep()'
].join('\n');

var content = [
'# Title',
'``` js',
'``` python',
code,
'```',
'some content',
Expand All @@ -29,7 +28,7 @@ exports.content = content;

exports.expected = [
'<h1 id="Title">Title</h1>',
util.highlight(code, {lang: 'js'}),
util.highlight(code, {lang: 'python'}),
'\n<p>some content</p>\n',
'<h2 id="Another_title">Another title</h2>',
'<blockquote>',
Expand Down
7 changes: 6 additions & 1 deletion test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var should = require('chai').should();
var util = require('hexo-util');
var _ = require('lodash');
var highlight = util.highlight;
var defaultConfig = require('../../../lib/hexo/default_config');

describe('Backtick code block', function(){
Expand All @@ -17,6 +16,12 @@ describe('Backtick code block', function(){
'}'
].join('\n');

function highlight(code, options){
return util.highlight(code, options || {})
.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');
}

beforeEach(function(){
// Reset config
hexo.config.highlight = _.clone(defaultConfig.highlight);
Expand Down
14 changes: 10 additions & 4 deletions test/scripts/helpers/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ describe('is', function(){
it('is_home', function(){
var paginationDir = hexo.config.pagination_dir;

is.home.call({path: '', config: hexo.config}).should.be.true;
is.home.call({path: paginationDir + '/2/', config: hexo.config}).should.be.true;
is.home.call({path: 'index.html', config: hexo.config}).should.be.true;
is.home.call({path: paginationDir + '/2/index.html', config: hexo.config}).should.be.true;
is.home.call({path: '', config: hexo.config, page: {}}).should.be.true;
is.home.call({path: paginationDir + '/2/', config: hexo.config, page: {}}).should.be.true;
is.home.call({path: 'index.html', config: hexo.config, page: {}}).should.be.true;
is.home.call({path: paginationDir + '/2/index.html', config: hexo.config, page: {}}).should.be.true;

is.home.call({
path: 'zh-tw/index.html',
config: hexo.config,
page: {canonical_path: 'index.html'}
}).should.be.true;
});

it('is_post', function(){
Expand Down
32 changes: 29 additions & 3 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,39 @@ describe('Post', function(){
});
});

it('render() - wrap variable with raw block if it\'s not a swig file', function(){
var content = 'Hello {{ world }}!';
it('render() - escaping swig blocks with similar names', function(){
var code = 'alert("Hello world")';
var highlighted = util.highlight(code);

var content = [
'{% codeblock %}',
code,
'{% endcodeblock %}',
'',
'{% code %}',
code,
'{% endcode %}'
].join('\n');

return post.render(null, {
content: content
}).then(function(data){
data.content.should.eql(content);
data.content.trim().should.eql([
highlighted,
'',
highlighted
].join('\n'));
});
});

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

return post.render(null, {
content: content,
engine: 'markdown'
}).then(function(data){
data.content.trim().should.eql('<p><code>{{ test }}</code></p>');
});
});
});
8 changes: 7 additions & 1 deletion test/scripts/tags/code.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var should = require('chai').should();
var highlight = require('hexo-util').highlight;
var util = require('hexo-util');

describe('code', function(){
var Hexo = require('../../../lib/hexo');
Expand All @@ -18,6 +18,12 @@ describe('code', function(){
return codeTag(args.split(' '), content);
}

function highlight(code, options){
return util.highlight(code, options || {})
.replace(/{/g, '&#123;')
.replace(/}/g, '&#125;');
}

it('default', function(){
var result = code('', fixture);
result.should.eql(highlight(fixture));
Expand Down

0 comments on commit 683fd0a

Please sign in to comment.