forked from hexojs/hexo
/
asset.js
539 lines (440 loc) · 13.2 KB
/
asset.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
'use strict';
const pathFn = require('path');
const fs = require('hexo-fs');
const Promise = require('bluebird');
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
describe('asset', () => {
const Hexo = require('../../../lib/hexo');
const baseDir = pathFn.join(__dirname, 'asset_test');
const hexo = new Hexo(baseDir);
const asset = require('../../../lib/plugins/processor/asset')(hexo);
const process = asset.process.bind(hexo);
const pattern = asset.pattern;
const source = hexo.source;
const File = source.File;
const Asset = hexo.model('Asset');
const Page = hexo.model('Page');
function newFile(options) {
options.source = pathFn.join(source.base, options.path);
options.params = {
renderable: options.renderable
};
return new File(options);
}
before(() => fs.mkdirs(baseDir).then(() => hexo.init()));
after(() => fs.rmdir(baseDir));
it('pattern', () => {
// Renderable files
pattern.match('foo.json').should.have.property('renderable', true);
// Non-renderable files
pattern.match('foo.txt').should.have.property('renderable', false);
// Tmp files
should.not.exist(pattern.match('foo.txt~'));
should.not.exist(pattern.match('foo.txt%'));
// Hidden files
should.not.exist(pattern.match('_foo.txt'));
should.not.exist(pattern.match('test/_foo.txt'));
should.not.exist(pattern.match('.foo.txt'));
should.not.exist(pattern.match('test/.foo.txt'));
// Include files
hexo.config.include = ['fff/**'];
pattern.match('fff/_foo.txt').should.exist;
hexo.config.include = [];
// Exclude files
hexo.config.exclude = ['fff/**'];
should.not.exist(pattern.match('fff/foo.txt'));
hexo.config.exclude = [];
// Skip render files
hexo.config.skip_render = ['fff/**'];
pattern.match('fff/foo.json').should.have.property('renderable', false);
hexo.config.skip_render = [];
});
it('asset - type: create', () => {
const file = newFile({
path: 'foo.jpg',
type: 'create',
renderable: false
});
return fs.writeFile(file.source, 'foo').then(() => process(file)).then(() => {
const id = 'source/' + file.path;
const asset = Asset.findById(id);
asset._id.should.eql(id);
asset.path.should.eql(file.path);
asset.modified.should.be.true;
asset.renderable.should.be.false;
return asset.remove();
}).finally(() => fs.unlink(file.source));
});
it('asset - type: create (when source path is configed to parent directory)', () => {
const file = newFile({
path: '../../source/foo.jpg',
type: 'create',
renderable: false
});
return fs.writeFile(file.source, 'foo').then(() => process(file)).then(() => {
const id = '../source/foo.jpg'; // The id should a relative path,because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
const asset = Asset.findById(id);
asset._id.should.eql(id);
asset.path.should.eql(file.path);
asset.modified.should.be.true;
asset.renderable.should.be.false;
return asset.remove();
}).finally(() => {
fs.unlink(file.source);
fs.rmdir(pathFn.dirname(file.source));
});
});
it('asset - type: update', () => {
const file = newFile({
path: 'foo.jpg',
type: 'update',
renderable: false
});
const id = 'source/' + file.path;
return Promise.all([
fs.writeFile(file.source, 'test'),
Asset.insert({
_id: id,
path: file.path,
modified: false
})
]).then(() => process(file)).then(() => {
const asset = Asset.findById(id);
asset._id.should.eql(id);
asset.path.should.eql(file.path);
asset.modified.should.be.true;
asset.renderable.should.be.false;
return asset.remove();
}).finally(() => fs.unlink(file.source));
});
it('asset - type: skip', () => {
const file = newFile({
path: 'foo.jpg',
type: 'skip',
renderable: false
});
const id = 'source/' + file.path;
return Promise.all([
fs.writeFile(file.source, 'test'),
Asset.insert({
_id: id,
path: file.path,
modified: false
})
]).then(() => process(file)).then(() => {
const asset = Asset.findById(id);
asset.modified.should.be.false;
}).finally(() => Promise.all([
Asset.removeById(id),
fs.unlink(file.source)
]));
});
it('asset - type: delete', () => {
const file = newFile({
path: 'foo.jpg',
type: 'delete',
renderable: false
});
const id = 'source/' + file.path;
return Asset.insert({
_id: id,
path: file.path
}).then(() => process(file)).then(() => {
should.not.exist(Asset.findById(id));
});
});
it('page - type: create', () => {
const body = [
'title: "Hello world"',
'date: 2006-01-02 15:04:05',
'updated: 2014-12-13 01:02:03',
'---',
'The quick brown fox jumps over the lazy dog'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.title.should.eql('Hello world');
page.date.format(dateFormat).should.eql('2006-01-02 15:04:05');
page.updated.format(dateFormat).should.eql('2014-12-13 01:02:03');
page._content.should.eql('The quick brown fox jumps over the lazy dog');
page.source.should.eql(file.path);
page.raw.should.eql(body);
page.path.should.eql('hello.html');
page.layout.should.eql('page');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - type: update', () => {
const body = [
'title: "Hello world"',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'update',
renderable: true
});
let id;
return Promise.all([
Page.insert({source: file.path, path: 'hello.html'}),
fs.writeFile(file.source, body)
]).spread(doc => {
id = doc._id;
return process(file);
}).then(() => {
const page = Page.findOne({source: file.path});
page._id.should.eql(id);
page.title.should.eql('Hello world');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - type: delete', () => {
const file = newFile({
path: 'hello.swig',
type: 'delete',
renderable: true
});
return Page.insert({
source: file.path,
path: 'hello.html'
}).then(() => process(file)).then(() => {
should.not.exist(Page.findOne({source: file.path}));
});
});
it('page - use the status of the source file if date not set', () => {
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, '').then(() => Promise.all([
fs.stat(file.source),
process(file)
])).spread(stats => {
const page = Page.findOne({source: file.path});
page.date.toDate().should.eql(stats.ctime);
page.updated.toDate().should.eql(stats.mtime);
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - use the date for updated if use_date_for_updated is set', () => {
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
hexo.config.use_date_for_updated = true;
return fs.writeFile(file.source, '').then(() => Promise.all([
fs.stat(file.source),
process(file)
])).spread(stats => {
const page = Page.findOne({source: file.path});
page.date.toDate().should.eql(stats.ctime);
page.updated.toDate().should.eql(page.date.toDate());
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
}).finally(() => {
hexo.config.use_date_for_updated = undefined;
});
});
it('page - permalink', () => {
const body = [
'title: "Hello world"',
'permalink: foo.html',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.path.should.eql('foo.html');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - permalink (without extension name)', () => {
const body = [
'title: "Hello world"',
'permalink: foo',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.path.should.eql('foo.html');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - permalink (with trailing slash)', () => {
const body = [
'title: "Hello world"',
'permalink: foo/',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.path.should.eql('foo/index.html');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - set layout to false if output is not html', () => {
const body = 'foo: 1';
const file = newFile({
path: 'test.yml',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.layout.should.eql('false');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - don\'t set layout to false if layout is set but output is not html', () => {
const body = [
'layout: something',
'---',
'foo: 1'
].join('\n');
const file = newFile({
path: 'test.yml',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.layout.should.eql('something');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - parse date', () => {
const body = [
'title: "Hello world"',
'date: Apr 24 2014',
'updated: May 5 2015',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.date.format(dateFormat).should.eql('2014-04-24 00:00:00');
page.updated.format(dateFormat).should.eql('2015-05-05 00:00:00');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - use file stats instead if date is invalid', () => {
const body = [
'title: "Hello world"',
'date: yomama',
'updated: isfat',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => Promise.all([
file.stat(),
process(file)
])).spread(stats => {
const page = Page.findOne({source: file.path});
page.date.toDate().should.eql(stats.ctime);
page.updated.toDate().should.eql(page.date.toDate());
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - don\'t remove extension name', () => {
const body = '';
const file = newFile({
path: 'test.min.js',
type: 'create',
renderable: true
});
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.path.should.eql('test.min.js');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});
it('page - timezone', () => {
const body = [
'title: "Hello world"',
'date: Apr 24 2014',
'updated: May 5 2015',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'create',
renderable: true
});
hexo.config.timezone = 'UTC';
return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const page = Page.findOne({source: file.path});
page.date.utc().format(dateFormat).should.eql('2014-04-24 00:00:00');
page.updated.utc().format(dateFormat).should.eql('2015-05-05 00:00:00');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
}).finally(() => {
hexo.config.timezone = '';
});
});
});