2019-05-27 02:08:57 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-07-30 06:24:06 +00:00
|
|
|
const micromatch = require('micromatch')
|
2019-05-27 02:08:57 +00:00
|
|
|
const template = require('./template')
|
|
|
|
|
2019-10-17 06:01:35 +00:00
|
|
|
const isMatch = (path, patterns) => {
|
|
|
|
if (patterns && patterns.length) {
|
|
|
|
if (micromatch.isMatch(path, patterns, { matchBase: true })) return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-27 02:08:57 +00:00
|
|
|
module.exports = function (locals) {
|
2020-07-01 06:52:00 +00:00
|
|
|
const { config } = this
|
|
|
|
const { sitemap, skip_render } = config
|
|
|
|
const { path } = sitemap
|
2019-12-12 08:58:09 +00:00
|
|
|
const skipRenderList = ['*.js', '*.css']
|
2019-05-27 02:08:57 +00:00
|
|
|
|
2020-07-01 06:52:00 +00:00
|
|
|
if (Array.isArray(skip_render)) {
|
|
|
|
skipRenderList.push(...skip_render)
|
|
|
|
} else if (skip_render != null) {
|
|
|
|
skipRenderList.push(skip_render)
|
2019-05-27 02:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
// configuration dictionary
|
|
|
|
const xmlConfig = {
|
2020-07-01 06:52:00 +00:00
|
|
|
config,
|
|
|
|
posts,
|
|
|
|
tags: locals.tags.toArray()
|
2019-05-27 02:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 06:52:00 +00:00
|
|
|
const data = template(config).render(xmlConfig)
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
return {
|
2020-07-01 06:52:00 +00:00
|
|
|
path,
|
|
|
|
data
|
2019-05-27 02:08:57 +00:00
|
|
|
}
|
|
|
|
}
|