From 245f4af3528c1eefd6c964f2a05af542d2f56e85 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 27 Apr 2015 13:26:39 +0800 Subject: [PATCH 1/2] Preserve non-null data in drafts. Fixed #1139 --- lib/hexo/post.js | 63 ++++++++++----------------------- test/scripts/console/new.js | 12 +++---- test/scripts/console/publish.js | 4 +-- test/scripts/hexo/post.js | 44 ++++++++++++++++------- 4 files changed, 57 insertions(+), 66 deletions(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 7e4be11904..c3fdd394cf 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -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; @@ -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; @@ -156,7 +131,11 @@ 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 + '"'; } } @@ -164,12 +143,6 @@ function prepareJFM(data){ return data; } -function prepareYFM(data){ - data.title = '"' + data.title + '"'; - - return data; -} - Post.prototype._getScaffold = function(layout){ var ctx = this.context; diff --git a/test/scripts/console/new.js b/test/scripts/console/new.js index 3b30e9cdc5..e00e8176ef 100644 --- a/test/scripts/console/new.js +++ b/test/scripts/console/new.js @@ -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:', '---' @@ -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'; @@ -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:', '---' @@ -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:', '---' @@ -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'; diff --git a/test/scripts/console/publish.js b/test/scripts/console/publish.js index 42fa74653e..972273d01e 100644 --- a/test/scripts/console/publish.js +++ b/test/scripts/console/publish.js @@ -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:', '---' @@ -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:', '---' diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index e117cc3806..35bcece94f 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -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(){ @@ -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:', '---' @@ -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:', '---' @@ -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:', '---' @@ -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:', '---' @@ -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'; @@ -245,7 +246,7 @@ describe('Post', function(){ }).then(function(post){ post.content.should.eql([ '---', - 'title: "Hello World"', + 'title: Hello World', '---' ].join('\n') + '\n'); @@ -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:', '---', @@ -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:', '---' @@ -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:', '---' @@ -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:', '---' @@ -484,7 +485,7 @@ describe('Post', function(){ }); }); - //https://github.com/hexojs/hexo/issues/1100 + // https://github.com/hexojs/hexo/issues/1100 it('publish() - non-string title', function(){ var path = pathFn.join(hexo.source_dir, '_posts', '12345.md'); @@ -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:', '---' @@ -541,6 +542,23 @@ describe('Post', function(){ }); }); + // https://github.com/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(); From 418095156813d700936988be49172db12d0d697a Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 27 Apr 2015 13:29:57 +0800 Subject: [PATCH 2/2] Post.create: add front-matter escaping test --- test/scripts/hexo/post.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 35bcece94f..75a856bb3d 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -295,6 +295,20 @@ describe('Post', function(){ }); }); + it('create() - escape title', function(){ + return post.create({ + title: 'Foo: Bar' + }).then(function(data){ + data.content.should.eql([ + 'title: "Foo: Bar"', + 'date: ' + moment(now).format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'); + return fs.unlink(data.path); + }); + }); + it('create() - with content', function(){ var path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md'); var date = moment(now);