-
Notifications
You must be signed in to change notification settings - Fork 15
/
recommend.js
64 lines (53 loc) · 1.74 KB
/
recommend.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
module.exports = function recommend(recommended_posts, total, post, config) {
var posts = filterPosts(recommended_posts, post, config.excludePattern)
if (config.fixedNumber || posts.length === 0 ) {
return recommendOffline(total, post.prev, post.next, posts);
} else {
return posts
}
}
function filterPosts(recommended_posts, post, excludePattern) {
if (recommended_posts === undefined ||
recommended_posts[post.permalink] === undefined) {
return [];
}
if (excludePattern === undefined || excludePattern.length === 0) {
return recommended_posts[post.permalink]
}
var res_posts = [];
recommended_posts[post.permalink].forEach( function(p) {
var pass = true;
if (excludePattern) {
for (var i = 0; i < excludePattern.length; i++) {
var re = new RegExp(excludePattern[i])
if (re.test(p.permalink)) {
pass = false;
break;
}
}
}
if (pass) {
res_posts.push(p)
}
});
return res_posts;
}
function recommendOffline(total, left, right, posts) {
if (posts.length == total) return posts;
if (right != undefined) {
posts.push(right);
if (posts.length == total) return posts;
}
if (left != undefined) {
posts.unshift(left);
}
if (left === undefined && right === undefined)
return posts;
else if (left === undefined) {
return recommendOffline(total, left, right.next, posts);
} else if (right === undefined) {
return recommendOffline(total, left.prev, right, posts);
} else {
return recommendOffline(total, left.prev, right.next, posts);
}
}