From 69a0e2e2dd767fa92912619272827e37bd326563 Mon Sep 17 00:00:00 2001 From: liuhongjiang Date: Thu, 13 Aug 2015 14:53:20 +0800 Subject: [PATCH] Add root path to permalink of post --- lib/models/post.js | 3 ++- test/scripts/helpers/list_archives.js | 14 ++++++++------ test/scripts/helpers/list_categories.js | 14 ++++++++------ test/scripts/helpers/list_posts.js | 12 ++++++------ test/scripts/helpers/list_tags.js | 14 ++++++++------ test/scripts/helpers/tagcloud.js | 14 ++++++++------ test/scripts/models/post.js | 9 +++++++++ 7 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/models/post.js b/lib/models/post.js index 036aece61b..e439673789 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -49,7 +49,8 @@ module.exports = function(ctx){ }); Post.virtual('permalink').get(function(){ - return ctx.config.url + '/' + this.path; + var url_for = ctx.extend.helper.get('url_for'); + return ctx.config.url + url_for.call(ctx, this.path); }); Post.virtual('full_source').get(function(){ diff --git a/test/scripts/helpers/list_archives.js b/test/scripts/helpers/list_archives.js index ffbf90342c..b724f06dc3 100644 --- a/test/scripts/helpers/list_archives.js +++ b/test/scripts/helpers/list_archives.js @@ -22,12 +22,14 @@ describe('list_archives', function(){ } before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)}, - {source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)}, - {source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)}, - {source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)} - ]).then(function(){ + return hexo.init().then(function(){ + return Post.insert([ + {source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)}, + {source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)}, + {source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)}, + {source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)} + ]); + }).then(function(){ resetLocals(); }); }); diff --git a/test/scripts/helpers/list_categories.js b/test/scripts/helpers/list_categories.js index e369ede862..a0ec46d071 100644 --- a/test/scripts/helpers/list_categories.js +++ b/test/scripts/helpers/list_categories.js @@ -18,12 +18,14 @@ describe('list_categories', function(){ var listCategories = require('../../../lib/plugins/helper/list_categories').bind(ctx); before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo'}, - {source: 'bar', slug: 'bar'}, - {source: 'baz', slug: 'baz'}, - {source: 'boo', slug: 'boo'} - ]).then(function(posts){ + return hexo.init().then(function(){ + return Post.insert([ + {source: 'foo', slug: 'foo'}, + {source: 'bar', slug: 'bar'}, + {source: 'baz', slug: 'baz'}, + {source: 'boo', slug: 'boo'} + ]); + }).then(function(posts){ return Promise.each([ ['baz'], ['baz', 'bar'], diff --git a/test/scripts/helpers/list_posts.js b/test/scripts/helpers/list_posts.js index e1d5483d6a..e1da3eaa99 100644 --- a/test/scripts/helpers/list_posts.js +++ b/test/scripts/helpers/list_posts.js @@ -18,12 +18,12 @@ describe('list_posts', function(){ hexo.config.permalink = ':title/'; before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo', title: 'Its', date: 1e8}, - {source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1}, - {source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1} - ]).then(function(){ - return hexo.init(); + return hexo.init().then(function(){ + return Post.insert([ + {source: 'foo', slug: 'foo', title: 'Its', date: 1e8}, + {source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1}, + {source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1} + ]); }).then(function(){ hexo.locals.invalidate(); ctx.site = hexo.locals.toObject(); diff --git a/test/scripts/helpers/list_tags.js b/test/scripts/helpers/list_tags.js index 4ed733d07f..2c0f7a4457 100644 --- a/test/scripts/helpers/list_tags.js +++ b/test/scripts/helpers/list_tags.js @@ -18,12 +18,14 @@ describe('list_tags', function(){ var listTags = require('../../../lib/plugins/helper/list_tags').bind(ctx); before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo'}, - {source: 'bar', slug: 'bar'}, - {source: 'baz', slug: 'baz'}, - {source: 'boo', slug: 'boo'} - ]).then(function(posts){ + return hexo.init().then(function(){ + return Post.insert([ + {source: 'foo', slug: 'foo'}, + {source: 'bar', slug: 'bar'}, + {source: 'baz', slug: 'baz'}, + {source: 'boo', slug: 'boo'} + ]); + }).then(function(posts){ // TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem return Promise.each([ ['foo'], diff --git a/test/scripts/helpers/tagcloud.js b/test/scripts/helpers/tagcloud.js index 662ee33486..cc0787de35 100644 --- a/test/scripts/helpers/tagcloud.js +++ b/test/scripts/helpers/tagcloud.js @@ -18,12 +18,14 @@ describe('tagcloud', function(){ var tagcloud = require('../../../lib/plugins/helper/tagcloud').bind(ctx); before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo'}, - {source: 'bar', slug: 'bar'}, - {source: 'baz', slug: 'baz'}, - {source: 'boo', slug: 'boo'} - ]).then(function(posts){ + return hexo.init().then(function(){ + return Post.insert([ + {source: 'foo', slug: 'foo'}, + {source: 'bar', slug: 'bar'}, + {source: 'baz', slug: 'baz'}, + {source: 'boo', slug: 'boo'} + ]); + }).then(function(posts){ // TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem return Promise.each([ ['bcd'], diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index 525af4d6c4..b2422c9ce9 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -77,6 +77,15 @@ describe('Post', function(){ }); it('permalink - virtual', function(){ + hexo.config.root = '/root/'; + return Post.insert({ + source: 'foo.md', + slug: 'bar' + }).then(function(data){ + data.permalink.should.eql(hexo.config.url + '/root/' + data.path); + return Post.removeById(data._id); + }); + hexo.config.root = '/'; return Post.insert({ source: 'foo.md', slug: 'bar'