2019-05-27 02:08:57 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const nanomatch = require('nanomatch')
|
|
|
|
const template = require('./template')
|
2019-06-04 03:38:09 +00:00
|
|
|
const moment = require('moment')
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
module.exports = function (locals) {
|
|
|
|
const config = this.config
|
2019-05-30 09:50:20 +00:00
|
|
|
let skipRenderList = ['*.js', '*.css']
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
if (Array.isArray(config.skip_render)) {
|
|
|
|
skipRenderList = skipRenderList.concat(config.skip_render)
|
|
|
|
} else if (config.skip_render != null) {
|
|
|
|
skipRenderList.push(config.skip_render)
|
|
|
|
}
|
|
|
|
|
|
|
|
const posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
|
|
|
|
.filter((post) => {
|
|
|
|
return post.sitemap !== false && !isMatch(post.source, skipRenderList)
|
|
|
|
})
|
|
|
|
.sort((a, b) => {
|
2019-06-04 03:38:09 +00:00
|
|
|
return b.date - a.date
|
2019-05-27 02:08:57 +00:00
|
|
|
})
|
2019-05-30 09:48:11 +00:00
|
|
|
.map((post) => ({
|
|
|
|
...post,
|
2019-06-04 03:38:09 +00:00
|
|
|
permalink: post.permalink.replace('index.html', ''),
|
|
|
|
date: moment(post.date).format('YYYY-MM-DD[T00:00:00.000Z]'),
|
|
|
|
lastUpdated: () => {
|
|
|
|
if (post.lastUpdated) return moment(post.lastUpdated).format('YYYY-MM-DD[T00:00:00.000Z]')
|
|
|
|
else return false
|
|
|
|
}
|
2019-05-30 09:48:11 +00:00
|
|
|
}))
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
// configuration dictionary
|
|
|
|
const xmlConfig = {
|
|
|
|
config: config,
|
|
|
|
posts: posts,
|
|
|
|
// add the sNow variable for creation of the home page and potential tags/cats
|
2019-06-04 03:38:09 +00:00
|
|
|
sNow: moment().format('YYYY-MM-DD[T00:00:00.000Z]')
|
2019-05-27 02:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add tags array available in the template
|
|
|
|
if (config.sitemap.tags !== false) {
|
|
|
|
xmlConfig.tags = locals.tags.toArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
// add categories available in the template
|
|
|
|
if (config.sitemap.categories !== false) {
|
|
|
|
xmlConfig.categories = locals.categories.toArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
const xml = template(config).render(xmlConfig)
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: config.sitemap.path,
|
|
|
|
data: xml
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMatch (path, patterns) {
|
|
|
|
if (patterns && patterns.length) {
|
|
|
|
if (nanomatch.some(path, patterns, { matchBase: true })) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|