Skip to content

Commit

Permalink
Add version redirects to current default version
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Electron authored Oct 4, 2023
1 parent ab45f6d commit cccc0cc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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,
};

0 comments on commit cccc0cc

Please sign in to comment.