From c61fdab84f413e41a396b0ff05ff926fb7ca283b Mon Sep 17 00:00:00 2001 From: curben <2809763-curben@users.noreply.gitlab.com> Date: Thu, 17 Oct 2019 07:01:35 +0100 Subject: [PATCH] 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 --- themes/chameleon/scripts/sitemap/.sitemap.xml | 27 +++++++------------ themes/chameleon/scripts/sitemap/generator.js | 17 ++++++------ themes/chameleon/scripts/sitemap/template.js | 12 +++------ 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/themes/chameleon/scripts/sitemap/.sitemap.xml b/themes/chameleon/scripts/sitemap/.sitemap.xml index 246cfd2..9ee4c32 100644 --- a/themes/chameleon/scripts/sitemap/.sitemap.xml +++ b/themes/chameleon/scripts/sitemap/.sitemap.xml @@ -2,7 +2,7 @@ {% for post in posts %} - {{ post.permalink | uriencode }} + {{ post.permalink }} {% if post.lastUpdated() %} {{ post.lastUpdated() }} {% elif post.date %} @@ -13,30 +13,21 @@ {# home page #} - {{ config.url + '/' + config.archive_dir + '/' | uriencode }} + {{ config.url + config.root }} + {{ sNow }} + + + {# archive page #} + + {{ config.url + '/' + archive_dir + '/' }} {{ sNow }} - daily - 1.0 {# tag pages #} {% for tag in tags %} - {{ tag.permalink | uriencode }} + {{ tag.permalink }} {{ sNow }} - daily - 0.6 {% endfor %} - - {# categories pages #} - {% for cat in categories %} - - {{ cat.permalink | uriencode }} - {{ sNow }} - daily - 0.6 - - {% endfor %} - diff --git a/themes/chameleon/scripts/sitemap/generator.js b/themes/chameleon/scripts/sitemap/generator.js index 50757fe..b087c38 100644 --- a/themes/chameleon/scripts/sitemap/generator.js +++ b/themes/chameleon/scripts/sitemap/generator.js @@ -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 -} diff --git a/themes/chameleon/scripts/sitemap/template.js b/themes/chameleon/scripts/sitemap/template.js index e65481e..9c733d6 100644 --- a/themes/chameleon/scripts/sitemap/template.js +++ b/themes/chameleon/scripts/sitemap/template.js @@ -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 }