Skip to content

Commit

Permalink
feat(umi-plugin): support README.md with locale as the root path
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Jan 13, 2020
1 parent 56365e2 commit 630b5c0
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions packages/umi-plugin-father-doc/src/routes/decorateRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ export default function decorateRoutes(
const fallbackLocalRoutes = [];
const defaultLocale = opts.locales[0]?.[0];

// fallback to readme if there has no index route
validLocales.forEach(locale => {
const localeRootPath = locale === defaultLocale ? '/' : `/${locale}`;
const localeFileAddon = locale === defaultLocale ? '' : `.${locale}`;

if (!result.some(route => route.path === localeRootPath)) {
const readmePath = path.join(paths.cwd, `README${localeFileAddon}.md`);

if (fs.existsSync(readmePath)) {
result.unshift({
path: localeRootPath,
component: `./README${localeFileAddon}.md`,
exact: true,
meta: {
locale,
title: `README${localeFileAddon}`,
},
title: `README${localeFileAddon}`,
});
}
}
});

// fallback remaining non-default-locale routes
validLocales.forEach(locale => {
const currentLocalePrefix = `/${locale}`;

Expand Down Expand Up @@ -212,22 +236,5 @@ export default function decorateRoutes(
}
});

// fallback to readme if there has no index route
if (!result.some(route => route.path === '/')) {
const readmePath = path.join(paths.cwd, 'README.md');

if (fs.existsSync(readmePath)) {
result.unshift({
path: '/',
component: './README.md',
exact: true,
meta: {
title: 'README',
},
title: 'README'
});
}
}

return result.concat(Object.values(redirects));
}

0 comments on commit 630b5c0

Please sign in to comment.