Skip to content

Commit

Permalink
Merge pull request #4029 from curbengh/feed_tag-atom-rss
Browse files Browse the repository at this point in the history
feat(feed_tag): support parsing config.feed
  • Loading branch information
curbengh committed Dec 31, 2019
2 parents 663f1eb + f45dda2 commit 198d818
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 9 deletions.
40 changes: 38 additions & 2 deletions lib/plugins/helper/feed_tag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
'use strict';

const feedFn = (str = '') => {
if (str) return str.replace(/2$/, '');
return str;
};

function feedTagHelper(path, options = {}) {
const title = options.title || this.config.title;
const { config } = this;
const title = options.title || config.title;

if (path) {
if (typeof path !== 'string') throw new TypeError('path must be a string!');

let type = feedFn(options.type);

if (!type) {
if (path.includes('atom')) type = 'atom';
else if (path.includes('rss')) type = 'rss';
}

const typeAttr = type ? `type="application/${type}+xml"` : '';

return `<link rel="alternate" href="${this.url_for(path)}" title="${title}" ${typeAttr}>`;
}

if (!path && config.feed) {
const { feed } = config;
if (feed.type && feed.path) {
if (typeof feed.type === 'string') {
return `<link rel="alternate" href="${this.url_for(feed.path)}" title="${title}" type="application/${feedFn(feed.type)}+xml">`;
}

let result = '';
for (const i in feed.type) {
result += `<link rel="alternate" href="${this.url_for(feed.path[i])}" title="${title}" type="application/${feedFn(feed.type[i])}+xml">`;
}
return result;
}
}

return `<link rel="alternate" href="${this.url_for(path)}" title="${title}">`;
return '';
}

module.exports = feedTagHelper;
78 changes: 71 additions & 7 deletions test/scripts/helpers/feed_tag.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
'use strict';

describe('feed_tag', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname);

const ctx = {
config: hexo.config
config: {
title: 'Hexo',
url: 'http://yoursite.com',
root: '/',
feed: {}
}
};

beforeEach(() => { ctx.config.feed = {}; });

ctx.url_for = require('../../../lib/plugins/helper/url_for').bind(ctx);

const feed = require('../../../lib/plugins/helper/feed_tag').bind(ctx);

it('path', () => {
feed('atom.xml').should.eql('<link rel="alternate" href="/atom.xml" title="Hexo">');
feed('atom.xml').should.eql('<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">');
});

it('title', () => {
feed('atom.xml', {title: 'RSS Feed'}).should.eql('<link rel="alternate" href="/atom.xml" title="RSS Feed">');
feed('atom.xml', {title: 'RSS Feed'}).should.eql('<link rel="alternate" href="/atom.xml" title="RSS Feed" type="application/atom+xml">');
});

it('type', () => {
feed('rss.xml', {type: 'rss'}).should.eql('<link rel="alternate" href="/rss.xml" title="Hexo">');
feed('rss.xml', {type: 'rss'}).should.eql('<link rel="alternate" href="/rss.xml" title="Hexo" type="application/rss+xml">');
});

it('type - null', () => {
feed('foo.xml', {type: null}).should.eql('<link rel="alternate" href="/foo.xml" title="Hexo" >');
});

it('invalid input - number', () => {
try {
feed(123);
} catch (err) {
err.message.should.eql('path must be a string!');
}
});

it('invalid input - undefined', () => {
ctx.config.feed = {};
const result = feed();
const typeOf = str => typeof str;

typeOf(result).should.eql('string');
result.length.should.eql(0);
});

it('feed - parse argument if available', () => {
ctx.config.feed = {
type: 'atom',
path: 'atom.xml'
};

feed('atomic.xml').should.eql('<link rel="alternate" href="/atomic.xml" title="Hexo" type="application/atom+xml">');
});

it('feed - atom', () => {
ctx.config.feed = {
type: 'atom',
path: 'atom.xml'
};

feed().should.eql('<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">');
});

it('feed - rss2', () => {
ctx.config.feed = {
type: 'rss2',
path: 'rss.xml'
};

feed().should.eql('<link rel="alternate" href="/rss.xml" title="Hexo" type="application/rss+xml">');
});

it('feed - rss2', () => {
ctx.config.feed = {
type: ['atom', 'rss2'],
path: ['atom.xml', 'rss.xml']
};

feed().should.eql([
'<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">',
'<link rel="alternate" href="/rss.xml" title="Hexo" type="application/rss+xml">'
].join(''));
});
});

0 comments on commit 198d818

Please sign in to comment.