Skip to content

Commit

Permalink
Refactor loadThemeAlias duplicate code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Sep 26, 2020
1 parent 4a060fa commit 4a21e1f
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/docusaurus/src/server/themes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@
import {ThemeAlias} from '@docusaurus/types';
import themeAlias from './alias';

function buildThemeAliases(
themeAliases: ThemeAlias,
aliases: ThemeAlias = {},
): ThemeAlias {
Object.keys(themeAliases).forEach((aliasKey) => {
if (aliasKey in aliases) {
const componentName = aliasKey.substring(aliasKey.indexOf('/') + 1);
// eslint-disable-next-line no-param-reassign
aliases[`@theme-init/${componentName}`] = aliases[aliasKey];
}
// eslint-disable-next-line no-param-reassign
aliases[aliasKey] = themeAliases[aliasKey];
});
return aliases;
}

export default function loadThemeAlias(
themePaths: string[],
userThemePaths: string[] = [],
): ThemeAlias {
const aliases = {};
let aliases = {};

themePaths.forEach((themePath) => {
const themeAliases = themeAlias(themePath);
Object.keys(themeAliases).forEach((aliasKey) => {
if (aliasKey in aliases) {
const componentName = aliasKey.substring(aliasKey.indexOf('/') + 1);
aliases[`@theme-init/${componentName}`] = aliases[aliasKey];
}

aliases[aliasKey] = themeAliases[aliasKey];
});
aliases = {...aliases, ...buildThemeAliases(themeAliases, aliases)};
});

userThemePaths.forEach((themePath) => {
const userThemeAliases = themeAlias(themePath, false);
Object.keys(userThemeAliases).forEach((aliasKey) => {
if (aliasKey in aliases) {
const componentName = aliasKey.substring(aliasKey.indexOf('/') + 1);
aliases[`@theme-init/${componentName}`] = aliases[aliasKey];
}
aliases[aliasKey] = userThemeAliases[aliasKey];
});
aliases = {...aliases, ...buildThemeAliases(userThemeAliases, aliases)};
});

return aliases;
Expand Down

0 comments on commit 4a21e1f

Please sign in to comment.