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>, opts: Partial<PluginOptions>,
): Plugin<LoadedContent | null> { ): Plugin<LoadedContent | null> {
const options = {...DEFAULT_OPTIONS, ...opts}; const options = {...DEFAULT_OPTIONS, ...opts};
const homePageDocsRoutePath =
options.routeBasePath === '' ? '/' : options.routeBasePath;
if (options.admonitions) { if (options.admonitions) {
options.remarkPlugins = options.remarkPlugins.concat([ options.remarkPlugins = options.remarkPlugins.concat([
@ -337,78 +339,82 @@ export default function pluginContentDocs(
const genRoutes = async ( const genRoutes = async (
metadataItems: Metadata[], metadataItems: Metadata[],
): Promise<RouteConfig[]> => { ): Promise<RouteConfig[]> => {
const routes: RouteConfig[] = []; const versionsRegex = new RegExp(versionsNames.join('|'), 'i');
await metadataItems.forEach(async (metadataItem, i) => { const routes = await Promise.all(
const isDocsHomePage = metadataItems.map(async (metadataItem, i) => {
metadataItem.id.substr(metadataItem.id.indexOf('/') + 1) === const isDocsHomePage =
options.homePageId; metadataItem.id.replace(versionsRegex, '').replace(/^\//, '') ===
options.homePageId;
if (isDocsHomePage) { if (isDocsHomePage) {
const homeDocsRoutePath = const versionDocsPathPrefix =
routeBasePath === '' ? '/' : routeBasePath; (metadataItem?.version === versioning.latestVersion
const versionDocsPathPrefix = ? ''
(metadataItem?.version === versioning.latestVersion : metadataItem.version!) ?? '';
? ''
: metadataItem.version!) ?? '';
// To show the sidebar, get the sidebar key of available sibling item. // To show the sidebar, get the sidebar key of available sibling item.
metadataItem.sidebar = ( metadataItem.sidebar = (
metadataItems[i - 1] ?? metadataItems[i + 1] metadataItems[i - 1] ?? metadataItems[i + 1]
).sidebar; ).sidebar;
const docsBaseMetadata = createDocsBaseMetadata( const docsBaseMetadata = createDocsBaseMetadata(
metadataItem.version!, metadataItem.version!,
); );
docsBaseMetadata.isHomePage = true; docsBaseMetadata.isHomePage = true;
docsBaseMetadata.homePagePath = normalizeUrl([ docsBaseMetadata.homePagePath = normalizeUrl([
baseUrl,
homeDocsRoutePath,
versionDocsPathPrefix,
options.homePageId,
]);
const docsBaseMetadataPath = await createData(
`${docuHash(metadataItem.source)}-base.json`,
JSON.stringify(docsBaseMetadata, null, 2),
);
// Add a route for docs home page.
addRoute({
path: normalizeUrl([
baseUrl, baseUrl,
homeDocsRoutePath, homePageDocsRoutePath,
versionDocsPathPrefix, versionDocsPathPrefix,
]), options.homePageId,
component: docLayoutComponent, ]);
exact: true, const docsBaseMetadataPath = await createData(
modules: { `${docuHash(metadataItem.source)}-base.json`,
docsMetadata: aliasedSource(docsBaseMetadataPath), JSON.stringify(docsBaseMetadata, null, 2),
content: metadataItem.source, );
},
});
}
await createData( // Add a route for docs home page.
// Note that this created data path must be in sync with addRoute({
// metadataPath provided to mdx-loader. path: normalizeUrl([
`${docuHash(metadataItem.source)}.json`, baseUrl,
JSON.stringify(metadataItem, null, 2), homePageDocsRoutePath,
); versionDocsPathPrefix,
]),
component: docLayoutComponent,
exact: true,
modules: {
docsMetadata: aliasedSource(docsBaseMetadataPath),
content: metadataItem.source,
},
});
}
// Do not create a route for a page created specifically for docs home page. await createData(
if (metadataItem.id !== REVERSED_DOCS_HOME_PAGE_ID) { // Note that this created data path must be in sync with
routes.push({ // metadataPath provided to mdx-loader.
`${docuHash(metadataItem.source)}.json`,
JSON.stringify(metadataItem, null, 2),
);
return {
path: metadataItem.permalink, path: metadataItem.permalink,
component: docItemComponent, component: docItemComponent,
exact: true, exact: true,
modules: { modules: {
content: metadataItem.source, content: metadataItem.source,
}, },
}); };
} }),
}); );
return routes.sort((a, b) => return (
a.path > b.path ? 1 : b.path > a.path ? -1 : 0, 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) { async routesLoaded(routes) {
const normalizedHomeDocsRoutePath = `/${options.routeBasePath}`;
const homeDocsRoutes = routes.filter( 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). // 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( const docsHomePageRouteIndex = routes.findIndex(
(route) => (route) =>
route.component === options.docLayoutComponent && route.component === options.docLayoutComponent &&
route.path === normalizedHomeDocsRoutePath, route.path === homePageDocsRoutePath,
); );
delete routes[docsHomePageRouteIndex!]; delete routes[docsHomePageRouteIndex!];