-
Notifications
You must be signed in to change notification settings - Fork 503
/
index.js
36 lines (31 loc) · 890 Bytes
/
index.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
const sanitizeFilename = require("sanitize-filename");
function slugToFolder(slug, joiner = "/") {
return (
slug
// We have slugs with these special characters that would be
// removed by the sanitizeFilename() function. What might then
// happen is that it leads to two *different slugs* becoming
// *same* folder name.
.replace(/\*/g, "_star_")
.replace(/::/g, "_doublecolon_")
.replace(/:/g, "_colon_")
.replace(/\?/g, "_question_")
.toLowerCase()
.split("/")
.map(sanitizeFilename)
.join(joiner)
);
}
function decodePath(path) {
const decoded = path.split("/").map(decodeURIComponent).join("/");
return decoded;
}
function encodePath(path) {
const decoded = path.split("/").map(encodeURIComponent).join("/");
return decoded;
}
module.exports = {
slugToFolder,
decodePath,
encodePath,
};