Skip to content

Commit

Permalink
fix(template): support both string and array
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 27, 2019
1 parent fc1637e commit 2e54192
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 4 additions & 6 deletions index.js
Expand Up @@ -69,19 +69,17 @@ if (typeof path === 'string') {
if (!extname(path)) path += '.xml';
}

if (typeof template === 'string') {
if (template.length >= 1) template = [template];
if (typeof template !== 'string' && !Array.isArray(template)) {
template = null;
}

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`));
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;
}
} else {
template = null;
}

config.type = type;
Expand Down
7 changes: 5 additions & 2 deletions lib/generator.js
Expand Up @@ -18,8 +18,11 @@ module.exports = function(locals, type, path) {
const config = this.config;
const feedConfig = config.feed;

const tmplSrc = feedConfig.template ? feedConfig.template[feedConfig.type.indexOf(type)]
: join(__dirname, `../${type}.xml`);
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');
Expand Down

0 comments on commit 2e54192

Please sign in to comment.