forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal-redirects.js
40 lines (36 loc) · 1.09 KB
/
external-redirects.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
const path = require("path");
const template = require("./utils/redirect-template");
const fs = require("fs");
function getDestinationPath(outDir, from) {
if (from.endsWith(".html")) {
// create single html file
return path.join(outDir, from);
} else {
// create index.html file under the folder
return path.join(outDir, from, "index.html");
}
}
/*
This plugin creates a HTML file in the build directory for each redirect.
The original docusaurus-client-redirects does not support external redirects or redirects from URLS pointing to .html files.
*/
module.exports = function (pluginOptions) {
const { redirects } = pluginOptions;
return function (config) {
return {
name: "external-redirects",
async postBuild() {
for (const r of redirects) {
let destinationPath = getDestinationPath(config.outDir, r.from);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(
destinationPath,
template({
to: r.to,
})
);
}
},
};
};
};