-
Notifications
You must be signed in to change notification settings - Fork 0
/
slug.js
50 lines (46 loc) · 1.13 KB
/
slug.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
exports.preprocessSlug = function(text){
if(!text)
return text;
var newSlug;
if(typeof text === "string"){
newSlug = text.toLowerCase();
}
return newSlug.replace(/\//gi,"|")
.replace(/#/g, "-")
.replace(/@/g, "-")
.replace(/!/g, "-")
.replace(/\?/g, "-")
.replace(/$/g, "-")
.replace(/€/g, "-")
.replace(/%/g, "-")
.replace(/!/g, "-")
.replace(/\s/g,"_");
};
exports.generateSlug = function(doc, modelString, baseSlug, slug, counter, next){
var counter = counter + 1;
console.log('\nSLUGIFY - 3 : ');
console.log(arguments);
mongoose.model(modelString).find({
slug: slug
}, function (err, items) {
console.log('\nSLUGIFY - 4 : ');
console.log(arguments);
if (!err) {
if(items.length){
exports.generateSlug(doc, modelString, baseSlug, baseSlug+"_"+counter, counter, next);
}
else {
doc.slug = slug;
next();
}
}
});
};
exports.handleSlug = function(doc, modelString, slug, additionalArgs, next){
if(doc.slug)
next();
else{
var preprocessedSlug = exports.preprocessSlug(slug);
exports.generateSlug(doc, modelString, preprocessedSlug, preprocessedSlug, 1, next);
}
};