fix(v2): i18n should not crash theme without footer (#3940)

* Fix theme translations when no footer

* fix TS issues
This commit is contained in:
Sébastien Lorber 2020-12-18 18:02:23 +01:00 committed by GitHub
parent 3fc29f4b14
commit ef49c2be72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 7 deletions

View file

@ -87,3 +87,24 @@ describe('translateThemeConfig', () => {
).toMatchSnapshot();
});
});
describe('getTranslationFiles and translateThemeConfig isomorphism', () => {
function verifyIsomorphism(themeConfig: ThemeConfig) {
const translationFiles = getTranslationFiles({themeConfig});
const translatedThemeConfig = translateThemeConfig({
themeConfig,
translationFiles,
});
expect(translatedThemeConfig).toEqual(themeConfig);
}
test('should be verified for main sample', () => {
verifyIsomorphism(ThemeConfigSample);
});
// undefined footer should not make the translation code crash
// See https://github.com/facebook/docusaurus/issues/3936
test('should be verified for sample without footer', () => {
verifyIsomorphism({...ThemeConfigSample, footer: undefined});
});
});

View file

@ -129,10 +129,17 @@ export function getTranslationFiles({
}: {
themeConfig: ThemeConfig;
}): TranslationFile[] {
return [
const translationFiles: (TranslationFile | undefined)[] = [
{path: 'navbar', content: getNavbarTranslationFile(themeConfig.navbar)},
{path: 'footer', content: getFooterTranslationFile(themeConfig.footer)},
themeConfig.footer
? {
path: 'footer',
content: getFooterTranslationFile(themeConfig.footer),
}
: undefined,
];
return translationFiles.filter(Boolean) as TranslationFile[];
}
export function translateThemeConfig({
@ -153,9 +160,8 @@ export function translateThemeConfig({
themeConfig.navbar,
translationFilesMap.navbar.content,
),
footer: translateFooter(
themeConfig.footer,
translationFilesMap.footer.content,
),
footer: themeConfig.footer
? translateFooter(themeConfig.footer, translationFilesMap.footer.content)
: undefined,
};
}

View file

@ -66,7 +66,7 @@ export type ThemeConfig = {
colorMode: any;
announcementBar: any;
prism: any;
footer: Footer;
footer: Footer | undefined;
hideableSidebar: any;
};