Skip to content

Commit

Permalink
Merge pull request #18 from f213/master
Browse files Browse the repository at this point in the history
URL handling small fix
  • Loading branch information
漩涡 committed Aug 8, 2015
2 parents de50b8c + 8ac4087 commit a1b7a6e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
1 change: 0 additions & 1 deletion atom.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<% var url = config.url + config.root %>
<title><%-: config.title | cdata %></title>
<% if (config.subtitle){ %><subtitle><%-: config.subtitle | cdata %></subtitle><% } %>
<link href="<%- encodeURI(feed_url) %>" rel="self"/>
Expand Down
14 changes: 14 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ module.exports = function(locals){
var posts = locals.posts.sort('-date');
if (feedConfig.limit) posts = posts.limit(feedConfig.limit);

var url;

// Sorry for that, it is hexo URL handling way.
if (config.root === '/'){
url = config.url + config.root;
}
else
{
url = config.url + '/';
}

// And sorry for that too.
url = url.replace(/([^:])\/\//g, '$1/');

var xml = template({
config: config,
url: url,
posts: posts,
feed_url: config.root + feedConfig.path
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"devDependencies": {
"chai": "^1.9.1",
"cheerio": "^0.19.0",
"coveralls": "^2.11.2",
"gulp": "^3.8.9",
"gulp-istanbul": "^0.5.0",
Expand Down
3 changes: 1 addition & 2 deletions rss2.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<% var url = config.url + config.root %>
<title><%-: config.title | cdata %></title>
<link><%- encodeURI(url) %></link>
<atom:link href="<%- encodeURI(feed_url) %>" rel="self" type="application/rss+xml"/>
Expand All @@ -22,7 +21,7 @@
<% } else {%><%-: post.content.substring(0, 140) | cdata %><% } %>
</description>
<content:encoded><%-: post.content | cdata %></content:encoded>
<% if (post.comments){ %><comments><%- encodeURI(url + post.path) %>#disqus_comments</comments><% } %>
<% if (post.comments){ %><comments><%- encodeURI(url + post.path) %>#disqus_thread</comments><% } %>
</item>
<% }) %>
</channel>
Expand Down
45 changes: 43 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var Hexo = require('hexo');
var ejs = require('ejs');
var pathFn = require('path');
var fs = require('fs');
var assign = require('object-assign');
var cheerio = require('cheerio');

ejs.filters.cdata = function(str){
return str ? '<![CDATA[' + str + ']]>' : '';
Expand All @@ -13,6 +15,11 @@ var atomTmpl = ejs.compile(fs.readFileSync(atomTmplSrc, 'utf8'));
var rss2TmplSrc = pathFn.join(__dirname, '../rss2.ejs');
var rss2Tmpl = ejs.compile(fs.readFileSync(rss2TmplSrc, 'utf8'));

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

describe('Feed generator', function(){
var hexo = new Hexo(__dirname, {silent: true});
var Post = hexo.model('Post');
Expand All @@ -37,12 +44,13 @@ describe('Feed generator', function(){
path: 'atom.xml',
limit: 2
};

hexo.config = assign(hexo.config, urlConfig);
var result = generator(locals);

result.path.should.eql('atom.xml');
result.data.should.eql(atomTmpl({
config: hexo.config,
url: urlConfig.url,
posts: posts.limit(2),
feed_url: hexo.config.root + 'atom.xml'
}));
Expand All @@ -54,12 +62,13 @@ describe('Feed generator', function(){
path: 'rss2.xml',
limit: 2
};

hexo.config = assign(hexo.config, urlConfig);
var result = generator(locals);

result.path.should.eql('rss2.xml');
result.data.should.eql(rss2Tmpl({
config: hexo.config,
url: urlConfig.url,
posts: posts.limit(2),
feed_url: hexo.config.root + 'rss2.xml'
}));
Expand All @@ -71,14 +80,46 @@ describe('Feed generator', function(){
path: 'atom.xml',
limit: 0
};
hexo.config = assign(hexo.config, urlConfig);

var result = generator(locals);

result.path.should.eql('atom.xml');
result.data.should.eql(atomTmpl({
config: hexo.config,
url: urlConfig.url,
posts: posts,
feed_url: hexo.config.root + 'atom.xml'
}));
});

it('Relative URL handling', function(){
hexo.config.feed = {
type: 'atom',
path: 'atom.xml'
};

var checkURL = function(root, path, valid){
hexo.config.url = root;
hexo.config.path = path;

var result = generator(locals);
var $ = cheerio.load(result.data);

$('feed>id').text().should.eql(valid);
$('feed>entry>link').attr('href').should.eql(valid);
}

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

var GOOD = 'http://localhost/blog/';

checkURL('http://localhost/blog', '/blog/', GOOD);
checkURL('http://localhost/blog', '/blog', GOOD);
checkURL('http://localhost/blog/', '/blog/', GOOD);
checkURL('http://localhost/blog/', '/blog', GOOD);

checkURL('http://localhost/b/l/o/g', '/', 'http://localhost/b/l/o/g/');

});
});

0 comments on commit a1b7a6e

Please sign in to comment.