This repository has been archived by the owner on Aug 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (56 loc) · 1.79 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
62
63
64
65
66
67
68
69
const validate = (metadata, settings) => {
if (!metadata.collections) {
return 'No collections found. You must use metalsmith-collections with this plugin.'
}
if (!settings.json.title && !(metadata.site && metadata.site.title)) {
return 'No title set. This is required in a JSON Feed.'
}
}
module.exports = (options = {}) => {
const settings = Object.assign(
{
destination: 'feed.json',
limit: 20,
json: {}
},
options
)
if (!settings.collection) {
throw new Error('No `collection` option specified.')
}
return (files, metalsmith, done) => {
const metadata = metalsmith.metadata()
const error = validate(metadata, settings)
if (error) return done(new Error(error))
const allCollections = Object.keys(metadata.collections)
let feed = {
version: 'https://jsonfeed.org/version/1',
title: metadata.site.title,
items: []
}
// Add home_page_url and feed_url automatically if the correct metadata exists
if (metadata.site && metadata.site.url) {
feed.home_page_url = metadata.site.url
feed.feed_url = `${metadata.site.url}${settings.destination}`
}
// Add author.name automatically if the correct metadata exists
if (metadata.site && metadata.site.author) {
feed.author = { name: metadata.site.author }
}
Object.assign(feed, settings.json)
allCollections.map(collection => metadata.collections[collection].forEach(file => {
feed.items.push({
id: file.slug,
title: file.title,
date: file.date,
permalink: file.permalink,
contents: file.contents.toString('utf8')
})
})
)
files[settings.destination] = {
contents: Buffer.from(JSON.stringify(feed, null, '\t'), 'utf8')
}
done()
}
}