Skip to content

Commit

Permalink
feat: add static redirect page
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Apr 1, 2024
1 parent 0d3293c commit f0a9a8d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
SINGLE_PAGE_DATA_FILENAME,
SINGLE_PAGE_FILENAME,
} from '../constants';
import { generateStaticRedirect } from '../utils/redirect';

const singlePageResults: Record<string, SinglePageResult[]> = {};
const singlePagePaths: Record<string, Set<string>> = {};
Expand Down Expand Up @@ -84,6 +85,11 @@ export async function processPages(outputBundlePath: string): Promise<void> {

if (singlePage) {
await saveSinglePages(outputBundlePath);
} else {
saveRedirectPage({
outputBundlePath,
outputDir: outputFolderPath,
});
}
}

Expand Down Expand Up @@ -180,6 +186,24 @@ async function saveSinglePages(outputBundlePath: string) {
}
}

function saveRedirectPage(pathData: {
outputBundlePath: string;
outputDir: string;
}): void {
const {
output: outputFolderPath,
lang,
} = ArgvService.getConfig();

const {outputBundlePath, outputDir} = pathData;

const relativeOutputBundlePath = relative(outputFolderPath, outputBundlePath);
const redirectPagePath = join(outputDir, "index.html");
const content = generateStaticRedirect(lang || Lang.RU, relativeOutputBundlePath);

writeFileSync(redirectPagePath, content);
}

function savePageResultForSinglePage(pageProps: DocInnerProps, pathData: PathData): void {
const {pathToFile, outputTocDir} = pathData;

Expand Down
41 changes: 41 additions & 0 deletions src/utils/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Lang, RTL_LANGS} from '../constants';
import {PluginService} from '../services';

import {join} from 'path';
import manifest from '@diplodoc/client/manifest';

export function generateStaticRedirect(
lang: Lang,
pathToBundle: string,
): string {
const isRTL = RTL_LANGS.includes(lang);

return `
<!DOCTYPE html>
<html lang="${lang}" dir="${isRTL ? 'rtl' : 'ltr'}">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=./${lang}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redirect</title>
<style type="text/css">
body {
height: 100vh;
}
</style>
${manifest.css
.filter((file: string) => isRTL === file.includes('.rtl.css'))
.map((target: string) => join(pathToBundle, target))
.map((src: string) => `<link type="text/css" rel="stylesheet" href="${src}" />`)
.join('\n')}
${PluginService.getHeadContent()}
<script type="text/javascript">
window.location.href = "./${lang}";
</script>
</head>
<body class="g-root g-root_theme_light">
If you are not redirected automatically, follow this <a href='./${lang}'>link to example</a>.
</body>
</html>
`;
}

0 comments on commit f0a9a8d

Please sign in to comment.