-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.js
64 lines (46 loc) · 2.41 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function(){
var minimatch = require('minimatch');
var url = require('url');
helpers = {
ensureOptions: (o, metadata) =>{
o = o || {};
metadata = metadata || {};
o.name = o.name || ':collection.xml';
o.author = o.author || metadata.author || '';
o.title = o.title || metadata.title || '';
o.description = o.description || metadata.description || '';
o.siteUrl = o.siteUrl || metadata.siteUrl || '';
o.generator = o.generator || 'metalsmith-rss';
o.feedOptions = helpers.ensureFunc(o.feedOptions || helpers.collectionToFeed);
o.collection = o.collection ? helpers.ensureArray(o.collection) : false;
o.collectionKey = o.collectionKey || o.collection ? 'collections' : false;
o.replaceToken = o.replaceToken || ':collection';
o.pattern = new minimatch.Minimatch(o.pattern || '**');
o.permalinkKey = o.permalinkKey || 'permalink';
o.limit = o.limit || false;
o.pathToUrl = o.pathToUrl || helpers.resolveUrl;
o.itemOptions = helpers.ensureFunc(o.itemOptions || helpers.fileToItem);
return o;
},
resolveUrl: (p, o) => url.resolve(o.siteUrl || '', p || ''),
fileToItem: (file, o) => ({
title: file.title || '',
description: (file.less || file.excerpt || file.contents || '').toString(),
author: (typeof file.author == 'object' ? file.author.name : file.author) || '',
url: file[o.permalinkKey] ? o.pathToUrl(file[o.permalinkKey], o) : o.pathToUrl(file.path || '', o),
date: file.date || '',
guid: file[o.permalinkKey] ? null : file.path
}),
collectionToFeed: (c, cc, o) => ({
generator: o.generator,
site_url: o.siteUrl,
author: (typeof o.author == 'object' ? o.author.name : o.author) || '',
description: o.description.replace(o.replaceToken, c),
title: o.title.replace(o.replaceToken, c),
feed_url: o.pathToUrl(o.name.replace(o.replaceToken, c), o)
}),
ensureFunc: o => typeof o == 'function' ? o : () => o,
ensureArray: o => o !== undefined ? [].concat(o) : [],
};
module.exports = helpers;
})();