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

View File

@ -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 {

View File

@ -13,13 +13,13 @@
{# home page #}
<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>
</url>
{# archive page #}
<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>
</url>

View File

@ -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)