mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-27 05:28:43 +02:00
feat(core): simplify plugin API, support route.props (#10042)
This commit is contained in:
parent
d1590e37ac
commit
5c1d6464d8
26 changed files with 2858 additions and 2240 deletions
89
packages/docusaurus-plugin-content-pages/src/routes.ts
Normal file
89
packages/docusaurus-plugin-content-pages/src/routes.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {aliasedSitePathToRelativePath, docuHash} from '@docusaurus/utils';
|
||||
import type {
|
||||
PluginContentLoadedActions,
|
||||
RouteConfig,
|
||||
RouteMetadata,
|
||||
} from '@docusaurus/types';
|
||||
import type {
|
||||
PluginOptions,
|
||||
Metadata,
|
||||
LoadedContent,
|
||||
MDXPageMetadata,
|
||||
} from '@docusaurus/plugin-content-pages';
|
||||
|
||||
type CreateAllRoutesParam = {
|
||||
content: LoadedContent;
|
||||
options: PluginOptions;
|
||||
actions: PluginContentLoadedActions;
|
||||
};
|
||||
|
||||
function createPageRouteMetadata(metadata: Metadata): RouteMetadata {
|
||||
const lastUpdatedAt =
|
||||
metadata.type === 'mdx' ? metadata.lastUpdatedAt : undefined;
|
||||
return {
|
||||
sourceFilePath: aliasedSitePathToRelativePath(metadata.source),
|
||||
lastUpdatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createAllRoutes(
|
||||
param: CreateAllRoutesParam,
|
||||
): Promise<void> {
|
||||
const routes = await buildAllRoutes(param);
|
||||
routes.forEach(param.actions.addRoute);
|
||||
}
|
||||
|
||||
export async function buildAllRoutes({
|
||||
content,
|
||||
actions,
|
||||
options,
|
||||
}: CreateAllRoutesParam): Promise<RouteConfig[]> {
|
||||
const {createData} = actions;
|
||||
|
||||
async function buildMDXPageRoute(
|
||||
metadata: MDXPageMetadata,
|
||||
): Promise<RouteConfig> {
|
||||
await createData(
|
||||
// Note that this created data path must be in sync with
|
||||
// metadataPath provided to mdx-loader.
|
||||
`${docuHash(metadata.source)}.json`,
|
||||
metadata,
|
||||
);
|
||||
return {
|
||||
path: metadata.permalink,
|
||||
component: options.mdxPageComponent,
|
||||
exact: true,
|
||||
metadata: createPageRouteMetadata(metadata),
|
||||
modules: {
|
||||
content: metadata.source,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function buildJSXRoute(metadata: Metadata): Promise<RouteConfig> {
|
||||
return {
|
||||
path: metadata.permalink,
|
||||
component: metadata.source,
|
||||
exact: true,
|
||||
metadata: createPageRouteMetadata(metadata),
|
||||
modules: {
|
||||
config: `@generated/docusaurus.config`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function buildPageRoute(metadata: Metadata): Promise<RouteConfig> {
|
||||
return metadata.type === 'mdx'
|
||||
? buildMDXPageRoute(metadata)
|
||||
: buildJSXRoute(metadata);
|
||||
}
|
||||
|
||||
return Promise.all(content.map(buildPageRoute));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue