/
metalsmith-autodate.js
55 lines (42 loc) · 1.43 KB
/
metalsmith-autodate.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
'use strict';
var path = require('path');
var moment = require('moment');
module.exports = autodate;
function autodate(options) {
options = normalize(options);
return function(files, metalsmith, done){
setImmediate(done);
Object.keys(files).forEach(function(filepath){
var file = files[filepath];
var dirname = path.dirname(filepath);
var filename = path.basename(file.preserved);
var withoutDate = filename;
if (filename.length > options.format.length) {
var test = filename.substr(0, options.format.length);
var m = moment(test, options.format, true);
if (m.isValid()) {
if (file.date && stringify(file.date, options.format) !== stringify(m, options.format)) {
throw new Error("Date-named file '" + filepath + "' has a non-matching date attribute: " + stringify(file.date, options.format));
}
file.date = m.toDate();
withoutDate = filename.substr(options.format.length);
if (withoutDate.match(/^[-.]/)) {
withoutDate = withoutDate.substr(1);
}
}
}
file.withoutDate = withoutDate;
});
};
}
function normalize(options){
if ('string' === typeof options) {
options = { date: options };
}
options = options || {};
options.format = options.format || 'YYYY-MM-DD';
return options;
}
function stringify(date, format) {
return moment(date).utc().format(format);
}