-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathindex.js
100 lines (82 loc) · 2.34 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* global hexo */
'use strict';
const { extname, join } = require('path');
hexo.config.feed = Object.assign({
enable: true,
type: 'atom',
limit: 20,
hub: '',
content: true,
content_limit: 140,
content_limit_delim: '',
order_by: '-date',
autodiscovery: true,
template: ''
}, hexo.config.feed);
const config = hexo.config.feed;
if (!config.enable) {
return;
}
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))) {
type = 'atom';
}
if (Array.isArray(type)) {
type = [...new Set(type.filter(item => ['atom', 'rss2'].includes(item)))];
if (type.length === 0) {
type = 'atom';
}
}
if (typeof type === 'string') {
if (type !== 'atom' && type !== 'rss2') type = 'atom';
}
if (!path || typeof path !== typeof type) {
if (typeof type === 'string') path = type.concat('.xml');
else path = type.map(str => str.concat('.xml'));
}
if (Array.isArray(path)) {
if (path.length !== type.length) {
if (path.length > type.length) path = path.slice(0, type.length);
else if (path.length === 0) path = type.map(str => str.concat('.xml'));
else path.push(type[1].concat('.xml'));
}
path = path.map(str => {
if (!extname(str)) return str.concat('.xml');
return str;
});
}
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 => {
return feedFn.call(hexo, locals, type, path);
});
} else {
for (const feedType of type) {
hexo.extend.generator.register(feedType, locals => {
return feedFn.call(hexo, locals, feedType, path[type.indexOf(feedType)]);
});
}
}
if (typeof config.autodiscovery !== 'boolean') config.autodiscovery = true;
if (config.autodiscovery === true) {
hexo.extend.filter.register('after_render:html', require('./lib/autodiscovery'));
}