Skip to content

Commit 29e3591

Browse files
committed
Don't break old links
1 parent b756b65 commit 29e3591

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

docusaurus.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const versions = require('./versions.json');
23

34
module.exports = {
@@ -104,6 +105,9 @@ module.exports = {
104105
],
105106
},
106107
},
108+
plugins: [
109+
path.resolve(__dirname, './src/plugins/docusaurus-plugin-redirect-html')
110+
],
107111
presets: [
108112
[
109113
'@docusaurus/preset-classic',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@docusaurus/core": "^2.0.0-alpha.41",
1515
"@docusaurus/preset-classic": "^2.0.0-alpha.41",
1616
"classnames": "^2.2.6",
17+
"mkdirp": "^1.0.3",
1718
"react": "^16.8.4",
1819
"react-dom": "^16.8.4"
1920
},
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { promisify } = require('util');
2+
const path = require('path');
3+
const fs = require('fs');
4+
const mkdirp = require('mkdirp');
5+
6+
const writeFileAsync = promisify(fs.writeFile);
7+
8+
module.exports = function(context) {
9+
return {
10+
name: 'docusaurus-plugin-redirect-html',
11+
12+
postBuild({ siteConfig = {}, routesPaths = [], outDir }) {
13+
routesPaths.forEach(async routesPath => {
14+
if (routesPath === '/' || routesPath.endsWith('.html')) {
15+
return;
16+
}
17+
18+
const permalink = siteConfig.url.concat(routesPath.replace(/^\//, ''));
19+
20+
const html = `
21+
<!DOCTYPE html>
22+
<html lang="en-US">
23+
<head>
24+
<meta charset="UTF-8">
25+
<meta http-equiv="refresh" content="0; url=${routesPath}">
26+
<link rel="canonical" href="${permalink}">
27+
<title>Redirecting to ${permalink}</title>
28+
</head>
29+
<body>
30+
If you are not redirected automatically, follow <a href="${routesPath}">this link</a>.
31+
<script>
32+
<!--
33+
window.location.href = "${routesPath}";
34+
// -->
35+
</script>
36+
</body>
37+
</html>
38+
`;
39+
40+
const redirects = [
41+
`${routesPath}.html/index.html`,
42+
`${routesPath.replace('/docs/', '/docs/en/')}.html`,
43+
`${routesPath.replace('/docs/', '/docs/en/next/')}.html`,
44+
];
45+
46+
for (redirect of redirects) {
47+
const file = path.join(outDir, redirect);
48+
49+
await mkdirp(path.dirname(file));
50+
await writeFileAsync(file, html);
51+
}
52+
});
53+
},
54+
};
55+
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5636,6 +5636,11 @@ mkdirp@^0.5.1, mkdirp@~0.5.1:
56365636
dependencies:
56375637
minimist "0.0.8"
56385638

5639+
mkdirp@^1.0.3:
5640+
version "1.0.3"
5641+
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.3.tgz#4cf2e30ad45959dddea53ad97d518b6c8205e1ea"
5642+
integrity sha512-6uCP4Qc0sWsgMLy1EOqqS/3rjDHOEnsStVr/4vtAIK2Y5i2kA7lFFejYrpIyiN9w0pYf4ckeCYT9f1r1P9KX5g==
5643+
56395644
move-concurrently@^1.0.1:
56405645
version "1.0.1"
56415646
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"

0 commit comments

Comments
 (0)