refactor: utilise full_url_for()

- https://github.com/hexojs/hexo-generator-feed/pull/149
This commit is contained in:
MDLeom 2020-08-21 12:37:10 +00:00
parent 25aab0be28
commit c422ff91ee
No known key found for this signature in database
GPG Key ID: 32D3E28E96A695E8
4 changed files with 17 additions and 7 deletions

View File

@ -45,10 +45,10 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if post.image %} {% if post.image %}
<content src="{{ url + post.image | uriencode }}" type="image" /> <content src="{{ post.image | fullUrlFor }}" type="image" />
{% endif %} {% endif %}
{% for tag in post.tags.toArray() %} {% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ url + tag.path | uriencode }}"/> <category term="{{ tag.name }}" scheme="{{ tag.path | fullUrlFor }}"/>
{% endfor %} {% endfor %}
</entry> </entry>
{% endfor %} {% endfor %}

View File

@ -6,7 +6,6 @@ const { join } = require('path')
const { readFileSync } = require('fs') const { readFileSync } = require('fs')
const moment = require('moment') const moment = require('moment')
const { encodeURL, full_url_for } = require('hexo-util') const { encodeURL, full_url_for } = require('hexo-util')
const { format } = require('url')
env.addFilter('uriencode', str => { env.addFilter('uriencode', str => {
return encodeURL(str) return encodeURL(str)
@ -25,10 +24,14 @@ const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env)
module.exports = function (locals) { module.exports = function (locals) {
const { config } = this const { config } = this
const { feed, root, url } = config const { feed, url } = config
const { icon: iconCfg, limit, order_by, path } = feed const { icon: iconCfg, limit, order_by, path } = feed
const template = atomTmpl const template = atomTmpl
env.addFilter('fullUrlFor', str => {
return full_url_for.call(this, str)
})
let posts = locals.posts.sort(order_by || '-date') let posts = locals.posts.sort(order_by || '-date')
posts = posts.filter(post => { posts = posts.filter(post => {
return post.draft !== true return post.draft !== true
@ -38,12 +41,14 @@ module.exports = function (locals) {
const icon = iconCfg ? full_url_for.call(this, iconCfg) : '' const icon = iconCfg ? full_url_for.call(this, iconCfg) : ''
const feed_url = full_url_for.call(this, path);
const data = template.render({ const data = template.render({
config, config,
url, url,
icon, icon,
posts, posts,
feed_url: root + path feed_url
}) })
return { return {

View File

@ -13,13 +13,13 @@
{# home page #} {# home page #}
<url> <url>
<loc>{{ config.url + config.root }}</loc> <loc>{{ config.root | fullUrlFor }}</loc>
<lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod> <lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod>
</url> </url>
{# archive page #} {# archive page #}
<url> <url>
<loc>{{ config.url + '/' + config.archive_dir + '/' }}</loc> <loc>{{ config.archive_dir | fullUrlFor + '/' }}</loc>
<lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod> <lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod>
</url> </url>

View File

@ -3,6 +3,7 @@
const { join } = require('path') const { join } = require('path')
const { readFileSync } = require('fs') const { readFileSync } = require('fs')
const moment = require('moment') const moment = require('moment')
const { full_url_for } = require('hexo-util')
let sitemapTmpl = '' let sitemapTmpl = ''
module.exports = function (config) { 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) 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') const sitemapSrc = config.sitemap.template || join(__dirname, '.sitemap.xml')
sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env) sitemapTmpl = nunjucks.compile(readFileSync(sitemapSrc, 'utf8'), env)