fix(v2): check for docs homepage correctly (#2777)

This commit is contained in:
Alexey Pyltsyn 2020-05-20 11:18:56 +03:00 committed by GitHub
parent d94a549cfb
commit e8f50af8f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,6 +70,8 @@ export default function pluginContentDocs(
opts: Partial<PluginOptions>,
): Plugin<LoadedContent | null> {
const options = {...DEFAULT_OPTIONS, ...opts};
const homePageDocsRoutePath =
options.routeBasePath === '' ? '/' : options.routeBasePath;
if (options.admonitions) {
options.remarkPlugins = options.remarkPlugins.concat([
@ -337,16 +339,15 @@ export default function pluginContentDocs(
const genRoutes = async (
metadataItems: Metadata[],
): Promise<RouteConfig[]> => {
const routes: RouteConfig[] = [];
const versionsRegex = new RegExp(versionsNames.join('|'), 'i');
await metadataItems.forEach(async (metadataItem, i) => {
const routes = await Promise.all(
metadataItems.map(async (metadataItem, i) => {
const isDocsHomePage =
metadataItem.id.substr(metadataItem.id.indexOf('/') + 1) ===
metadataItem.id.replace(versionsRegex, '').replace(/^\//, '') ===
options.homePageId;
if (isDocsHomePage) {
const homeDocsRoutePath =
routeBasePath === '' ? '/' : routeBasePath;
const versionDocsPathPrefix =
(metadataItem?.version === versioning.latestVersion
? ''
@ -362,7 +363,7 @@ export default function pluginContentDocs(
docsBaseMetadata.isHomePage = true;
docsBaseMetadata.homePagePath = normalizeUrl([
baseUrl,
homeDocsRoutePath,
homePageDocsRoutePath,
versionDocsPathPrefix,
options.homePageId,
]);
@ -375,7 +376,7 @@ export default function pluginContentDocs(
addRoute({
path: normalizeUrl([
baseUrl,
homeDocsRoutePath,
homePageDocsRoutePath,
versionDocsPathPrefix,
]),
component: docLayoutComponent,
@ -394,21 +395,26 @@ export default function pluginContentDocs(
JSON.stringify(metadataItem, null, 2),
);
// Do not create a route for a page created specifically for docs home page.
if (metadataItem.id !== REVERSED_DOCS_HOME_PAGE_ID) {
routes.push({
return {
path: metadataItem.permalink,
component: docItemComponent,
exact: true,
modules: {
content: metadataItem.source,
},
});
}
});
};
}),
);
return routes.sort((a, b) =>
a.path > b.path ? 1 : b.path > a.path ? -1 : 0,
return (
routes
// Do not create a route for a page created specifically for docs home page.
.filter(
({path}) =>
path.substr(path.lastIndexOf('/') + 1) !==
REVERSED_DOCS_HOME_PAGE_ID,
)
.sort((a, b) => (a.path > b.path ? 1 : b.path > a.path ? -1 : 0))
);
};
@ -477,9 +483,8 @@ export default function pluginContentDocs(
},
async routesLoaded(routes) {
const normalizedHomeDocsRoutePath = `/${options.routeBasePath}`;
const homeDocsRoutes = routes.filter(
(routeConfig) => routeConfig.path === normalizedHomeDocsRoutePath,
(routeConfig) => routeConfig.path === homePageDocsRoutePath,
);
// Remove the route for docs home page if there is a page with the same path (i.e. docs).
@ -487,7 +492,7 @@ export default function pluginContentDocs(
const docsHomePageRouteIndex = routes.findIndex(
(route) =>
route.component === options.docLayoutComponent &&
route.path === normalizedHomeDocsRoutePath,
route.path === homePageDocsRoutePath,
);
delete routes[docsHomePageRouteIndex!];