Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(feed_tag): support parsing config.feed #4029

Merged
merged 3 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions lib/plugins/helper/feed_tag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
'use strict';

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

return `<link rel="alternate" href="${this.url_for(path)}" title="${title}">`;
if (path) {
if (typeof path !== 'string') throw new TypeError('path must be a string!');
const { type } = options;
let typeAttr = '';

if (type === 'atom' || path.includes('atom')) typeAttr = 'type="application/atom+xml"';
else if (type.includes('rss') || path.includes('rss')) typeAttr = 'type="application/rss+xml"';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an edge case, feed_tag('feed.xml') and type is not specified; typeAttr will be empty as "feed.xml" can refer to atom or rss.

It's fine to leave it empty since currently this helper doesn't add type="application/atom+xml".


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

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

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

return '';
}

module.exports = feedTagHelper;
74 changes: 67 additions & 7 deletions test/scripts/helpers/feed_tag.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,86 @@
'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('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(''));
});
});