Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change redirect logic #85

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions server/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,42 @@ export const getRedirects = () => {

return result.map((redirect) => {
// If a page is an index page for a section, it has the same name as the
// containing subdirectory. If a redirect destination is a menu page,
// rewrite it to point to the containing URL path so Docusaurus recognizes
// it as a valid redirect.
const pathSegs = redirect.destination
// containing subdirectory. Handle this logic to accommodate redirects
// from the previous docs site.
const sourceSegs = redirect.source
.replaceAll(new RegExp("/$", "g"), "")
.split("/");
if (
pathSegs.length >= 2 &&
pathSegs[pathSegs.length - 1] == pathSegs[pathSegs.length - 2]
) {
redirect.destination =
pathSegs.slice(0, pathSegs.length - 2).join("/") + "/";

const destSegs = redirect.destination
.replaceAll(new RegExp("/$", "g"), "")
.split("/");

// The destination is an index page, since the slug matches the containing
// path segment.
const destIndex =
destSegs.length >= 2 &&
destSegs[destSegs.length - 1] == destSegs[destSegs.length - 2];

if (!destIndex) {
return {
from: redirect.source,
to: redirect.destination,
};
}

const docusaurusDest =
destSegs.slice(0, -1).join("/") + "/";

// This redirect maps a Docusaurus-style index page to a previous-site index
// page, so reverse the mapping.
if (redirect.source == docusaurusDest) {
const oldSource = redirect.source;
redirect.source = redirect.destination;
redirect.destination = oldSource;
// This redirect has an old-style index page path as a destination, so
// change it to a Docusaurus-style one.
} else {
redirect.destination = docusaurusDest;
}

return {
Expand Down
Loading