2019-05-27 02:08:57 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-10-17 06:01:35 +00:00
|
|
|
const { join } = require('path')
|
|
|
|
const { readFileSync } = require('fs')
|
2020-07-01 06:46:16 +00:00
|
|
|
const moment = require('moment')
|
2019-06-04 03:38:09 +00:00
|
|
|
let sitemapTmpl = ''
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
module.exports = function (config) {
|
|
|
|
if (sitemapTmpl) return sitemapTmpl
|
|
|
|
|
|
|
|
const nunjucks = require('nunjucks')
|
|
|
|
const env = new nunjucks.Environment(null, {
|
|
|
|
autoescape: false,
|
|
|
|
watch: false
|
|
|
|
})
|
|
|
|
|
2020-07-01 06:46:16 +00:00
|
|
|
env.addFilter('formatDate', str => {
|
|
|
|
if (typeof str === 'string') return str.substring(0, 10)
|
|
|
|
return moment(str).format('YYYY-MM-DD[T00:00:00.000Z]').substring(0, 10)
|
|
|
|
})
|
|
|
|
|
2019-10-17 06:01:35 +00:00
|
|
|
const sitemapSrc = config.sitemap.template || join(__dirname, '.sitemap.xml')
|
|
|
|
sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env)
|
2019-05-27 02:08:57 +00:00
|
|
|
|
|
|
|
return sitemapTmpl
|
|
|
|
}
|