-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4029 from curbengh/feed_tag-atom-rss
feat(feed_tag): support parsing config.feed
- Loading branch information
Showing
2 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('')); | ||
}); | ||
}); |