Skip to content

Commit

Permalink
feat: add language default from langs & redirect path check to exist
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Apr 26, 2024
1 parent 8057b0f commit bc5d971
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const getFileProps = async (options: ResolverOptions) => {
const tocBaseLang = tocBase?.split('/')[0];
const tocLang = configLangs?.includes(tocBaseLang as Lang) && tocBaseLang;

const lang = tocLang || configLang || Lang.RU;
const lang = tocLang || configLang || configLangs?.[0] || Lang.RU;
const langs = configLangs?.length ? configLangs : [lang];

const props = {
Expand Down
10 changes: 7 additions & 3 deletions src/steps/processPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,16 @@ async function saveSinglePages() {
}

function saveRedirectPage(outputDir: string): void {
const {lang} = ArgvService.getConfig();
const {lang, langs} = ArgvService.getConfig();

const redirectLang = lang || langs?.[0] || Lang.RU;
const redirectLangRelativePath = `./${redirectLang}/index.html`;

const redirectPagePath = join(outputDir, 'index.html');
const redirectLangPath = join(outputDir, redirectLangRelativePath);

if (!existsSync(redirectPagePath)) {
const content = generateStaticRedirect(lang || Lang.RU);
if (!existsSync(redirectPagePath) && existsSync(redirectLangPath)) {
const content = generateStaticRedirect(redirectLang, redirectLangRelativePath);
writeFileSync(redirectPagePath, content);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {PluginService} from '../services';
import {join} from 'path';
import manifest from '@diplodoc/client/manifest';

export function generateStaticRedirect(lang: Lang): string {
export function generateStaticRedirect(lang: Lang, link: 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}/index.html">
<meta http-equiv="refresh" content="0; url=${link}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redirect</title>
<style type="text/css">
Expand All @@ -27,11 +27,11 @@ export function generateStaticRedirect(lang: Lang): string {
.join('\n')}
${PluginService.getHeadContent()}
<script type="text/javascript">
window.location.replace("./${lang}/index.html");
window.location.replace("${link}");
</script>
</head>
<body class="g-root g-root_theme_light">
If you are not redirected automatically, follow this <a href="./${lang}/index.html">link to example</a>.
If you are not redirected automatically, follow this <a href="${link}">link to example</a>.
</body>
</html>
`;
Expand Down
34 changes: 17 additions & 17 deletions tests/e2e/__snapshots__/rtl.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ exports[`Generate html document with correct lang and dir attributes. Load corre

exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with only one rtl lang 2`] = `
<!DOCTYPE html>
<html lang="ru"
dir="ltr"
<html lang="he"
dir="rtl"
>
<head>
<meta charset="utf-8">
Expand All @@ -23,19 +23,19 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</style>
<link type="text/css"
rel="stylesheet"
href="_bundle/vendor.css"
href="_bundle/vendor.rtl.css"
>
<link type="text/css"
rel="stylesheet"
href="_bundle/app.css"
href="_bundle/app.rtl.css"
>
</head>
<body class="g-root g-root_theme_light">
<div id="root">
</div>
<script type="application/javascript">
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"leading":true,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"}],"base":"."},"data":{"title":"Documentation","links":[{"title":"Documentation","href":"page.html"}]},"meta":{"metadata":[],"style":[],"script":[]}},"router":{"pathname":"index.html"},"lang":"ru","langs":["he"]};
window.__DATA__ = {"data":{"leading":true,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"}],"base":"."},"data":{"title":"Documentation","links":[{"title":"Documentation","href":"page.html"}]},"meta":{"metadata":[],"style":[],"script":[]}},"router":{"pathname":"index.html"},"lang":"he","langs":["he"]};
</script>
<script type="application/javascript"
src="_bundle/react.js"
Expand All @@ -55,8 +55,8 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with only one rtl lang 3`] = `
<!DOCTYPE html>
<html lang="ru"
dir="ltr"
<html lang="he"
dir="rtl"
>
<head>
<meta charset="utf-8">
Expand All @@ -74,11 +74,11 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</style>
<link type="text/css"
rel="stylesheet"
href="_bundle/vendor.css"
href="_bundle/vendor.rtl.css"
>
<link type="text/css"
rel="stylesheet"
href="_bundle/app.css"
href="_bundle/app.rtl.css"
>
</head>
<body class="g-root g-root_theme_light">
Expand All @@ -88,7 +88,7 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
window.STATIC_CONTENT = false
window.__DATA__ = {"data":{"leading":false,"toc":{"title":"Documentation","href":"index.html","items":[{"name":"Documentation","href":"page.html","id":"Documentation-RANDOM"}],"base":"."},"meta":{"metadata":[],"style":[],"script":[]},"assets":[],"headings":[],"title":"","includes":[],"html":"
<p>תיעוד
</p>/n"},"router":{"pathname":"page.html"},"lang":"ru","langs":["he"]};
</p>/n"},"router":{"pathname":"page.html"},"lang":"he","langs":["he"]};
</script>
<script type="application/javascript"
src="_bundle/react.js"
Expand Down Expand Up @@ -327,13 +327,13 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
exports[`Generate html document with correct lang and dir attributes. Load correct bundles. documentation with rtl and ltr langs 6`] = `
<!DOCTYPE html>
<html lang="ru"
dir="ltr"
<html lang="ar"
dir="rtl"
>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh"
content="0; url=./ru/index.html"
content="0; url=./ar/index.html"
>
<meta name="viewport"
content="width=device-width, initial-scale=1.0"
Expand All @@ -348,19 +348,19 @@ exports[`Generate html document with correct lang and dir attributes. Load corre
</style>
<link type="text/css"
rel="stylesheet"
href="_bundle/vendor.css"
href="_bundle/vendor.rtl.css"
>
<link type="text/css"
rel="stylesheet"
href="_bundle/app.css"
href="_bundle/app.rtl.css"
>
<script type="text/javascript">
window.location.replace("./ru/index.html");
window.location.replace("./ar/index.html");
</script>
</head>
<body class="g-root g-root_theme_light">
If you are not redirected automatically, follow this
<a href="./ru/index.html">
<a href="./ar/index.html">
link to example
</a>
.
Expand Down

0 comments on commit bc5d971

Please sign in to comment.