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-06-04 03:38:09 +00:00
|
|
|
const moment = require('moment')
|
2019-05-27 02:08:57 +00:00
|
|
|
|
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) {
|
|
|
|
const config = this.config
|
2019-12-12 08:58:09 +00:00
|
|
|
const skipRenderList = ['*.js', '*.css']
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
if (Array.isArray(config.skip_render)) {
|
2019-11-04 10:44:30 +00:00
|
|
|
skipRenderList.push(...config.skip_render)
|
2019-05-27 02:08:57 +00:00
|
|
|
} 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-08-19 00:49:36 +00:00
|
|
|
// https://github.com/pyyzcwg2833/hexo-generator-sitemap/commit/a92dbbb83cc39ff60d43faa5cd688a56574a3889
|
2019-05-30 09:48:11 +00:00
|
|
|
.map((post) => ({
|
|
|
|
...post,
|
2019-06-04 03:38:09 +00:00
|
|
|
date: moment(post.date).format('YYYY-MM-DD[T00:00:00.000Z]'),
|
2020-06-23 04:27:55 +00:00
|
|
|
updated: post.updated ? moment(post.updated).format('YYYY-MM-DD[T00:00:00.000Z]') : 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,
|
2019-08-19 00:49:36 +00:00
|
|
|
// add current time to <lastmod> of homepage and tags
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (config.sitemap.tags !== false) {
|
|
|
|
xmlConfig.tags = locals.tags.toArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.sitemap.categories !== false) {
|
|
|
|
xmlConfig.categories = locals.categories.toArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
const xml = template(config).render(xmlConfig)
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: config.sitemap.path,
|
|
|
|
data: xml
|
|
|
|
}
|
|
|
|
}
|