-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.js
More file actions
120 lines (93 loc) · 3.18 KB
/
Copy pathgithub.js
File metadata and controls
120 lines (93 loc) · 3.18 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import atob from "atob";
import replace from "batch-replace";
export const decodeBlob = (content) => {
// b64DecodeUnicode
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(
atob(content)
.split("")
.map((c) => {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
};
export const formatContentUrls = ({
blob = "",
locale = "en",
category = "",
level = "",
content = null,
}) => {
const domainString = process.env.ROOT + "/";
const decodedContent = decodeBlob(blob);
const replacedContent = replace(/umbrella?:\/\//g)
.with(domainString)
.in(decodedContent);
const urlRegex = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
let formattedContent = replacedContent.replace(urlRegex, (url) => {
const index = url.indexOf(domainString);
if (index < 0) return url;
const startIndex = domainString.length;
const path = url.substring(startIndex, url.length);
let newUrl = `${domainString}lessons/${locale}/`;
let subpaths = path.split("/");
const category = subpaths[0];
const subcategory = subpaths[1];
if (path.indexOf(".md") == path.length - 3) {
const filename = subpaths[subpaths.length - 1];
if (path.indexOf("tools") === 0) {
const lesson = content[locale]["tools"][subcategory].content.find(
(c) => c.filename == filename
);
const sha = lesson ? lesson.sha : "";
newUrl += `tools.${subcategory}/-/${sha}`;
return newUrl;
} else {
const level = subpaths[2];
const lesson = content[locale][category][subcategory][
level
].content.find((c) => c.filename == filename);
const sha = lesson ? lesson.sha : "";
newUrl += `${category}.${subcategory}/${level}/${sha}`;
return newUrl;
}
} else {
newUrl = `/lessons/${locale}/${category}.${subcategory}`;
// If category is not Tools, check for level
if (url[1].indexOf("umbrella://tools") < 0) {
const level = subpaths[2];
if (["beginner", "advanced", "expert"].includes(level)) {
newUrl += `/${level}`;
}
}
return newUrl;
}
});
const imageUrlRegex = /\(([^\)]+)\)/gi;
formattedContent = formattedContent.replace(imageUrlRegex, (url) => {
let strings = url.split(".");
if (
["jpg)", "jpeg)", "png)", "svg)", "gif)", "bmp)"].includes(strings[1])
) {
let subpath = "";
if (category == "-") subpath = "about";
else if (category.indexOf("tools") > -1)
subpath = `${category.replace(".", "/")}`;
else subpath = `${category.replace(".", "/")}/${level}`;
return `(https://raw.githubusercontent.com/securityfirst/umbrella-content/master/en/${subpath}/${url
.replace("(", "")
.replace(")", "")})`;
}
return url;
});
formattedContent = formattedContent
.replace(/index\: [0-9]/, "")
.replace("title", "Title");
return formattedContent;
};
export const getNameFromFilenameYml = (filename) => {
return !!filename
? filename.slice(2).replace(/\.yml/, "").replace(/-/g, " ")
: "";
};