定制文章内容xml模板

This commit is contained in:
结发受长生
2019-07-01 14:48:12 +08:00
parent 2b635d6783
commit da0530d37a
6 changed files with 56 additions and 17 deletions
+23
View File
@@ -0,0 +1,23 @@
const nunjucks = require('nunjucks')
const path = require('path')
const fs = require('fs')
const env = new nunjucks.Environment()
env.addFilter('noControlChars', function(str) {
return str && str.replace(/[\x00-\x1F\x7F]/g, '')
})
const searchTmplSrc = path.join(__dirname, '../templates/articles.xml')
hexo.extend.generator.register('xml', function(locals){
const searchTmpl = nunjucks.compile(fs.readFileSync(searchTmplSrc, 'utf8'), env)
const posts = locals.posts.sort('-date');
const xmlData = searchTmpl.render({
posts: posts,
root: this.config.root
})
return {
path: 'articles.xml',
data: xmlData
}
})