createRedirectPageContent: use eta template + encode URI for meta refresh header + tests

This commit is contained in:
slorber 2020-05-25 16:27:36 +02:00
parent fde4933cd5
commit d1a162ec30
5 changed files with 79 additions and 20 deletions

View file

@ -13,6 +13,7 @@
"dependencies": {
"@docusaurus/types": "^2.0.0-alpha.55",
"@docusaurus/utils": "^2.0.0-alpha.55",
"eta": "^1.1.1",
"globby": "^10.0.1"
},
"peerDependencies": {

View file

@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`createRedirectPageContent should encode uri special chars 1`] = `
"<!DOCTYPE html>
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/\\">
</head>
<script>
window.location.href = 'https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/';
</script>
</html>"
`;
exports[`createRedirectPageContent should match snapshot 1`] = `
"<!DOCTYPE html>
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/\\">
</head>
<script>
window.location.href = 'https://docusaurus.io/';
</script>
</html>"
`;

View file

@ -0,0 +1,26 @@
/**
* 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.
*/
import createRedirectPageContent from '../createRedirectPageContent';
describe('createRedirectPageContent', () => {
test('should match snapshot', () => {
expect(
createRedirectPageContent({toUrl: 'https://docusaurus.io/'}),
).toMatchSnapshot();
});
test('should encode uri special chars', () => {
const result = createRedirectPageContent({
toUrl: 'https://docusaurus.io/gr/σελιδας/',
});
expect(result).toContain(
'https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/',
);
expect(result).toMatchSnapshot();
});
});

View file

@ -5,6 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
const eta = require('eta');
const redirectPageTemplate = require('./template/redirectPage.html.template');
type CreateRedirectPageOptions = {
toUrl: string;
};
@ -12,24 +15,7 @@ type CreateRedirectPageOptions = {
export default function createRedirectPageContent({
toUrl,
}: CreateRedirectPageOptions) {
return `
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=${toUrl}">
<script>
const redirectLink = '${toUrl}' + location.search + location.hash;
document.write('<link rel="canonical" href="' + redirectLink + '">');
document.write('<title>Redirecting to ' + redirectLink + '</title>');
document.write('</head>')
document.write('<body>')
document.write('If you are not redirected automatically, follow this <a href="' + redirectLink + '">link</a>.')
document.write('</body>')
setTimeout(() => {
window.location.assign(redirectLink)
})
</script>
</html>
`;
return eta.render(redirectPageTemplate.trim(), {
toUrl: encodeURI(toUrl),
});
}

View file

@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=<%= it.toUrl %>">
</head>
<script>
window.location.href = '<%= it.toUrl %>';
</script>
</html>
`;