-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
82 lines (57 loc) · 1.86 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
const { join } = require('path'),
{ VEvent } = require('icalendar');
hexo.extend.helper.register('echo', console.log);
hexo.extend.helper.register('groupBy', (list, grouper) => {
const data = {};
for (const item of list) {
let keys = grouper instanceof Function ? grouper(item) : item[grouper];
if (!(keys instanceof Array)) keys = [keys];
for (const key of new Set(keys)) (data[key] = data[key] || []).push(item);
}
return data;
});
hexo.extend.helper.register('getPosts', ({ pagination, filter }) => {
const { order_by, per_page } = hexo.config.index_generator;
var { data } = hexo.locals.get('posts').sort(order_by);
if (filter instanceof Function) data = data.filter(filter);
return data.slice(0, pagination ? per_page : Infinity);
});
hexo.extend.helper.register('hasCategory', (post, name) =>
Array.from((post.categories || '').data, ({ name }) => name).includes(name)
);
const type_map = {
doc: 'word',
docx: 'word',
xls: 'excel',
xlsx: 'excel',
ppt: 'powerpoint',
pptx: 'powerpoint',
};
hexo.extend.helper.register('fileType', (path) => {
var type = path.split('.');
if (!type[1]) return;
type = type.slice(-1)[0].toLowerCase();
return type_map[type] || type;
});
hexo.extend.helper.register('joinPath', (base, path) =>
join(base.replace(/index\.html$/, ''), encodeURI(path))
);
hexo.extend.helper.register('urlFor', (path, base) =>
/^(\w+:)?\/\//.test(path) ? path : base + path
);
hexo.extend.helper.register(
'toDataURI',
(data, type = '') =>
`data:${type};base64,${Buffer.from(data).toString('base64')}`
);
hexo.extend.helper.register(
'eventOf',
(title, description, start, end, location) => {
const event = new VEvent();
event.setSummary(title);
event.setDescription(description);
event.setDate(new Date(start), new Date(end));
event.setLocation(location);
return event;
}
);