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: custom template #110

Merged
merged 8 commits into from
Nov 27, 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
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
```
20 changes: 18 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 (!type || (typeof type !== 'string' && !Array.isArray(type))) {
Expand Down Expand Up @@ -67,8 +69,22 @@ if (typeof path === 'string') {
if (!extname(path)) path += '.xml';
}

if (typeof template !== 'string' && !Array.isArray(template)) {
template = null;
}

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

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

if (typeof type === 'string') {
hexo.extend.generator.register(type, locals => {
Expand Down
13 changes: 7 additions & 6 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ 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;

let tmplSrc = join(__dirname, `../${type}.xml`);
if (feedConfig.template) {
if (typeof feedConfig.template === 'string') tmplSrc = feedConfig.template;
else tmplSrc = feedConfig.template[feedConfig.type.indexOf(type)];
}
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