forked from mia-platform/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateRedirects.js
30 lines (24 loc) · 953 Bytes
/
createRedirects.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
/*
WARNING: The redirect map is deprecated!!!
To add a redirect, please add it to the object in the file `301redirects.json` in the root of the project.
The format is as follows:
"/docs/the/old/link/": {
"destination": "/docs/new/link",
"addedOn": "2022-10-01"
},
addedOn is optional but recommended, please use the format YYYY-MM-DD, this is used to track when the redirect was added.
*/
const linkFile = require('./301redirects.json');
// Build JS redirect map from JSON file
let redirectPaths = Object.keys(linkFile).reduce((redirects, sourceLink) => {
const destinationLink = linkFile[sourceLink].destination;
redirects[destinationLink] = sourceLink;
return redirects;
}, {});
const createRedirects = (path) => {
const redirectPath = redirectPaths[path];
if (redirectPath) return [redirectPath];
// Redirects all url without docs to docs/../..
return [path.replace(/^\/docs/g, "")];
};
module.exports = createRedirects;