-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmetalsmith_feed.js
68 lines (58 loc) · 1.79 KB
/
metalsmith_feed.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
62
63
64
65
66
67
68
module.exports = function(options) {
if (options == null) {
options = {};
}
const RSS = require('rss');
const url = require('url');
const limit = options.limit != null ? options.limit : 20;
const destination = options.destination || 'rss.xml';
const collectionName = options.collection;
if (!collectionName) {
throw new Error('collection option is required');
}
return function(files, metalsmith, done) {
const metadata = metalsmith.metadata();
if (!metadata.collections) {
return done(
new Error('no collections configured - see metalsmith-collections')
);
}
let collection = metadata.collections[collectionName];
const feedOptions = {
...metadata.site,
...options,
site_url: metadata.site != null ? metadata.site.url : undefined,
generator: 'metalsmith-feed'
};
const siteUrl = feedOptions.site_url;
if (!siteUrl) {
return done(
new Error('either site_url or metadata.site.url must be configured')
);
}
if (feedOptions.feed_url == null) {
feedOptions.feed_url = url.resolve(siteUrl, destination);
}
const feed = new RSS(feedOptions);
if (limit) {
collection = collection.slice(0, limit);
}
const preprocess = options.preprocess || (file => file);
for (let file of collection) {
const itemData = {
...file,
description: file.less || file.excerpt || file.contents
};
if (!itemData.url && itemData.path) {
itemData.url = url.resolve(siteUrl, itemData.path);
}
if (itemData.link) {
itemData.guid = itemData.url;
itemData.url = itemData.link;
}
feed.item(preprocess(itemData));
}
files[destination] = {contents: new Buffer(feed.xml(), 'utf8')};
return done();
};
};