-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (50 loc) · 1.75 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = function(o){
var RSS = require('rss');
var helpers = require('./helpers.js');
return function(files, metalsmith, done){
var md = metalsmith.metadata();
var siteMd = md.site || md;
o = helpers.ensureOptions(o, siteMd);
var collections = {};
var feeds = {};
if(o.collection){
for(var c of o.collection){
collections[c] = md[o.collectionKey][c];
}
}else if(o.collectionKey){
collections = md[o.collectionKey];
}else{
collections.feed = [];
for(var f in files){
if(o.pattern.match(f)){
collections.feed.push(files[f]);
}
}
}
for(var c in collections){
var feedName = o.name.replace(o.replaceToken, c);
var collection = collections[c];
if(!feeds[feedName]){
var feedOptions = o.feedOptions(c, collection, o);
feeds[feedName] = new RSS(feedOptions);
}
var feed = feeds[feedName];
if(o.limit){
collection = collection.slice(0, o.limit);
}
for(var i in collection){
var file = collection[i];
if(!o.collection || file.collection == o.collection){
var item = o.itemOptions(file, o);
feed.item(item);
}
}
}
for(var f in feeds){
files[f] = {
contents: Buffer.from(feeds[f].xml(), 'utf8')
};
}
done();
}
}