-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
43 lines (37 loc) · 1.03 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
35
36
37
38
39
40
41
42
43
const Matcher = require("minimatch").Minimatch;
function setMetadata(file, global, rule) {
if (typeof rule.metadata === "function") {
rule = Object.assign({}, rule, { metadata: rule.metadata(file, global) });
}
Object.keys(rule.metadata).forEach(function (key) {
if (rule.preserve && key in file) {
return;
}
file[key] = rule.metadata[key];
});
}
/**
* Sets some metadata on each file depending a pattern
*/
module.exports = function (rules = []) {
const matchers = [];
rules.forEach(function (rule) {
matchers.push({
matcher: new Matcher(rule.pattern),
metadata: rule.metadata,
preserve: rule.preserve,
});
});
return function (files, metalsmith, done) {
const globalMetadata = metalsmith.metadata();
Object.keys(files).forEach(function (file) {
const fileObject = files[file];
matchers.forEach(function (rule) {
if (rule.matcher.match(file)) {
setMetadata(fileObject, globalMetadata, rule);
}
});
});
done();
};
};