fix(sitemap): modernise

- 'index.html' removal function is replaced by pretty_urls config
- add homepage
- remove uriencode, permalink is now encoded by default in Hexo v4+
- es6-fy
This commit is contained in:
curben 2019-10-17 07:01:35 +01:00
parent ce9ebeb1bf
commit c61fdab84f
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
3 changed files with 21 additions and 35 deletions

View File

@ -2,7 +2,7 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in posts %}
<url>
<loc>{{ post.permalink | uriencode }}</loc>
<loc>{{ post.permalink }}</loc>
{% if post.lastUpdated() %}
<lastmod>{{ post.lastUpdated() }}</lastmod>
{% elif post.date %}
@ -13,30 +13,21 @@
{# home page #}
<url>
<loc>{{ config.url + '/' + config.archive_dir + '/' | uriencode }}</loc>
<loc>{{ config.url + config.root }}</loc>
<lastmod>{{ sNow }}</lastmod>
</url>
{# archive page #}
<url>
<loc>{{ config.url + '/' + archive_dir + '/' }}</loc>
<lastmod>{{ sNow }}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
{# tag pages #}
{% for tag in tags %}
<url>
<loc>{{ tag.permalink | uriencode }}</loc>
<loc>{{ tag.permalink }}</loc>
<lastmod>{{ sNow }}</lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
</url>
{% endfor %}
{# categories pages #}
{% for cat in categories %}
<url>
<loc>{{ cat.permalink | uriencode }}</loc>
<lastmod>{{ sNow }}</lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
</url>
{% endfor %}
</urlset>

View File

@ -4,6 +4,14 @@ const micromatch = require('micromatch')
const template = require('./template')
const moment = require('moment')
const isMatch = (path, patterns) => {
if (patterns && patterns.length) {
if (micromatch.isMatch(path, patterns, { matchBase: true })) return true
}
return false
}
module.exports = function (locals) {
const config = this.config
let skipRenderList = ['*.js', '*.css']
@ -24,7 +32,6 @@ module.exports = function (locals) {
// https://github.com/pyyzcwg2833/hexo-generator-sitemap/commit/a92dbbb83cc39ff60d43faa5cd688a56574a3889
.map((post) => ({
...post,
permalink: post.permalink.replace('index.html', ''),
date: moment(post.date).format('YYYY-MM-DD[T00:00:00.000Z]'),
lastUpdated: () => {
if (post.lastUpdated) return moment(post.lastUpdated).format('YYYY-MM-DD[T00:00:00.000Z]')
@ -55,11 +62,3 @@ module.exports = function (locals) {
data: xml
}
}
function isMatch (path, patterns) {
if (patterns && patterns.length) {
if (micromatch.isMatch(path, patterns, { matchBase: true })) return true
}
return false
}

View File

@ -1,7 +1,7 @@
'use strict'
const pathFn = require('path')
const fs = require('fs')
const { join } = require('path')
const { readFileSync } = require('fs')
let sitemapTmpl = ''
module.exports = function (config) {
@ -13,12 +13,8 @@ module.exports = function (config) {
watch: false
})
env.addFilter('uriencode', (str) => {
return encodeURI(str)
})
const sitemapSrc = config.sitemap.template || pathFn.join(__dirname, '.sitemap.xml')
sitemapTmpl = nunjucks.compile(fs.readFileSync(sitemapSrc, 'utf8'), env)
const sitemapSrc = config.sitemap.template || join(__dirname, '.sitemap.xml')
sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env)
return sitemapTmpl
}