-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
45 lines (40 loc) · 1.39 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
const path = require('path')
const RSS = require('rss')
const chalk = require('chalk')
module.exports = (pluginOptions, ctx) => {
return {
name: 'rss',
generated () {
const fs = require('fs-extra')
const { pages, sourceDir } = ctx
const { filter = () => true, count = 20 } = pluginOptions
const siteData = require(path.resolve(sourceDir, '.vuepress/config.js'))
const feed = new RSS({
title: siteData.title,
description: siteData.description,
feed_url: `${pluginOptions.site_url}/rss.xml`,
site_url: `${pluginOptions.site_url}`,
copyright: `${pluginOptions.copyright ? pluginOptions.copyright : 'Coralo 2018'}`,
language: 'en',
})
pages
.filter(page => String(page.frontmatter.type).toLowerCase() === 'post')
.filter(page => filter(page.frontmatter))
.map(page => ({...page, date: new Date(page.frontmatter.date || '')}))
.sort((a, b) => b.date - a.date)
.map(page => ({
title: page.frontmatter.title,
description: page.excerpt,
url: `${pluginOptions.site_url}${page.path}`,
date: page.date,
}))
.slice(0, count)
.forEach(page => feed.item(page))
fs.writeFile(
path.resolve(ctx.outDir, 'rss.xml'),
feed.xml()
);
console.log(chalk.green.bold('RSS has been generated!'))
}
}
}