From c422ff91ee0421f63ac08db3245691be8678e3c6 Mon Sep 17 00:00:00 2001 From: MDLeom <2809763-curben@users.noreply.gitlab.com> Date: Fri, 21 Aug 2020 12:37:10 +0000 Subject: [PATCH] refactor: utilise full_url_for() - https://github.com/hexojs/hexo-generator-feed/pull/149 --- themes/chameleon/scripts/feed/.atom.xml | 4 ++-- themes/chameleon/scripts/feed/generator.js | 11 ++++++++--- themes/chameleon/scripts/sitemap/.sitemap.xml | 4 ++-- themes/chameleon/scripts/sitemap/template.js | 5 +++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/themes/chameleon/scripts/feed/.atom.xml b/themes/chameleon/scripts/feed/.atom.xml index 672e25b..93988e6 100644 --- a/themes/chameleon/scripts/feed/.atom.xml +++ b/themes/chameleon/scripts/feed/.atom.xml @@ -45,10 +45,10 @@ {% endif %} {% endif %} {% if post.image %} - + {% endif %} {% for tag in post.tags.toArray() %} - + {% endfor %} {% endfor %} diff --git a/themes/chameleon/scripts/feed/generator.js b/themes/chameleon/scripts/feed/generator.js index c828ddc..5a29fb5 100644 --- a/themes/chameleon/scripts/feed/generator.js +++ b/themes/chameleon/scripts/feed/generator.js @@ -6,7 +6,6 @@ const { join } = require('path') const { readFileSync } = require('fs') const moment = require('moment') const { encodeURL, full_url_for } = require('hexo-util') -const { format } = require('url') env.addFilter('uriencode', str => { return encodeURL(str) @@ -25,10 +24,14 @@ const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env) module.exports = function (locals) { const { config } = this - const { feed, root, url } = config + const { feed, url } = config const { icon: iconCfg, limit, order_by, path } = feed const template = atomTmpl + env.addFilter('fullUrlFor', str => { + return full_url_for.call(this, str) + }) + let posts = locals.posts.sort(order_by || '-date') posts = posts.filter(post => { return post.draft !== true @@ -38,12 +41,14 @@ module.exports = function (locals) { const icon = iconCfg ? full_url_for.call(this, iconCfg) : '' + const feed_url = full_url_for.call(this, path); + const data = template.render({ config, url, icon, posts, - feed_url: root + path + feed_url }) return { diff --git a/themes/chameleon/scripts/sitemap/.sitemap.xml b/themes/chameleon/scripts/sitemap/.sitemap.xml index 9b7face..a4c3544 100644 --- a/themes/chameleon/scripts/sitemap/.sitemap.xml +++ b/themes/chameleon/scripts/sitemap/.sitemap.xml @@ -13,13 +13,13 @@ {# home page #} - {{ config.url + config.root }} + {{ config.root | fullUrlFor }} {% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %} {# archive page #} - {{ config.url + '/' + config.archive_dir + '/' }} + {{ config.archive_dir | fullUrlFor + '/' }} {% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %} diff --git a/themes/chameleon/scripts/sitemap/template.js b/themes/chameleon/scripts/sitemap/template.js index 213f39c..ebabecd 100644 --- a/themes/chameleon/scripts/sitemap/template.js +++ b/themes/chameleon/scripts/sitemap/template.js @@ -3,6 +3,7 @@ const { join } = require('path') const { readFileSync } = require('fs') const moment = require('moment') +const { full_url_for } = require('hexo-util') let sitemapTmpl = '' module.exports = function (config) { @@ -18,6 +19,10 @@ module.exports = function (config) { return moment(str).format('YYYY-MM-DD[T00:00:00.000Z]').substring(0, 10) }) + env.addFilter('fullUrlFor', str => { + return full_url_for.call({ config }, str) + }) + const sitemapSrc = config.sitemap.template || join(__dirname, '.sitemap.xml') sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env)