Skip to content

Commit

Permalink
feat: render absolute feed URLs (#143)
Browse files Browse the repository at this point in the history
This PR is an update to #29.

This supposed to be a small PR, but I've encountered some issues regarding the base URL.

The `[full_url_for](https://github.com/hexojs/hexo-util/blob/master/lib/full_url_for.js#L27)` function expects the `config.url` not to end with a forward slash. Otherwise it will create a double slash URL, e.g. `http://example.com//atom.xml` (notice that a double slash replacement happens only _after_ the base URL).

I was wondering how the icon feature (#102) can be using `full_url_for` with the current code and not run into any double slash issues. The answer is: it doesn't handle this case. The [unit tests](https://github.com/hexojs/hexo-generator-feed/pull/102/files#diff-910eb6f57886ca16c136101fb1699231R240) are simply running against a custom base URL without an ending slash.

I've cleaned up the tests:
- removed the ending slash from the base URL
- adjusted and added new tests to handle subdirectories correctly (as specified in the [hexo documentation](https://hexo.io/docs/configuration.html#URL))
- hard-coded expected values - it makes a test more reliable and readable
  • Loading branch information
darekkay authored Jun 1, 2020
1 parent 866c21f commit c164383
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ module.exports = function(locals, type, path) {
if (feedConfig.icon) icon = full_url_for.call(this, feedConfig.icon);
else if (config.email) icon = gravatar(config.email);

const feed_url = full_url_for.call(this, path);

const xml = template.render({
config,
url,
icon,
posts,
feed_url: config.root + path
feed_url
});

return {
Expand Down
74 changes: 57 additions & 17 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, full_url_for } = require('hexo-util');
const { encodeURL } = require('hexo-util');
const p = require('./parse');

env.addFilter('uriencode', str => {
Expand All @@ -26,10 +26,16 @@ const customTmplSrc = join(__dirname, 'custom.xml');
const customTmlp = nunjucks.compile(readFileSync(customTmplSrc, 'utf8'), env);

const urlConfig = {
url: 'http://localhost/',
url: 'http://localhost',
root: '/'
};

const urlConfigSubfolder = {
// subdirectory configuration as per hexo documentation
url: 'http://localhost/blog',
root: '/blog/'
};

describe('Feed generator', () => {
const hexo = new Hexo(__dirname, {
silent: true
Expand Down Expand Up @@ -65,9 +71,28 @@ describe('Feed generator', () => {
result.path.should.eql('atom.xml');
result.data.should.eql(atomTmpl.render({
config: hexo.config,
url: urlConfig.url,
url: 'http://localhost/',
posts: posts.limit(3),
feed_url: hexo.config.root + 'atom.xml'
feed_url: 'http://localhost/atom.xml'
}));
});

it('type = atom (subfolder)', () => {
hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
limit: 3
};
hexo.config = Object.assign(hexo.config, urlConfigSubfolder);
const feedCfg = hexo.config.feed;
const result = generator(locals, feedCfg.type, feedCfg.path);

result.path.should.eql('atom.xml');
result.data.should.eql(atomTmpl.render({
config: hexo.config,
url: 'http://localhost/blog/',
posts: posts.limit(3),
feed_url: 'http://localhost/blog/atom.xml'
}));
});

Expand All @@ -84,9 +109,9 @@ describe('Feed generator', () => {
result.path.should.eql('rss2.xml');
result.data.should.eql(rss2Tmpl.render({
config: hexo.config,
url: urlConfig.url,
url: 'http://localhost/',
posts: posts.limit(3),
feed_url: hexo.config.root + 'rss2.xml'
feed_url: 'http://localhost/rss2.xml'
}));
});

Expand All @@ -103,9 +128,9 @@ describe('Feed generator', () => {
result.path.should.eql('atom.xml');
result.data.should.eql(atomTmpl.render({
config: hexo.config,
url: urlConfig.url,
url: 'http://localhost/',
posts: posts,
feed_url: hexo.config.root + 'atom.xml'
feed_url: 'http://localhost/atom.xml'
}));
});

Expand Down Expand Up @@ -196,9 +221,7 @@ describe('Feed generator', () => {
path: file
};

const domain = 'http://example.com/';

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

Expand All @@ -209,9 +232,9 @@ describe('Feed generator', () => {
atom.link.should.eql(valid);
};

await checkURL('/', '/' + file);
await checkURL('/', 'http://example.com', 'http://example.com/' + file);

await checkURL('blo g/', 'blo%20g/' + file);
await checkURL('blo g/', 'http://example.com/blo%20g', 'http://example.com/blo%20g/' + file);
});

it('Prints an enclosure on `image` metadata', async () => {
Expand Down Expand Up @@ -255,7 +278,7 @@ describe('Feed generator', () => {
const result = generator(locals, feedCfg.type, feedCfg.path);
const atom = await p(result.data);

atom.icon.should.eql(full_url_for.call(hexo, hexo.config.feed.icon));
atom.icon.should.eql('http://example.com/icon.svg');
});

it('Icon (atom) - no icon', async () => {
Expand Down Expand Up @@ -286,7 +309,7 @@ describe('Feed generator', () => {
const result = generator(locals, feedCfg.type, feedCfg.path);
const rss = await p(result.data);

rss.icon.url.should.eql(full_url_for.call(hexo, hexo.config.feed.icon));
rss.icon.url.should.eql('http://example.com/icon.svg');
});

it('Icon (rss2) - no icon', async () => {
Expand All @@ -303,6 +326,23 @@ describe('Feed generator', () => {
rss.icon.url.length.should.eql(0);
});

it('Icon (rss2) - subdirectory', async () => {
hexo.config.url = 'http://example.com/blog';
hexo.config.root = '/blog/';

hexo.config.feed = {
type: 'rss2',
path: 'rss2.xml',
icon: 'icon.svg'
};

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

rss.icon.url.should.eql('http://example.com/blog/icon.svg');
});

it('path must follow order of type', () => {
hexo.config.feed = {
type: ['rss2', 'atom'],
Expand Down Expand Up @@ -330,9 +370,9 @@ describe('Feed generator', () => {

result.data.should.eql(customTmlp.render({
config: hexo.config,
url: urlConfig.url,
url: 'http://localhost/',
posts,
feed_url: hexo.config.root + feedCfg.path
feed_url: 'http://localhost/atom.xml'
}));
});
});
Expand Down

0 comments on commit c164383

Please sign in to comment.