From eb2a009ac057bb748008b07dacc2458d750f55b8 Mon Sep 17 00:00:00 2001 From: curben Date: Mon, 27 May 2019 11:38:57 +0930 Subject: [PATCH] feat: add sitemap.xml https://github.com/hexojs/hexo-generator-sitemap --- _config.yml | 2 +- themes/typing/scripts/sitemap/.LICENSE | 7 +++ themes/typing/scripts/sitemap/.sitemap.xml | 42 +++++++++++++++ themes/typing/scripts/sitemap/generator.js | 59 ++++++++++++++++++++++ themes/typing/scripts/sitemap/index.js | 19 +++++++ themes/typing/scripts/sitemap/template.js | 24 +++++++++ 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 themes/typing/scripts/sitemap/.LICENSE create mode 100644 themes/typing/scripts/sitemap/.sitemap.xml create mode 100644 themes/typing/scripts/sitemap/generator.js create mode 100644 themes/typing/scripts/sitemap/index.js create mode 100644 themes/typing/scripts/sitemap/template.js diff --git a/_config.yml b/_config.yml index ea1766f..e3f94ae 100644 --- a/_config.yml +++ b/_config.yml @@ -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: diff --git a/themes/typing/scripts/sitemap/.LICENSE b/themes/typing/scripts/sitemap/.LICENSE new file mode 100644 index 0000000..34623dc --- /dev/null +++ b/themes/typing/scripts/sitemap/.LICENSE @@ -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. \ No newline at end of file diff --git a/themes/typing/scripts/sitemap/.sitemap.xml b/themes/typing/scripts/sitemap/.sitemap.xml new file mode 100644 index 0000000..302dd1e --- /dev/null +++ b/themes/typing/scripts/sitemap/.sitemap.xml @@ -0,0 +1,42 @@ + + + {% for post in posts %} + + {{ post.permalink | uriencode }} + {% if post.updated %} + {{ post.updated.toISOString() }} + {% elif post.date %} + {{ post.date.toISOString() }} + {% endif %} + + {% endfor %} + + {# ------------- home page - with now date #} + + {{ config.url }} + {{ sNow }} + daily + 1.0 + + + {# ------------- tag pages #} + {% for tag in tags %} + + {{ tag.permalink }} + {{ sNow }} + daily + 0.6 + + {% endfor %} + + {# ------------- categories pages #} + {% for cat in categories %} + + {{ cat.permalink }} + {{ sNow }} + daily + 0.6 + + {% endfor %} + + diff --git a/themes/typing/scripts/sitemap/generator.js b/themes/typing/scripts/sitemap/generator.js new file mode 100644 index 0000000..afe00db --- /dev/null +++ b/themes/typing/scripts/sitemap/generator.js @@ -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 +} diff --git a/themes/typing/scripts/sitemap/index.js b/themes/typing/scripts/sitemap/index.js new file mode 100644 index 0000000..b327f98 --- /dev/null +++ b/themes/typing/scripts/sitemap/index.js @@ -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')) diff --git a/themes/typing/scripts/sitemap/template.js b/themes/typing/scripts/sitemap/template.js new file mode 100644 index 0000000..eb7ab01 --- /dev/null +++ b/themes/typing/scripts/sitemap/template.js @@ -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 +}