From cccc0cc597d484109fd0c766299bc60d44e2de4b Mon Sep 17 00:00:00 2001 From: Dr-Electron Date: Wed, 4 Oct 2023 12:46:59 +0200 Subject: [PATCH] Add version redirects to current default version --- docusaurus.config.js | 16 ++++++++++++++++ src/utils/pluginConfigGenerators.js | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/docusaurus.config.js b/docusaurus.config.js index ce8e84d0434..bb3c22ecf7a 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -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( @@ -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']); @@ -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', diff --git a/src/utils/pluginConfigGenerators.js b/src/utils/pluginConfigGenerators.js index 44b4a6303ad..b94aad4fdc8 100644 --- a/src/utils/pluginConfigGenerators.js +++ b/src/utils/pluginConfigGenerators.js @@ -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 @@ -89,4 +114,5 @@ function generateSwitcherConfig(pluginConfig) { module.exports = { generatePluginConfig, generateSwitcherConfig, + createMainVersionRedirects, };