Skip to content

Commit

Permalink
Merge pull request #149 from curbengh/url-for
Browse files Browse the repository at this point in the history
refactor: utilize full_url_for()
  • Loading branch information
curbengh committed Jul 2, 2020
2 parents 95cc7cc + 8a0db16 commit fa1f04a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
6 changes: 3 additions & 3 deletions atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
{% endif %}
{% endif %}
{% if post.image %}
<content src="{{ url + post.image | uriencode }}" type="image"/>
<content src="{{ post.image | formatUrl }}" type="image"/>
{% endif %}
{% for category in post.categories.toArray() %}
<category term="{{ category.name }}" scheme="{{ url + category.path | uriencode }}"/>
<category term="{{ category.name }}" scheme="{{ category.permalink }}"/>
{% endfor %}
{% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ url + tag.path | uriencode }}"/>
<category term="{{ tag.name }}" scheme="{{ tag.permalink }}"/>
{% endfor %}
</entry>
{% endfor %}
Expand Down
4 changes: 4 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = function(locals, type, path) {
const { email, feed, url: urlCfg } = config;
const { icon: iconCfg, limit, order_by, template: templateCfg, type: typeCfg } = feed;

env.addFilter('formatUrl', str => {
return full_url_for.call(this, str);
});

let tmplSrc = join(__dirname, `../${type}.xml`);
if (templateCfg) {
if (typeof templateCfg === 'string') tmplSrc = templateCfg;
Expand Down
33 changes: 27 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const env = new nunjucks.Environment();
const { join } = require('path');
const { readFileSync } = require('fs');
const cheerio = require('cheerio');
const { encodeURL } = require('hexo-util');
const { encodeURL, full_url_for } = require('hexo-util');
const p = require('./parse');

env.addFilter('uriencode', str => {
Expand Down Expand Up @@ -40,6 +40,11 @@ describe('Feed generator', () => {
const hexo = new Hexo(__dirname, {
silent: true
});

env.addFilter('formatUrl', str => {
return full_url_for.call(hexo, str);
});

const Post = hexo.model('Post');
const generator = require('../lib/generator').bind(hexo);

Expand Down Expand Up @@ -243,25 +248,41 @@ describe('Feed generator', () => {
path: 'atom.xml'
};

const checkURL = async function(url, root, index) {
const checkURL = async function(url, root) {
hexo.config.url = url;
hexo.config.root = root;

const feedCfg = hexo.config.feed;
const result = generator(locals, feedCfg.type, feedCfg.path);

const feed = await p(result.data);
feed.items[index].image.should.not.eql('');
const { items } = await p(result.data);
const postImg = items.filter(({ image }) => image.length)[0];
postImg.image.length.should.not.eql(0);
};

await checkURL('http://localhost/', '/', 2);
await checkURL('http://localhost/', '/');

hexo.config.feed = {
type: 'rss2',
path: 'rss2.xml',
content: true
};
await checkURL('http://localhost/', '/', 2);
await checkURL('http://localhost/', '/');
});

it('Image should have full link', async () => {
hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
limit: 3
};
hexo.config = Object.assign(hexo.config, urlConfig);
const feedCfg = hexo.config.feed;
const result = generator(locals, feedCfg.type, feedCfg.path);
const { items } = await p(result.data);
const postImg = items.filter(({ image }) => image.length)[0];

postImg.image.should.eql(full_url_for.call(hexo, 'test.png'));
});

it('Icon (atom)', async () => {
Expand Down

0 comments on commit fa1f04a

Please sign in to comment.