fix(sitemap): use post.lastUpdated

* post.updated = execution time of 'hexo generate'
* use moment.format() instead of Date.toISOString() to avoid timezone conversion
  - Use '[T00:00:00.000Z]' to ignore time, instead of '[T]HH:mm:ss.sss[Z]'
  - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
This commit is contained in:
curben 2019-06-04 13:08:09 +09:30
parent fba0d3deab
commit 8717b52283
4 changed files with 16 additions and 10 deletions

View File

@ -3,10 +3,10 @@
{% for post in posts %} {% for post in posts %}
<url> <url>
<loc>{{ post.permalink | uriencode }}</loc> <loc>{{ post.permalink | uriencode }}</loc>
{% if post.updated %} {% if post.lastUpdated() %}
<lastmod>{{ post.updated.toISOString() }}</lastmod> <lastmod>{{ post.lastUpdated() }}</lastmod>
{% elif post.date %} {% elif post.date %}
<lastmod>{{ post.date.toISOString() }}</lastmod> <lastmod>{{ post.date }}</lastmod>
{% endif %} {% endif %}
</url> </url>
{% endfor %} {% endfor %}

View File

@ -2,6 +2,7 @@
const nanomatch = require('nanomatch') const nanomatch = require('nanomatch')
const template = require('./template') const template = require('./template')
const moment = require('moment')
module.exports = function (locals) { module.exports = function (locals) {
const config = this.config const config = this.config
@ -18,11 +19,16 @@ module.exports = function (locals) {
return post.sitemap !== false && !isMatch(post.source, skipRenderList) return post.sitemap !== false && !isMatch(post.source, skipRenderList)
}) })
.sort((a, b) => { .sort((a, b) => {
return b.updated - a.updated return b.date - a.date
}) })
.map((post) => ({ .map((post) => ({
...post, ...post,
permalink: post.permalink.replace('index.html', '') 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]')
else return false
}
})) }))
// configuration dictionary // configuration dictionary
@ -30,7 +36,7 @@ module.exports = function (locals) {
config: config, config: config,
posts: posts, posts: posts,
// add the sNow variable for creation of the home page and potential tags/cats // add the sNow variable for creation of the home page and potential tags/cats
sNow: new Date().toISOString() sNow: moment().format('YYYY-MM-DD[T00:00:00.000Z]')
} }
// add tags array available in the template // add tags array available in the template

View File

@ -1,11 +1,11 @@
'use strict'
/* global hexo */
/* /*
* Add sitemap.xml * Add sitemap.xml
* Based on https://github.com/hexojs/hexo-generator-sitemap/pull/26 * Based on https://github.com/hexojs/hexo-generator-sitemap/pull/26
*/ */
/* global hexo */
'use strict'
const pathFn = require('path') const pathFn = require('path')
const config = hexo.config.sitemap = Object.assign({ const config = hexo.config.sitemap = Object.assign({

View File

@ -2,7 +2,7 @@
const pathFn = require('path') const pathFn = require('path')
const fs = require('fs') const fs = require('fs')
let sitemapTmpl = "" let sitemapTmpl = ''
module.exports = function (config) { module.exports = function (config) {
if (sitemapTmpl) return sitemapTmpl if (sitemapTmpl) return sitemapTmpl