forked from freeCodeCamp/search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormaliser.js
58 lines (51 loc) · 1.26 KB
/
normaliser.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
const articleMetaRE = /^\-\-\-[\w\W]+?\-\-\-/;
const mdImageRE = /!\[[\w\W]*?\]\([\w\W]+?\)/;
const titleRE = /^#[^\n]*$/gm;
const htmlTagsRE = /(<([^>]+)>)/gi;
function formatExMdownFile(description) {
return description
.replace(articleMetaRE, '')
.replace(htmlTagsRE, '')
.replace(mdImageRE, '')
.replace(titleRE, '');
}
function challengeNormaliser(doc) {
return {
...doc,
friendlySearchString: doc.description.replace(htmlTagsRE, '')
};
}
function newsNormaliser(doc) {
return {
...doc,
friendlySearchString: formatExMdownFile(doc.content),
title: doc.data.title
};
}
function guideNormaliser(doc) {
return {
...doc,
friendlySearchString: formatExMdownFile(doc.body),
url: `https://guide.freecodecamp.org${doc.url}`
};
}
function youtubeNormaliser(doc) {
return {
...doc,
friendlySearchString: doc.description
};
}
const normaliserMap = {
challenge: challengeNormaliser,
news: newsNormaliser,
guides: guideNormaliser,
youtube: youtubeNormaliser
};
exports.normaliser = function normaliser(index, docs) {
return docs.map(doc => {
if (!(index in normaliserMap)) {
throw new Error('No normalising function found for %s', index);
}
return normaliserMap[index](doc);
});
};