-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (59 loc) · 1.91 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
const nunjucks = require('nunjucks')
const env = new nunjucks.Environment()
const pathFn = require('path')
const fs = require('fs')
const {URL} = require('url')
env.addFilter('uriencode', (str) => {
return encodeURI(str)
})
const atomTmplSrc = pathFn.join(__dirname, 'atom.xml')
const rss2TmplSrc = pathFn.join(__dirname, 'rss2.xml')
const tpls = {
atom: nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env),
rss2: nunjucks.compile(fs.readFileSync(rss2TmplSrc, 'utf8'), env)
}
/**
* generator for atom and rss2
*/
module.exports = function (data) {
let feedConfigs = data.appConfigs.feed
if (feedConfigs === false) return false
let postData = data.data.posts0
let ret = []
Object.keys(feedConfigs).forEach(key => {
let isDefault = key === data.appConfigs.lang
let configs = feedConfigs[key]
let type = configs.type || 'atom'
let tpl = tpls[type] || tpls['atom']
let posts = postData[key]
if ((!posts || posts.length === 0) && configs.use_default_posts) {
posts = postData[data.appConfigs.lang] || []
}
posts = posts.filter(post => {
return post.draft !== true
})
if (configs.limit) posts = posts.slice(0, configs.limit - 1)
configs.posts = posts
configs.lastUpdated = posts[0] ? posts[0].updatedISO : new Date().toISOString()
configs.urlRoot = data.appConfigs.url
configs.author = data.appAuthor
if (typeof configs.author === 'string') {
configs.author = {
name: configs.author
}
}
let targetFile
if (isDefault) {
targetFile = pathFn.join(this.dirTheme, `static/${type}.xml`)
configs.url = new URL(`${type}.xml`, configs.urlRoot).toString()
} else {
targetFile = pathFn.join(this.dirTheme, `static/${type}-${key}.xml`)
configs.url = new URL(`${type}-${key}.xml`, configs.urlRoot).toString()
}
ret.push({
path: targetFile,
data: tpl.render(configs)
})
})
return ret
}