-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
74 lines (63 loc) · 2.07 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
'use strict';
const util = require("./util");
// prepare tags by `hexo.config.enhancer.tags` and `hexo.config.keywords`
const tags = [];
util.parseTags(hexo.config.tags, tags);
util.parseTags(hexo.config.keywords, tags);
/**
* fitler hexo's post, auto generate `title`, `date`, `abbrlink`
*
* @param log
* @param data
*/
function filterPost(log, data) {
let metadata = util.parseSource(data.source);
if (!data.title) {
data.title = metadata.title;
log.i("Generate title [%s] for post [%s]", metadata.title, data.source);
}
if (metadata.date) {
data.date = metadata.date;
log.i("Generate date [%s] for post [%s]", metadata.date, data.source);
}
if (!data.abbrlink) {
data.abbrlink = util.crc32(data.title);
log.i("Generate abbrlink [%s] for post [%s]", data.abbrlink, data.source);
}
if (metadata.categories.length) {
let categories = metadata.categories;
data.categories.forEach(item => {
if (typeof item === 'string') {
categories.push(item);
} else if (item.name) {
categories.push(item.name);
}
});
categories.reverse();
data.setCategories(categories);
log.i("Generate categories [%s] for post [%s]", categories, data.source);
}
if (tags.length) {
let matchedTags = util.matchTags(data.raw, tags);
if (matchedTags.length) {
data.tags.forEach(tag => {
if (matchedTags.indexOf(tag) < 0) {
matchedTags.push(tag.name);
}
});
data.setTags(matchedTags);
log.i("Generate tags [%s] for post [%s]", matchedTags, data.source);
}
}
if (data.source && data.source.indexOf('$') >= 0) {
data.mathjax = true;
log.i("Generate mathjax [true] for post [%s]", data.source);
}
data.toc = true;
}
hexo.extend.filter.register('before_post_render', function (data) {
if (data.layout === 'post') {
filterPost(this.log, data);
}
return data;
});