mirror of https://gitlab.com/curben/blog
refactor(feed): object shorthand
This commit is contained in:
parent
d0de387829
commit
88a26ab144
|
@ -6,6 +6,7 @@ const { join } = require('path')
|
||||||
const { readFileSync } = require('fs')
|
const { readFileSync } = require('fs')
|
||||||
const moment = require('moment')
|
const moment = require('moment')
|
||||||
const { encodeURL } = require('hexo-util')
|
const { encodeURL } = require('hexo-util')
|
||||||
|
const { format } = require('url')
|
||||||
|
|
||||||
env.addFilter('uriencode', str => {
|
env.addFilter('uriencode', str => {
|
||||||
return encodeURL(str)
|
return encodeURL(str)
|
||||||
|
@ -23,33 +24,30 @@ const atomTmplSrc = join(__dirname, './.atom.xml')
|
||||||
const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env)
|
const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env)
|
||||||
|
|
||||||
module.exports = function (locals) {
|
module.exports = function (locals) {
|
||||||
const config = this.config
|
const { config } = this
|
||||||
const feedConfig = config.feed
|
const { feed, root, url } = config
|
||||||
|
const { icon: iconCfg, limit, order_by, path } = feed
|
||||||
const template = atomTmpl
|
const template = atomTmpl
|
||||||
|
|
||||||
let posts = locals.posts.sort(feedConfig.order_by || '-date')
|
let posts = locals.posts.sort(order_by || '-date')
|
||||||
posts = posts.filter(post => {
|
posts = posts.filter(post => {
|
||||||
return post.draft !== true
|
return post.draft !== true
|
||||||
})
|
})
|
||||||
|
|
||||||
if (feedConfig.limit) posts = posts.limit(feedConfig.limit)
|
if (limit) posts = posts.limit(limit)
|
||||||
|
|
||||||
let url = config.url
|
const icon = iconCfg ? format(new URL(iconCfg, url), { unicode: true }) : ''
|
||||||
if (url[url.length - 1] !== '/') url += '/'
|
|
||||||
|
|
||||||
let icon = ''
|
const data = template.render({
|
||||||
if (feedConfig.icon) icon = url + encodeURI(feedConfig.icon)
|
config,
|
||||||
|
url,
|
||||||
const xml = template.render({
|
icon,
|
||||||
config: config,
|
posts,
|
||||||
url: url,
|
feed_url: root + path
|
||||||
icon: icon,
|
|
||||||
posts: posts,
|
|
||||||
feed_url: config.root + feedConfig.path
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
path: feedConfig.path,
|
path,
|
||||||
data: xml
|
data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue