-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.61 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
const path = require('path')
const RSS = require('rss')
const chalk = require('chalk')
module.exports = (options, ctx) => {
return {
name: 'rss',
generated () {
const fs = require('fs-extra')
const { pages, outDir, themeConfig, siteConfig } = ctx
const { filter = () => true, count = 20, site_url, copyright } = options
const year = themeConfig.startYear || new Date().getFullYear()
const author = themeConfig.author || siteConfig.title || 'reco_luan'
const feed = new RSS({
title: siteConfig.title,
description: siteConfig.description,
feed_url: `${site_url}/rss.xml`,
site_url,
copyright: copyright || `${author} ${year}`,
language: 'en'
})
pages
.filter(page => {
const { title, frontmatter: { home, publish }} = page
return !(home == true || title == undefined || publish === false)
})
.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.title,
description: page.excerpt,
url: `${site_url}${page.path}`,
categories: page.frontmatter.categories || [],
author: page.frontmatter.author || themeConfig.author || siteConfig.title,
date: page.date
}))
.slice(0, count)
.forEach(page => feed.item(page))
fs.writeFile(
path.resolve(outDir, 'rss.xml'),
feed.xml()
)
console.log(chalk.green.bold('RSS has been generated!'))
}
}
}