From a2a68d3291ec05d13688a0edf7f9271705c57bdf Mon Sep 17 00:00:00 2001 From: MDLeom <2809763-curben@users.noreply.gitlab.com> Date: Wed, 1 Jul 2020 07:46:16 +0100 Subject: [PATCH] fix(sitemap): use latest post's date as - posts[0] is used instead of posts.first() because first() is not available after posts.toArray() --- themes/chameleon/scripts/sitemap/.sitemap.xml | 25 +++++++++---------- themes/chameleon/scripts/sitemap/generator.js | 4 +-- themes/chameleon/scripts/sitemap/index.js | 3 ++- themes/chameleon/scripts/sitemap/template.js | 6 +++++ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/themes/chameleon/scripts/sitemap/.sitemap.xml b/themes/chameleon/scripts/sitemap/.sitemap.xml index e151dcd..9b7face 100644 --- a/themes/chameleon/scripts/sitemap/.sitemap.xml +++ b/themes/chameleon/scripts/sitemap/.sitemap.xml @@ -4,30 +4,29 @@ {{ post.permalink }} {% if post.updated %} - {{ post.updated }} + {{ post.updated | formatDate }} {% elif post.date %} - {{ post.date }} + {{ post.date | formatDate }} {% endif %} {% endfor %} {# home page #} - {{ config.url + config.root }} - {{ sNow }} - + {{ config.url + config.root }} + {% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %} + {# archive page #} - {{ config.url + '/' + archive_dir + '/' }} - {{ sNow }} + {{ config.url + '/' + config.archive_dir + '/' }} + {% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %} - - {# tag pages #} + {% for tag in tags %} - - {{ tag.permalink }} - {{ sNow }} - + + {{ tag.permalink }} + {% if tag.posts.first().updated %}{{ tag.posts.first().updated | formatDate }}{% else %}{{ tag.posts.first().date | formatDate }}{% endif %} + {% endfor %} diff --git a/themes/chameleon/scripts/sitemap/generator.js b/themes/chameleon/scripts/sitemap/generator.js index 9422dbc..12bf0ce 100644 --- a/themes/chameleon/scripts/sitemap/generator.js +++ b/themes/chameleon/scripts/sitemap/generator.js @@ -39,9 +39,7 @@ module.exports = function (locals) { // configuration dictionary const xmlConfig = { config: config, - posts: posts, - // add current time to of homepage and tags - sNow: moment().format('YYYY-MM-DD[T00:00:00.000Z]') + posts: posts } if (config.sitemap.tags !== false) { diff --git a/themes/chameleon/scripts/sitemap/index.js b/themes/chameleon/scripts/sitemap/index.js index ea4d954..0da1d40 100644 --- a/themes/chameleon/scripts/sitemap/index.js +++ b/themes/chameleon/scripts/sitemap/index.js @@ -9,7 +9,8 @@ const pathFn = require('path') const config = hexo.config.sitemap = Object.assign({ - path: 'sitemap.xml' + path: 'sitemap.xml', + tags: true }, hexo.config.sitemap) if (!pathFn.extname(config.path)) { diff --git a/themes/chameleon/scripts/sitemap/template.js b/themes/chameleon/scripts/sitemap/template.js index 9c733d6..25a1370 100644 --- a/themes/chameleon/scripts/sitemap/template.js +++ b/themes/chameleon/scripts/sitemap/template.js @@ -2,6 +2,7 @@ const { join } = require('path') const { readFileSync } = require('fs') +const moment = require('moment') let sitemapTmpl = '' module.exports = function (config) { @@ -13,6 +14,11 @@ module.exports = function (config) { watch: false }) + 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) + }) + const sitemapSrc = config.sitemap.template || join(__dirname, '.sitemap.xml') sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env)