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 all commits
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
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(''));
});
});