From 1d112bbb0dfcb6f15d08bfef136e535dc65292cc Mon Sep 17 00:00:00 2001 From: Dr-Electron Date: Sat, 30 Sep 2023 22:47:27 +0200 Subject: [PATCH] Implement version redirects --- docusaurus.config.js | 22 ++++++++++++---------- src/utils/pluginConfigGenerators.js | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 4e73944ad22..e325f58c523 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -5,6 +5,11 @@ 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 +18,10 @@ 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 +290,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', @@ -375,16 +387,6 @@ module.exports = async () => { }, ]; - // Version redirects are only used to asign path with the actual version it to the "current" version - const versionRedirects = [ - { - from: '/identity.rs/0.6', - to: '/identity.rs', - }, - ]; - - redirects.push(...versionRedirects); - for (const redirect of redirects) { if (existingPath.includes(redirect.to)) { return existingPath.replace(redirect.to, redirect.from); diff --git a/src/utils/pluginConfigGenerators.js b/src/utils/pluginConfigGenerators.js index 44b4a6303ad..7321533ee3b 100644 --- a/src/utils/pluginConfigGenerators.js +++ b/src/utils/pluginConfigGenerators.js @@ -62,6 +62,32 @@ 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 +115,5 @@ function generateSwitcherConfig(pluginConfig) { module.exports = { generatePluginConfig, generateSwitcherConfig, + createMainVersionRedirects, };