This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
70 lines (64 loc) · 1.77 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
70
const WPApi = require('wpapi')
const axios = require('axios')
const urljoin = require('url-join')
const defaults = {
injectDefaultHead: true,
baseUrl: process.env.HOSTNAME || 'http://localhost:3000',
path: '/feed.xml'
}
module.exports = function(moduleOptions) {
const options = {
...defaults,
...moduleOptions
}
return {
path: options.path,
cacheTime: 1000 * 60 * 15,
type: 'atom1',
async create(feed) {
const endpoint = options.endpoint ? options.endpoint : 'https://wp.kmr.io/wp-json'
const wpapi = new WPApi({
endpoint
})
const data = (await axios.get(endpoint)).data
feed.options = {
title: data.name,
description: data.description,
generator: 'Feed',
id: options.baseUrl,
link: options.baseUrl,
feedLinks: {
atom: urljoin(options.baseUrl, options.path)
},
...options
}
const posts = await wpapi.posts()
posts.forEach(post => {
feed.addItem({
title: post.title.rendered,
id: urljoin(options.baseUrl, post.slug),
link: urljoin(options.baseUrl, post.slug),
content: post.content.rendered.replace(/(\r\n|\n|\r)/gm, ''),
description: post.excerpt.rendered,
date: new Date(post.date)
})
})
},
...options
}
}
module.exports.injectDefaultHead = async function() {
await Promise.all(
this.options.feed.map(async feed => {
if (feed.injectDefaultHead) {
const data = (await axios.get(this.options.wp.endpoint)).data
this.options.head.link.push({
rel: 'alternate',
type: 'application/atom+xml',
title: data.name,
href: urljoin(feed.baseUrl, feed.path)
})
}
})
)
}