-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincludes.js
More file actions
56 lines (43 loc) · 1.56 KB
/
includes.js
File metadata and controls
56 lines (43 loc) · 1.56 KB
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';
// add a sort option to main
module.exports = function includes() {
return function (files, metalsmith, done) {
setImmediate(done);
var meta = metalsmith.metadata();
meta.includes = {};
Object.keys(files).forEach(file => {
if (! /html/.test(file)) return;
var collection = files[file].collection;
if (collection === undefined) return;
if (typeof collection === 'string') {
collection = [collection];
}
/**
* This is what makes it work.
*
* By calling this before `metalsmith-collections`
* we can make a `copy` of the `file` object
* which will NOT be updated by reference with later transforms
* and have a clean original to include using our
* template engine in `metalsmith-layouts`
*
* If we call this after `metalsmith-collections`
* we have to deal with the introduced complexity
* of copying the `file` object after circular references
* have been introduced (next, previous)
**/
// var included = JSON.parse(JSON.stringify(files[file]));
var included = Object.assign({}, files[file]);
included.path = file;
// included.contents = included.contents.toString(); // not needed
collection.forEach(key => {
if (meta.includes[key] === undefined) {
meta.includes[key] = [];
}
meta.includes[key].push(included);
// sort here ...
});
});
// console.log('INC', meta.includes);
};
};