forked from sanx/metalsmith-date-in-filename
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (33 loc) · 1.34 KB
/
index.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
var _ = require('lodash');
module.exports = function(options){
var override = (('boolean' === typeof options) && options) || ('object' === typeof options && options.override) || false;
return function drafts(files, metalsmith, done){
_.forEach(files, function (fileMeta, fileName) {
if (!override && fileMeta.date) {
return;
}
var m;
if (m = fileName.match(/(\d{4}-\d{2}-\d{2})/)) {
fileMeta.date = new Date(m[1]);
if (options.basename) fileMeta[options.basename] = fileMeta.basename.replace(/(\d{4}-\d{2}-\d{2})/g, '').replace(/^[^a-z\d]*|[^a-z\d]*$/gi, '')
} else if (m = fileName.match(/(\d{8})/)) {
fileMeta.date = new Date(
m[1].substr(0, 4) +'-'+
m[1].substr(4, 2) +'-'+
m[1].substr(6, 2)
);
if (options.basename) fileMeta[options.basename] = fileMeta.basename.replace(/(\d{8})/g, '').replace(/^[^a-z\d]*|[^a-z\d]*$/gi, '')
}
});
var filesWithoutContents = _.zipObject(_.map(files, function (fileMeta, fileName) {
var filteredFileMeta = _.reduce(fileMeta, function (acc, metaValue, metaName) {
if ('contents' !== metaName) {
acc[metaName] = metaValue;
}
return acc;
}, {});
return [fileName, filteredFileMeta];
}));
done();
};
};