/
index.js
198 lines (170 loc) · 5.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
const chalk = require('chalk');
const { slugize } = require('hexo-util');
const moduleName = require('./package.json').name;
const moduleConfigKey = 'easy_tags_plugin';
function init(hexo) {
const { config, log, extend } = hexo;
const moduleConfig = Object.assign({
enable: true,
tag_name_case: 0,
tag_slug_case: 1,
sort_ignore_case: true,
action: 'correct' // correct, report
}, config[moduleConfigKey]);
if (!moduleConfig.enable) {
return;
}
log.debug('=========== %s ===========', chalk.cyan(moduleName));
log.debug('config %s', chalk.magenta(JSON.stringify(moduleConfig)));
const prototype = hexo.model('Post').Document.prototype;
const setTagsFunc = prototype.setTags;
const allTags = {};
const allConflictTags = {};
let inited = false;
if (moduleConfig.sort_ignore_case) {
const Tag = hexo.model('Tag');
Tag.schema.virtual('_sort_name').get(function() {
const name = this.name;
if (!name) return;
return name.toLowerCase();
});
const listTagsHelper = extend.helper.get('list_tags');
const tagCloudHelper = extend.helper.get('tagcloud');
const orderbyOverrideHelper = func => function(tags, options) {
if (!options && (!tags || !Object.prototype.hasOwnProperty.call(tags, 'length'))) {
options = tags;
tags = this.site.tags;
}
if (!tags || !tags.length) return '';
options = options || {};
if (!options.orderby || options.orderby === 'name') {
options.orderby = '_sort_name';
}
return func.call(this, tags, options);
};
extend.helper.register('list_tags', orderbyOverrideHelper(listTagsHelper));
extend.helper.register('tagcloud', orderbyOverrideHelper(tagCloudHelper));
extend.helper.register('tag_cloud', orderbyOverrideHelper(tagCloudHelper));
}
if (moduleConfig.action === 'report') {
extend.filter.register('before_generate', () => {
scanConflictTags(hexo.model('Tag'));
if (Object.keys(allConflictTags).length > 0) {
// To get posts of tags
hexo.locals.invalidate();
printConflictTags();
// process.exit(1); // eslint-disable-line no-process-exit
throw new TypeError('Tag Name Conflicted');
}
});
} else {
// overwrite setTags method
prototype.setTags = function(tags) {
if (!inited) {
// load all tags from database
initAllTags();
}
tags = renameTags(tags);
return setTagsFunc.call(this, tags);
};
}
// load all tags from database
function initAllTags() {
renameTags(hexo.model('Tag').map(tag => tag.name));
inited = true;
}
// Corrects tag names and return non-duplicated tag names
function renameTags(tags) {
return tags.reduce((list, name) => {
const key = makeKey(name);
let newName = allTags[key];
if (!newName) {
newName = transform(name);
allTags[key] = newName;
}
// Ensure items are unique.
if (!list.includes(newName)) {
list.push(newName);
}
return list;
}, []);
}
function scanConflictTags(tags) {
tags.forEach(tag => {
const name = tag.name;
const key = makeKey(name);
const tagStored = allTags[key];
if (!tagStored) {
allTags[key] = tag;
} else if (tag._id !== tagStored._id) {
const conflictTags = allConflictTags[key] || {};
conflictTags[tagStored._id] = tagStored;
conflictTags[tag._id] = tag;
allConflictTags[key] = conflictTags;
}
});
}
function printConflictTags() {
const msg = [];
msg.push('========== Conflicted Tags ==========');
for (const key in allConflictTags) {
msg.push(` tag slug: ${chalk.magenta(key)}`);
const conflictTags = allConflictTags[key];
let firstName;
Object.values(conflictTags).sort(compareTag).forEach((tag, index) => {
if (index === 0) {
msg.push(` tag name: ${chalk.cyan(tag.name)}`);
firstName = tag.name;
} else {
msg.push(` tag name: ${chalk.cyan(tag.name)}. Rename to ${chalk.cyan(firstName)} or define another slug in 'tag_map'. (e.g. ${tag.name}: ${makeKey(tag.name)}_${index})`);
}
if (tag.posts) {
tag.posts.sort('date', 1).forEach((post, index) => {
// Show the first 3 posts.
if (index < 3) {
msg.push(` post: ${chalk.magenta(post.source)}`);
} else if (index === 3) {
msg.push(' ...');
}
});
}
});
}
log.error(msg.join('\n'));
}
function compareTag(a, b) {
if (!a.posts) {
return -1;
}
if (!b.posts) {
return 1;
}
const dateA = a.posts.sort('date', 1).first().date;
const dateB = b.posts.sort('date', 1).first().date;
return dateA._d.getTime() - dateB._d.getTime();
}
function makeKey(name) {
const map = config.tag_map || {};
if (!name) return;
if (Reflect.apply(Object.prototype.hasOwnProperty, map, [name])) {
name = String(map[name] || name);
}
return slugize(name, {transform: 1});
}
function transform(name) {
switch (moduleConfig.tag_name_case) {
case 1:
return name.toLowerCase();
case 2:
return name.toUpperCase();
default:
return name;
}
}
}
module.exports = init;
// execute from hexo plugin loader
if (typeof hexo !== 'undefined') {
init(hexo); // eslint-disable-line no-undef
}