Skip to content

Commit

Permalink
Preserve non-null data in drafts. Fixed #1139
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Apr 27, 2015
1 parent 1a16481 commit 245f4af
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 66 deletions.
63 changes: 18 additions & 45 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,67 +60,44 @@ Post.prototype.create = function(data, replace, callback){
// Get the scaffold
this._getScaffold(data.layout)
]).spread(function(path, scaffold){
data.date = data.date.format('YYYY-MM-DD HH:mm:ss');

// Split data part from the raw scaffold
var split = yfm.split(scaffold);
var separator = split.separator || '---';
var jsonMode = separator[0] === ';';

var frontMatter;

if (jsonMode){
frontMatter = prepareJFM(_.clone(data));
} else {
frontMatter = prepareYFM(_.clone(data));
}
var frontMatter = prepareFrontMatter(_.clone(data));
var content = '';

// Compile front-matter with data
var content = swig.compile(split.data)(frontMatter) + '\n';
var renderedData = swig.compile(split.data)(frontMatter);

// Parse front-matter
var compiled;

if (jsonMode){
compiled = JSON.parse('{' + content + '}');
compiled = JSON.parse('{' + renderedData + '}');
} else {
compiled = yaml.load(content);
compiled = yaml.load(renderedData);
}

// Add data which are not in the front-matter
var keys = Object.keys(data);
var key = '';
var obj = {};
var obj = compiled;

for (var i = 0, len = keys.length; i < len; i++){
key = keys[i];

if (!preservedKeys[key] && !compiled.hasOwnProperty(key)){
if (!preservedKeys[key] && obj[key] == null){
obj[key] = data[key];
}
}

if (Object.keys(obj).length){
if (jsonMode){
if (content){
content = content.trim() + ',\n';
}

content += JSON.stringify(obj, null, ' ')
// Remove indention
.replace(/\n {2}/g, function(){
return '\n';
})
// Remove prefixing and trailing braces
.replace(/^{\n|}$/g, '');
} else {
content += yaml.dump(obj);
}
}
// Prepend the separator
if (split.prefixSeparator) content += separator + '\n';

// Add separators
if (split.prefixSeparator) content = separator + '\n' + content;
content += separator + '\n';
content += yfm.stringify(obj, {
mode: jsonMode ? 'json' : ''
});

// Concat content
content += split.content;
Expand All @@ -145,9 +122,7 @@ Post.prototype.create = function(data, replace, callback){
}).nodeify(callback);
};

// Prepare data for JSON front-matter:
// - Add quotations for strings
function prepareJFM(data){
function prepareFrontMatter(data){
var keys = Object.keys(data);
var key = '';
var item;
Expand All @@ -156,20 +131,18 @@ function prepareJFM(data){
key = keys[i];
item = data[key];

if (typeof item === 'string'){
if (moment.isMoment(item)){
data[key] = item.utc().format('YYYY-MM-DD HH:mm:ss');
} else if (moment.isDate(item)){
data[key] = moment.utc(item).format('YYYY-MM-DD HH:mm:ss');
} else if (typeof item === 'string'){
data[key] = '"' + item + '"';
}
}

return data;
}

function prepareYFM(data){
data.title = '"' + data.title + '"';

return data;
}

Post.prototype._getScaffold = function(layout){
var ctx = this.context;

Expand Down
12 changes: 6 additions & 6 deletions test/scripts/console/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('new', function(){
var date = moment(now);
var path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
var body = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -64,7 +64,7 @@ describe('new', function(){
it('layout', function(){
var path = pathFn.join(hexo.source_dir, '_drafts', 'Hello-World.md');
var body = [
'title: "Hello World"',
'title: Hello World',
'tags:',
'---',
].join('\n') + '\n';
Expand All @@ -83,7 +83,7 @@ describe('new', function(){
var date = moment(now);
var path = pathFn.join(hexo.source_dir, '_posts', 'foo.md');
var body = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -104,7 +104,7 @@ describe('new', function(){
var date = moment(now);
var path = pathFn.join(hexo.source_dir, '_posts', 'bar.md');
var body = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand Down Expand Up @@ -165,10 +165,10 @@ describe('new', function(){
var date = moment(now);
var path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
var body = [
'title: "Hello World"',
'title: Hello World',
'foo: bar',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'foo: bar',
'---'
].join('\n') + '\n';

Expand Down
4 changes: 2 additions & 2 deletions test/scripts/console/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('publish', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -81,7 +81,7 @@ describe('publish', function(){

var content = [
'layout: photo',
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand Down
44 changes: 31 additions & 13 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Promise = require('bluebird');
var fs = require('hexo-fs');
var util = require('hexo-util');
var sinon = require('sinon');
var frontMatter = require('hexo-front-matter');
var fixture = require('../../fixtures/post_render');

describe('Post', function(){
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('Post', function(){
var listener = sinon.spy();

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -78,7 +79,7 @@ describe('Post', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -105,7 +106,7 @@ describe('Post', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -131,7 +132,7 @@ describe('Post', function(){

var content = [
'layout: photo',
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -156,10 +157,10 @@ describe('Post', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'foo: bar',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'foo: bar',
'---'
].join('\n') + '\n';

Expand Down Expand Up @@ -245,7 +246,7 @@ describe('Post', function(){
}).then(function(post){
post.content.should.eql([
'---',
'title: "Hello World"',
'title: Hello World',
'---'
].join('\n') + '\n');

Expand Down Expand Up @@ -299,7 +300,7 @@ describe('Post', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---',
Expand Down Expand Up @@ -330,7 +331,7 @@ describe('Post', function(){
});

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand All @@ -353,7 +354,7 @@ describe('Post', function(){
var date = moment(now);

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand Down Expand Up @@ -390,7 +391,7 @@ describe('Post', function(){

var content = [
'layout: photo',
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand Down Expand Up @@ -484,7 +485,7 @@ describe('Post', function(){
});
});

//https:/hexojs/hexo/issues/1100
// https:/hexojs/hexo/issues/1100
it('publish() - non-string title', function(){
var path = pathFn.join(hexo.source_dir, '_posts', '12345.md');

Expand All @@ -511,7 +512,7 @@ describe('Post', function(){
});

var content = [
'title: "Hello World"',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
Expand Down Expand Up @@ -541,6 +542,23 @@ describe('Post', function(){
});
});

// https:/hexojs/hexo/issues/1139
it('publish() - preserve non-null data in drafts', function(){
return post.create({
title: 'foo',
layout: 'draft',
tags: ['tag', 'test']
}).then(function(data){
return post.publish({
slug: 'foo'
});
}).then(function(data){
var meta = frontMatter(data.content);
meta.tags.should.eql(['tag', 'test']);
return fs.unlink(data.path);
});
});

it('render()', function(){
// TODO: validate data
var beforeHook = sinon.spy();
Expand Down

0 comments on commit 245f4af

Please sign in to comment.