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

Add version redirects to current version #1162

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const common = require('./common/docusaurus.config');
const contentConfigs = require('./contentPlugins');
const articleRedirectsFile = require('./articleRedirects');
const switcherConfig = require('./switcherConfig');
const {
buildPluginsConfig,
maintainPluginsConfig,
} = require('./versionedConfig');
const {
createMainVersionRedirects,
} = require('./src/utils/pluginConfigGenerators');

module.exports = async () => {
const contentPlugins = await Promise.all(
Expand All @@ -13,6 +20,12 @@ module.exports = async () => {
).map(async (contentConfig) => await create_doc_plugin(contentConfig)),
);

const buildMainVersionRedirects =
createMainVersionRedirects(buildPluginsConfig);
const maintainMainVersionRedirects = createMainVersionRedirects(
maintainPluginsConfig,
);

// Get tutorials
const additionalPlugins = await glob(['tutorials']);

Expand Down Expand Up @@ -281,6 +294,9 @@ module.exports = async () => {
// directory redirects - only added for directories that didn't have a direct match
createRedirects(existingPath) {
const redirects = [
// Version redirects are only used to asign paths with the actual version to the "current" version
...buildMainVersionRedirects,
...maintainMainVersionRedirects,
{
from: '/develop/nodes/rest-api',
to: '/apis/core/v1',
Expand Down
26 changes: 26 additions & 0 deletions src/utils/pluginConfigGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ function generatePluginConfig(pluginConfig, basePath) {
});
}

/**
* Generate directs versioned links to the main version.
* @param {import('../common/components/Switcher').Doc[]} versionedConfig - An array of versioned plugin configurations.
* @returns {Array} - An array of redirects.
*/
function createMainVersionRedirects(versionedConfig) {
redirects = [];
for (const doc of versionedConfig) {
if (doc.versions.length > 1) {
// Find main version
const mainVersion = findMainVersion(doc);

// TODO: This could be removed once we don't use points in paths anymore.
const routeBasePath = doc.routeBasePath ? doc.routeBasePath : doc.id;

redirects.push({
from: '/' + routeBasePath + '/' + mainVersion.label,
to: '/' + routeBasePath,
});
}
}

return redirects;
}

/**
* Generate the switcher config from the versioned config.
* @param {import('../common/components/Switcher').Doc[]} pluginConfig
Expand Down Expand Up @@ -89,4 +114,5 @@ function generateSwitcherConfig(pluginConfig) {
module.exports = {
generatePluginConfig,
generateSwitcherConfig,
createMainVersionRedirects,
};