-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper-functions.js
80 lines (72 loc) · 2.62 KB
/
helper-functions.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
const { ALGOLIA_ID, ALGOLIA_ADMIN_KEY } = process.env;
import algolia from 'algoliasearch';
const algoliaApp = algolia(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);
const formatPost = (post) => {
const { id, title, primary_author, tags, url, feature_image, published_at } =
post;
const currProfileImg = primary_author.profile_image;
const profileImageUrl =
currProfileImg && currProfileImg.includes('//www.gravatar.com/avatar/')
? `https:${currProfileImg}`
: currProfileImg;
const algoliaFilterRegex = [/java\b/i];
return {
objectID: id,
title: title,
author: {
name: primary_author.name,
url: primary_author.url,
profileImage: profileImageUrl
},
tags: tags.map((obj) => {
return {
name: obj.name,
url: obj.url
};
}),
url: url,
featureImage: feature_image,
publishedAt: published_at,
publishedAtTimestamp: (new Date(published_at).getTime() / 1000) | 0,
filterTerms: algoliaFilterRegex.reduce((acc, regex) => {
const isMatch = title.match(regex);
if (isMatch) acc.push(isMatch[0].toLowerCase());
return acc;
}, [])
};
};
const indexNameMap = {
'http://localhost:2368/': 'news-dev',
'https://www.freecodecamp.dev/news/': 'news-dev',
'https://www.freecodecamp.org/news/': 'news',
'https://www.freecodecamp.org/espanol/news/': 'news-es',
'https://chinese.freecodecamp.org/news/': 'news-zh',
'https://www.freecodecamp.org/portuguese/news/': 'news-pt-br',
'https://www.freecodecamp.org/italian/news/': 'news-it',
'https://www.freecodecamp.org/ukrainian/news/': 'news-uk',
'https://www.freecodecamp.org/japanese/news/': 'news-ja',
'https://www.freecodecamp.org/arabic/news/': 'news-ar',
'https://www.freecodecamp.org/bengali/news/': 'news-bn',
'https://www.freecodecamp.org/urdu/news/': 'news-ur',
'https://www.freecodecamp.org/swahili/news': 'news-sw',
'https://www.freecodecamp.org/korean/news': 'news-ko',
'https://www.freecodecamp.org/turkish/news': 'news-tr',
'https://www.freecodecamp.org/french/news': 'news-fr',
'https://www.freecodecamp.org/hindi/news': 'news-hi',
'https://www.freecodecamp.org/german/news': 'news-de',
'https://www.freecodecamp.org/vietnamese/news': 'news-vi',
'https://www.freecodecamp.org/indonesian': 'news-id',
'https://www.freecodecamp.org/haitian/news': 'news-ht'
};
const setIndex = (url) => {
const paths = Object.keys(indexNameMap);
let indexStr;
paths.forEach((path) => {
if (url.startsWith(path)) {
indexStr = indexNameMap[path];
return;
}
});
return indexStr ? algoliaApp.initIndex(indexStr) : null;
};
export { formatPost, setIndex };