forked from hexojs/hexo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_permalink.js
174 lines (144 loc) · 4.88 KB
/
post_permalink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
const moment = require('moment');
describe('post_permalink', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const postPermalink = require('../../../lib/plugins/filter/post_permalink').bind(hexo);
const Post = hexo.model('Post');
let post;
before(async () => {
hexo.config.permalink = ':year/:month/:day/:title/';
hexo.config.permalink_defaults = {};
await hexo.init();
const apost = await Post.insert({
source: 'foo.md',
slug: 'foo',
date: moment('2014-01-02')
});
const id = apost._id;
await apost.setCategories(['foo', 'bar']);
post = Post.findById(id);
});
it('default', () => {
postPermalink(post).should.eql('2014/01/02/foo/');
});
it('categories', () => {
hexo.config.permalink = ':category/:title/';
postPermalink(post).should.eql('foo/bar/foo/');
});
it('uncategorized', async () => {
hexo.config.permalink = ':category/:title/';
const post = await Post.insert({
source: 'bar.md',
slug: 'bar'
});
postPermalink(post).should.eql(hexo.config.default_category + '/bar/');
Post.removeById(post._id);
});
it('extra data', () => {
hexo.config.permalink = ':layout/:title/';
postPermalink(post).should.eql(post.layout + '/foo/');
});
it('id', () => {
hexo.config.permalink = ':id';
postPermalink(post).should.eql(post._id);
post.id = 1;
postPermalink(post).should.eql('1');
});
it('name', async () => {
hexo.config.permalink = ':title/:name';
const post = await Post.insert({
source: 'sub/bar.md',
slug: 'sub/bar'
});
postPermalink(post).should.eql('sub/bar/bar');
Post.removeById(post._id);
});
it('post_title', async () => {
hexo.config.permalink = ':year/:month/:day/:post_title/';
const post = await Post.insert({
source: 'sub/2015-05-06-my-new-post.md',
slug: '2015-05-06-my-new-post',
title: 'My New Post',
date: moment('2015-05-06')
});
postPermalink(post).should.eql('2015/05/06/my-new-post/');
Post.removeById(post._id);
});
it('hour minute and second', async () => {
hexo.config.permalink = ':year/:month/:day/:hour/:minute/:second/:post_title/';
const post = await Post.insert({
source: 'sub/2015-05-06-my-new-post.md',
slug: '2015-05-06-my-new-post',
title: 'My New Post',
date: moment('2015-05-06 12:13:14')
});
postPermalink(post).should.eql('2015/05/06/12/13/14/my-new-post/');
Post.removeById(post._id);
});
it('time is omitted in front-matter', async () => {
hexo.config.permalink = ':year/:month/:day/:hour/:minute/:second/:post_title/';
const post = await Post.insert({
source: 'sub/2015-05-06-my-new-post.md',
slug: '2015-05-06-my-new-post',
title: 'My New Post',
date: moment('2015-05-06')
});
postPermalink(post).should.eql('2015/05/06/00/00/00/my-new-post/');
Post.removeById(post._id);
});
it('permalink_defaults', async () => {
hexo.config.permalink = 'posts/:lang/:title/';
hexo.config.permalink_defaults = {lang: 'en'};
const posts = await Post.insert([{
source: 'my-new-post.md',
slug: 'my-new-post',
title: 'My New Post1'
}, {
source: 'my-new-fr-post.md',
slug: 'my-new-fr-post',
title: 'My New Post2',
lang: 'fr'
}]);
postPermalink(posts[0]).should.eql('posts/en/my-new-post/');
postPermalink(posts[1]).should.eql('posts/fr/my-new-fr-post/');
await Promise.all(posts.map(post => Post.removeById(post._id)));
});
it('permalink_defaults - null', async () => {
hexo.config.permalink = 'posts/:lang/:title/';
hexo.config.permalink_defaults = null;
const posts = await Post.insert([{
source: 'my-new-post.md',
slug: 'my-new-post',
title: 'My New Post1',
lang: 'en'
}, {
source: 'my-new-post-2.md',
slug: 'my-new-post-2',
title: 'My New Post2',
lang: 'fr'
}]);
postPermalink(posts[0]).should.eql('posts/en/my-new-post/');
postPermalink(posts[1]).should.eql('posts/fr/my-new-post-2/');
await Promise.all(posts.map(post => Post.removeById(post._id)));
});
it('permalink - should override everything', async () => {
hexo.config.permalink = ':year/:month/:day/:title/';
const posts = await Post.insert([{
source: 'my-new-post.md',
slug: 'hexo/permalink-test',
__permalink: 'hexo/permalink-test',
title: 'Permalink Test',
date: moment('2014-01-02')
}, {
source: 'another-new-post.md',
slug: '/hexo-hexo/permalink-test-2',
__permalink: 'hexo-hexo/permalink-test-2',
title: 'Permalink Test',
date: moment('2014-01-02')
}]);
postPermalink(posts[0]).should.eql('/hexo/permalink-test');
postPermalink(posts[1]).should.eql('/hexo-hexo/permalink-test-2');
await Promise.all(posts.map(post => Post.removeById(post._id)));
});
});