Skip to content

Commit

Permalink
Merge c13ebf6 into dd558c6
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 21, 2019
2 parents dd558c6 + c13ebf6 commit 941da4d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ feed:
order_by: -date
icon: icon.png
autodiscovery: true
template:
```

- **type** - Feed type. `atom` or `rss2`. Specify `['atom', 'rss2']` to output both types. (Default: `atom`)
Expand Down Expand Up @@ -63,3 +64,20 @@ feed:
- **icon** - (optional) Custom feed icon. Defaults to a gravatar of email specified in the main config.
- **autodiscovery** - Add feed [autodiscovery](http://www.rssboard.org/rss-autodiscovery). (Default: `true`)
* Many themes already offer this feature, so you may also need to adjust the theme's config if you wish to disable it.
- **template** - Custom template path(s). This file will be used to generate feed xml file, see the default templates: [atom.xml](atom.xml) and [rss2.xml](rss2.xml).
* It is possible to specify just one custom template, even when this plugin is configured to output both feed types,
``` yaml
# (Optional) Exclude custom template from being copied into public/ folder
# Alternatively, you could also prepend an underscore to its filename, e.g. _custom.xml
# https://hexo.io/docs/configuration#Include-Exclude-Files-or-Folders
exclude:
- 'custom.xml'
feed:
type:
- atom
- rss2
template:
- ./source/custom.xml
# atom will be generated using custom.xml
# rss2 will be generated using the default template instead
```
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global hexo */
'use strict';

const { extname } = require('path');
const { extname, join } = require('path');

const config = hexo.config.feed = Object.assign({
type: 'atom',
Expand All @@ -11,11 +11,13 @@ const config = hexo.config.feed = Object.assign({
content_limit: 140,
content_limit_delim: '',
order_by: '-date',
autodiscovery: true
autodiscovery: true,
template: ''
}, hexo.config.feed);

let type = config.type;
let path = config.path;
let template = config.template;
const feedFn = require('./lib/generator');

if (typeof type === 'string') type = [type];
Expand Down Expand Up @@ -50,8 +52,24 @@ path = path.map(str => {
return str;
});

if (typeof template === 'string') {
if (template.length >= 1) template = [template];
}

if (Array.isArray(template)) {
if (template.length >= 1) {
if (template.length > 2) template = template.slice(0, 2);
if (template.length < type.length) template.push(join(__dirname, `${type[1]}.xml`));
} else {
template = null;
}
} else {
template = null;
}

config.type = type;
config.path = path;
config.template = template;

for (const feedType of type) {
hexo.extend.generator.register(feedType, locals => {
Expand Down
10 changes: 4 additions & 6 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ env.addFilter('noControlChars', str => {
return str.replace(/[\x00-\x1F\x7F]/g, ''); // eslint-disable-line no-control-regex
});

const atomTmplSrc = join(__dirname, '../atom.xml');
const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env);
const rss2TmplSrc = join(__dirname, '../rss2.xml');
const rss2Tmpl = nunjucks.compile(readFileSync(rss2TmplSrc, 'utf8'), env);

module.exports = function(locals, type, path) {
const config = this.config;
const feedConfig = config.feed;
const template = type === 'atom' ? atomTmpl : rss2Tmpl;

const tmplSrc = feedConfig.template ? feedConfig.template[feedConfig.type.indexOf(type)]
: join(__dirname, `../${type}.xml`);
const template = nunjucks.compile(readFileSync(tmplSrc, 'utf8'), env);

let posts = locals.posts.sort(feedConfig.order_by || '-date');
posts = posts.filter(post => {
Expand Down
8 changes: 8 additions & 0 deletions test/custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<aaa>
{% for post in posts.toArray() %}
<entry>
<published>{{ post.date.toISOString() }}</published>
</entry>
{% endfor %}
</aaa>
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const atomTmplSrc = join(__dirname, '../atom.xml');
const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env);
const rss2TmplSrc = join(__dirname, '../rss2.xml');
const rss2Tmpl = nunjucks.compile(readFileSync(rss2TmplSrc, 'utf8'), env);
const customTmplSrc = join(__dirname, 'custom.xml');
const customTmlp = nunjucks.compile(readFileSync(customTmplSrc, 'utf8'), env);

const urlConfig = {
url: 'http://localhost/',
Expand Down Expand Up @@ -320,6 +322,24 @@ describe('Feed generator', () => {
const atom = generator(locals, feedCfg.type[1], feedCfg.path[1]);
atom.path.should.eql(hexo.config.feed.path[1]);
});

it('custom template', () => {
hexo.config.feed = {
type: ['atom'],
path: 'atom.xml',
template: ['test/custom.xml']
};
hexo.config = Object.assign(hexo.config, urlConfig);
const feedCfg = hexo.config.feed;
const result = generator(locals, feedCfg.type[0], feedCfg.path);

result.data.should.eql(customTmlp.render({
config: hexo.config,
url: urlConfig.url,
posts,
feed_url: hexo.config.root + feedCfg.path
}));
});
});

it('No posts', () => {
Expand Down

0 comments on commit 941da4d

Please sign in to comment.