forked from diff001a/diff001a-gatsby-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-related-post.js
68 lines (67 loc) · 1.62 KB
/
gatsby-related-post.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
exports.extractRelatedPosts = extractRelatedPosts
exports.defaultConfig = {
keywords: 100,
tags: 50,
threshold: 50,
number: 5,
}
function extractRelatedPosts(posts, post, config) {
const tags = post.node.frontmatter.tags
const keywords = post.node.frontmatter.keywords
const temp_arr = []
posts.map(e => {
let score = 0
const frontmatter = e.node.frontmatter
const temp_keywords = frontmatter.keywords
const temp_tags = frontmatter.tags
// 自分を除外 //
if (post.node.frontmatter.title === frontmatter.title) {
return
}
// keywords //
if (temp_keywords === keywords) {
score += config.keywords
}
// tags //
temp_tags.map(e => {
tags.map(f => {
if (e === f) {
score += config.tags
}
})
})
// スコアがthresholdを下回る記事を除外 //
if (score < config.threshold) {
return
}
const temp = {
title: frontmatter.title,
slug: frontmatter.slug,
date: frontmatter.date,
score: score,
}
temp_arr.push(temp)
})
// scoreとdateをもとに並び替え //
temp_arr.sort(function(a, b) {
const temp_date_a = new Date(a.date).getTime()
const temp_date_b = new Date(b.date).getTime()
// 第一ソートキーは関連性 //
if (a.score > b.score) {
return -1
}
if (a.score < b.score) {
return 1
}
// 第二ソートキーは日付 //
if (temp_date_a > temp_date_b) {
return -1
}
if (temp_date_a < temp_date_b) {
return 1
}
return 0
})
const arr = temp_arr.slice(0, config.number)
return arr
}