-
Notifications
You must be signed in to change notification settings - Fork 251
/
merge-meta.js
40 lines (36 loc) · 1.32 KB
/
merge-meta.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
var pi = require('pipe-iterators'),
xtend = require('xtend'),
path = require('path');
module.exports = function(meta) {
return pi.map(function(item) {
var subpath, newItem = {};
// split by path.sep for Windows compatibility
var parts = item.relative.split(path.sep).filter(Boolean);
// we have exactly as many directories to consider
// as there are elements in parts
for (var i = 0; i < parts.length; i++) {
subpath = parts.slice(0, i).join('/');
// add a trailing '/' for all but the first one, which is just '*'
if (i > 0) {
subpath += '/';
}
// each of these corresponds to a directory
subpath += '*';
// console.log('index i=' + i + ' subpath=' + subpath);
if (typeof meta[subpath] === 'object') {
newItem = xtend(newItem, meta[subpath]);
}
}
// as far as the file itself, we remove trailing .md
subpath = item.relative.replace(/.md$/, '');
//console.log("last=" + subpath);
newItem = xtend(newItem, meta[subpath]);
// finally inject the data from the file itself
newItem = xtend(newItem, item);
// set title from first heading if otherwise missing
if (!newItem.title && newItem.headings && newItem.headings[0]) {
newItem.title = newItem.headings[0].text;
}
return newItem;
});
};