-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
60 lines (51 loc) · 1.86 KB
/
plugin.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
/**
* Created by Nicolas on 6/19/15.
*/
/**
* Modified by Dreadfull on 8/09/17
*/
/**
* Changed to translate from Chinese to Pinyin by Yang Zhang on 05/08/18
*/
(function (Plugin) {
'use strict';
var convert = require('convert-chinese-to-pinyin');
function slugify (text) {
var slug = text.toLowerCase();
if(slug.match(/\/(.*)/)){
var content = slug.replace(/(.+\/)(.*)$/, '$2');
content = convert(content).toLowerCase();
slug = slug.replace(/(.+\/)(.*)$/, `$1${content}`);
}
return slug;
}
//NodeBB list of Hooks: https://github.com/NodeBB/NodeBB/wiki/Hooks
Plugin.hooks = {
filters: {
categoryCreate: function (categoryData, callback) {
categoryData.category.slug = slugify(categoryData.category.slug);
callback(null, categoryData);
},
categoryUpdate: function (categoryData, callback) {
if ('slug' in categoryData.category) {
categoryData.category.slug = slugify(categoryData.category.slug);
}
callback(null, categoryData);
},
topicCreate: function (topicData, callback) {
topicData.topic.slug = slugify(topicData.topic.slug);
callback(null, topicData);
},
topicEdit: function (topicData, callback) {
//Here was a problem
topicData.topic.slug = slugify(topicData.topic.slug);
callback(null, topicData);
},
userCreate: function (userData, callback) {
//If there will be username collision, userslug will be overridden by NodeBB...
userData.userslug = slugify(userData.userslug);
callback(null, userData);
}
}
};
})(module.exports);