feat: add sitemap.xml

https://github.com/hexojs/hexo-generator-sitemap
This commit is contained in:
curben 2019-05-27 11:38:57 +09:30
parent 0df867fc1a
commit eb2a009ac0
6 changed files with 152 additions and 1 deletions

View File

@ -12,7 +12,7 @@ timezone:
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: /
url: https://curben.netlify.com
root: ""
permalink: :year/:month/:day/:title/
permalink_defaults:

View File

@ -0,0 +1,7 @@
Copyright (c) 2014 Tommy Chen, Alex Chauvin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in posts %}
<url>
<loc>{{ post.permalink | uriencode }}</loc>
{% if post.updated %}
<lastmod>{{ post.updated.toISOString() }}</lastmod>
{% elif post.date %}
<lastmod>{{ post.date.toISOString() }}</lastmod>
{% endif %}
</url>
{% endfor %}
{# ------------- home page - with now date #}
<url>
<loc>{{ config.url }}</loc>
<lastmod>{{ sNow }}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
{# ------------- tag pages #}
{% for tag in tags %}
<url>
<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 }}</loc>
<lastmod>{{ sNow }}</lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
</url>
{% endfor %}
</urlset>

View File

@ -0,0 +1,59 @@
'use strict'
const nanomatch = require('nanomatch')
const template = require('./template')
module.exports = function (locals) {
const config = this.config
const skipRenderList = [
'*.js',
'*.css'
]
if (Array.isArray(config.skip_render)) {
skipRenderList = skipRenderList.concat(config.skip_render)
} else if (config.skip_render != null) {
skipRenderList.push(config.skip_render)
}
const posts = [].concat(locals.posts.toArray(), locals.pages.toArray())
.filter((post) => {
return post.sitemap !== false && !isMatch(post.source, skipRenderList)
})
.sort((a, b) => {
return b.updated - a.updated
})
// configuration dictionary
const xmlConfig = {
config: config,
posts: posts,
// add the sNow variable for creation of the home page and potential tags/cats
sNow: new Date().toISOString()
}
// add tags array available in the template
if (config.sitemap.tags !== false) {
xmlConfig.tags = locals.tags.toArray()
}
// add categories available in the template
if (config.sitemap.categories !== false) {
xmlConfig.categories = locals.categories.toArray()
}
const xml = template(config).render(xmlConfig)
return {
path: config.sitemap.path,
data: xml
}
}
function isMatch (path, patterns) {
if (patterns && patterns.length) {
if (nanomatch.some(path, patterns, { matchBase: true })) return true
}
return false
}

View File

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

View File

@ -0,0 +1,24 @@
'use strict'
const pathFn = require('path')
const fs = require('fs')
let sitemapTmpl = ""
module.exports = function (config) {
if (sitemapTmpl) return sitemapTmpl
const nunjucks = require('nunjucks')
const env = new nunjucks.Environment(null, {
autoescape: false,
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)
return sitemapTmpl
}