blog/themes/chameleon/scripts/sitemap/generator.js

48 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

'use strict'
const micromatch = require('micromatch')
const template = require('./template')
const isMatch = (path, patterns) => {
if (patterns && patterns.length) {
if (micromatch.isMatch(path, patterns, { matchBase: true })) return true
}
return false
}
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']
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)
}
const posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
.filter((post) => {
return post.sitemap !== false && !isMatch(post.source, skipRenderList)
})
.sort((a, b) => {
return b.date - a.date
})
// configuration dictionary
const xmlConfig = {
2020-07-01 06:52:00 +00:00
config,
posts,
tags: locals.tags.toArray()
}
2020-07-01 06:52:00 +00:00
const data = template(config).render(xmlConfig)
return {
2020-07-01 06:52:00 +00:00
path,
data
}
}