Skip to content

Commit d6dd70c

Browse files
committed
Add root path to permalink of post
This commit fixs this problem, when the root path is set to a subdirectory, there is no this root path in the permalink of post.
1 parent 7dcc719 commit d6dd70c

File tree

7 files changed

+52
-31
lines changed

7 files changed

+52
-31
lines changed

lib/models/post.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = function(ctx) {
4747
});
4848

4949
Post.virtual('permalink').get(function() {
50-
return ctx.config.url + '/' + this.path;
50+
var url_for = ctx.extend.helper.get('url_for');
51+
return ctx.config.url + url_for.call(ctx, this.path);
5152
});
5253

5354
Post.virtual('full_source').get(function() {

test/scripts/helpers/list_archives.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ describe('list_archives', function() {
2222
}
2323

2424
before(function() {
25-
return Post.insert([
26-
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
27-
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
28-
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
29-
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
30-
]).then(function() {
25+
return hexo.init().then(function() {
26+
return Post.insert([
27+
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
28+
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
29+
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
30+
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
31+
]);
32+
}).then(function() {
3133
resetLocals();
3234
});
3335
});

test/scripts/helpers/list_categories.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('list_categories', function() {
1818
var listCategories = require('../../../lib/plugins/helper/list_categories').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
return Promise.each([
2830
['baz'],
2931
['baz', 'bar'],

test/scripts/helpers/list_posts.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ describe('list_posts', function() {
1818
hexo.config.permalink = ':title/';
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
23-
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
24-
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
25-
]).then(function() {
26-
return hexo.init();
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
24+
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
25+
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
26+
]);
2727
}).then(function() {
2828
hexo.locals.invalidate();
2929
ctx.site = hexo.locals.toObject();

test/scripts/helpers/list_tags.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('list_tags', function() {
1818
var listTags = require('../../../lib/plugins/helper/list_tags').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
// TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
2830
return Promise.each([
2931
['foo'],

test/scripts/helpers/tagcloud.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('tagcloud', function() {
1818
var tagcloud = require('../../../lib/plugins/helper/tagcloud').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
// TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
2830
return Promise.each([
2931
['bcd'],

test/scripts/models/post.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe('Post', function() {
7777
});
7878

7979
it('permalink - virtual', function() {
80+
hexo.config.root = '/';
8081
return Post.insert({
8182
source: 'foo.md',
8283
slug: 'bar'
@@ -86,6 +87,17 @@ describe('Post', function() {
8687
});
8788
});
8889

90+
it('permalink_prefix - virtual', function() {
91+
hexo.config.root = '/root/';
92+
return Post.insert({
93+
source: 'foo.md',
94+
slug: 'bar'
95+
}).then(function(data) {
96+
data.permalink.should.eql(hexo.config.url + '/root/' + data.path);
97+
return Post.removeById(data._id);
98+
});
99+
});
100+
89101
it('full_source - virtual', function() {
90102
return Post.insert({
91103
source: 'foo.md',

0 commit comments

Comments
 (0)