Skip to content

Commit

Permalink
Implement version redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Electron committed Sep 30, 2023
1 parent 50975a2 commit 1d112bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
22 changes: 12 additions & 10 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand 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']);

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions src/utils/pluginConfigGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,4 +115,5 @@ function generateSwitcherConfig(pluginConfig) {
module.exports = {
generatePluginConfig,
generateSwitcherConfig,
createMainVersionRedirects,
};

0 comments on commit 1d112bb

Please sign in to comment.