Generates a file that redirects from /docs/file.html to /docs/en/folder.html when languages are enabled (languages.js is present) (#137)

This commit is contained in:
Eric Nakagawa 2017-10-18 12:56:14 -07:00 committed by Joel Marcey
parent bcba05ae03
commit 967970eaa5
3 changed files with 104 additions and 5 deletions

View file

@ -18,7 +18,7 @@ class Head extends React.Component {
let sourceCodeButton = this.props.config.sourceCodeButton;
// defaults to github, but other values may be allowed in the future
let includeGithubButton =
sourceCodeButton === 'github' || sourceCodeButton == null;
sourceCodeButton === "github" || sourceCodeButton == null;
return (
<head>
<meta charSet="utf-8" />
@ -36,6 +36,12 @@ class Head extends React.Component {
content={this.props.config.baseUrl + this.props.config.ogImage}
/>
)}
{this.props.redirect && (
<meta
http-equiv="refresh"
content={"2; URL=" + this.props.redirect}
/>
)}
<link
rel="shortcut icon"
href={this.props.config.baseUrl + this.props.config.favicon}
@ -66,10 +72,13 @@ class Head extends React.Component {
title={this.props.config.title + " Blog RSS Feed"}
/>
)}
{includeGithubButton &&
{includeGithubButton && (
<script async defer src="https://buttons.github.io/buttons.js" />
}
<script type="text/javascript" src={this.props.config.baseUrl + "js/webplayer.js"} />
)}
<script
type="text/javascript"
src={this.props.config.baseUrl + "js/webplayer.js"}
/>
<script type="text/javascript" src="https://snack.expo.io/embed.js" />
</head>
);

68
lib/core/Redirect.js Normal file
View file

@ -0,0 +1,68 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const React = require("react");
const fs = require("fs");
const Head = require("./Head.js");
const translation = require("../server/translation.js");
const CWD = process.cwd();
// Component used to provide same head, header, footer, other scripts to all pages
class Redirect extends React.Component {
render() {
const tagline = translation[this.props.language]
? translation[this.props.language]["localized-strings"].tagline
: this.props.config.tagline;
const title = this.props.title
? this.props.title + " · " + this.props.config.title
: (!this.props.config.disableTitleTagline &&
this.props.config.title + " · " + tagline) ||
this.props.config.title;
const description = this.props.description || tagline;
const url =
this.props.config.url +
this.props.config.baseUrl +
(this.props.url || "index.html");
let latestVersion;
const redirect = this.props.redirect || false;
if (fs.existsSync(CWD + "/versions.json")) {
latestVersion = require(CWD + "/versions.json")[0];
}
return (
<html>
<Head
config={this.props.config}
description={description}
title={title}
url={url}
redirect={redirect}
/>
<body className={this.props.className}>
The page you are looking for has moved. If you are not redirected to
the new page in three seconds, then you can navigate to the new page
by using <a href={this.props.redirect}>this link</a>.
<script
dangerouslySetInnerHTML={{
__html: `
<!--
window.setTimeout(
function(){window.location.href = "${this.props
.redirect}"},3000
);
// -->
`
}}
/>
</body>{" "}
</html>
);
}
}
module.exports = Redirect;

View file

@ -111,6 +111,7 @@ function execute() {
});
const DocsLayout = require("../core/DocsLayout.js");
const Redirect = require("../core/Redirect.js");
fs.removeSync(CWD + "/build");
@ -192,8 +193,29 @@ function execute() {
const str = renderToStaticMarkup(docComp);
const targetFile =
CWD + "/build/" + siteConfig.projectName + "/" + metadata.permalink;
writeFileAndCreateFolder(targetFile, str);
// generate english page redirects when languages are enabled
if (ENABLE_TRANSLATION) {
const redirectComp = (
<Redirect
metadata={metadata}
language={language}
config={siteConfig}
redirect={"/" + metadata.permalink}
/>
);
const redirectStr = renderToStaticMarkup(redirectComp);
// create a redirects page for doc files
const redirectFile =
CWD +
"/build/" +
siteConfig.projectName +
"/" +
metadata.permalink.replace("docs/en", "docs");
writeFileAndCreateFolder(redirectFile, redirectStr);
}
});
// copy docs assets if they exist