mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 05:12:31 +02:00
stable collectRedirects
This commit is contained in:
parent
9579ac10fc
commit
d8c163e76b
1 changed files with 38 additions and 4 deletions
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {flatten} from 'lodash';
|
import {flatten, uniqBy} from 'lodash';
|
||||||
import {
|
import {
|
||||||
RedirectsCreator,
|
RedirectsCreator,
|
||||||
PluginContext,
|
PluginContext,
|
||||||
|
@ -23,6 +23,26 @@ import {
|
||||||
export default function collectRedirects(
|
export default function collectRedirects(
|
||||||
pluginContext: PluginContext,
|
pluginContext: PluginContext,
|
||||||
): RedirectMetadata[] {
|
): RedirectMetadata[] {
|
||||||
|
const redirects = doCollectRedirects(pluginContext);
|
||||||
|
return filterUnwantedRedirects(redirects, pluginContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterUnwantedRedirects(
|
||||||
|
redirects: RedirectMetadata[],
|
||||||
|
pluginContext: PluginContext,
|
||||||
|
): RedirectMetadata[] {
|
||||||
|
// we don't want to create twice the same redirect
|
||||||
|
redirects = uniqBy(redirects, (redirect) => redirect.fromRoutePath);
|
||||||
|
|
||||||
|
// We don't want to override an existing route
|
||||||
|
redirects = redirects.filter(
|
||||||
|
(redirect) => !pluginContext.routesPaths.includes(redirect.fromRoutePath),
|
||||||
|
);
|
||||||
|
|
||||||
|
return redirects;
|
||||||
|
}
|
||||||
|
|
||||||
|
function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] {
|
||||||
const redirectsCreators: RedirectsCreator[] = buildRedirectCreators(
|
const redirectsCreators: RedirectsCreator[] = buildRedirectCreators(
|
||||||
pluginContext.options,
|
pluginContext.options,
|
||||||
);
|
);
|
||||||
|
@ -50,7 +70,7 @@ function createRoutesPathsRedirects(
|
||||||
): RedirectMetadata[] {
|
): RedirectMetadata[] {
|
||||||
return flatten(
|
return flatten(
|
||||||
pluginContext.routesPaths.map((routePath) =>
|
pluginContext.routesPaths.map((routePath) =>
|
||||||
createRoutePathRedirects(routePath, redirectCreator, pluginContext),
|
createRoutePathRedirects(routePath, redirectCreator),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -59,20 +79,32 @@ function createRoutesPathsRedirects(
|
||||||
function createRoutePathRedirects(
|
function createRoutePathRedirects(
|
||||||
routePath: string,
|
routePath: string,
|
||||||
redirectCreator: RedirectsCreator,
|
redirectCreator: RedirectsCreator,
|
||||||
{siteConfig, outDir}: PluginContext,
|
|
||||||
): RedirectMetadata[] {
|
): RedirectMetadata[] {
|
||||||
|
/*
|
||||||
// TODO do we receive absolute urls???
|
// TODO do we receive absolute urls???
|
||||||
if (!path.isAbsolute(routePath)) {
|
if (!path.isAbsolute(routePath)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
// TODO addTrailingSlash ?
|
// TODO addTrailingSlash ?
|
||||||
const toUrl = addTrailingSlash(`${siteConfig.url}${routePath}`);
|
const toUrl = addTrailingSlash(`${baseUrl}${routePath}`);
|
||||||
|
|
||||||
const redirectPageContent = createRedirectPageContent({toUrl});
|
const redirectPageContent = createRedirectPageContent({toUrl});
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
const fromRoutePaths: string[] = redirectCreator(routePath) ?? [];
|
const fromRoutePaths: string[] = redirectCreator(routePath) ?? [];
|
||||||
|
|
||||||
|
return fromRoutePaths.map((fromRoutePath) => {
|
||||||
|
return {
|
||||||
|
fromRoutePath,
|
||||||
|
toRoutePath: routePath,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
/*
|
||||||
return fromRoutePaths.map((fromRoutePath) => {
|
return fromRoutePaths.map((fromRoutePath) => {
|
||||||
const redirectAbsoluteFilePath = path.join(
|
const redirectAbsoluteFilePath = path.join(
|
||||||
outDir,
|
outDir,
|
||||||
|
@ -86,4 +118,6 @@ function createRoutePathRedirects(
|
||||||
redirectAbsoluteFilePath,
|
redirectAbsoluteFilePath,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue