Skip to content

Commit

Permalink
fix(sitemap): use date only in <lastmod> (#94)
Browse files Browse the repository at this point in the history
* make the date formate right

experiment: ACCEPT by baidu and google after modified
### Reference:
[baidu](https://ziyuan.baidu.com/college/courseinfo?id=267&page=2#h2_article_title16)
[google](https://support.google.com/webmasters/answer/183668?hl=en&ref_topic=4581190)
[sitemap](https://www.sitemaps.org/protocol.html)
[bing](https://www.bing.com/webmaster/help/how-to-submit-sitemaps-82a15bd4)
[yandex](https://yandex.ru/support/webmaster/controlling-robot/sitemap.html)

* refactor: move date formatting into a dedicated function

Co-authored-by: MDLeom <43627182+curbengh@users.noreply.github.com>
  • Loading branch information
flashlab and curbengh committed Jun 30, 2020
1 parent c9c2d90 commit a0e233b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = function(config) {
return encodeURL(str);
});

env.addFilter('formatDate', input => {
return input.toISOString().substring(0, 10);
});

const sitemapSrc = config.sitemap.template || join(__dirname, '../sitemap.xml');
sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env);

Expand Down
4 changes: 2 additions & 2 deletions sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<url>
<loc>{{ post.permalink | uriencode }}</loc>
{% if post.updated %}
<lastmod>{{ post.updated.toISOString() }}</lastmod>
<lastmod>{{ post.updated | formatDate }}</lastmod>
{% elif post.date %}
<lastmod>{{ post.date.toISOString() }}</lastmod>
<lastmod>{{ post.date | formatDate }}</lastmod>
{% endif %}
</url>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Sitemap generator', () => {
const { items } = await p(result.data);
items.forEach((element, index) => {
element.link.should.eql(posts[index].permalink);
element.date.should.eql(posts[index].updated.toISOString());
element.date.should.eql(posts[index].updated.toISOString().substring(0, 10));
});
});

Expand Down

0 comments on commit a0e233b

Please sign in to comment.