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

fix(theme): docsVersionDropdown navbar item not showing the appropriate version #10309

Merged
merged 9 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getActiveDocContext,
getActiveVersion,
getDocVersionSuggestions,
sortVersionsByPathDepth,
} from '../docsClientUtils';
import type {
GlobalPluginData,
Expand Down Expand Up @@ -184,12 +185,71 @@ describe('docsClientUtils', () => {
expect(getActiveVersion(data, '/docs/someDoc')?.name).toBe('version2');

expect(getActiveVersion(data, '/docs/version1')?.name).toBe('version1');
expect(getActiveVersion(data, '/docs/version1')?.name).toBe('version1');
expect(getActiveVersion(data, '/docs/version1/')?.name).toBe('version1');
expect(getActiveVersion(data, '/docs/version1/someDoc')?.name).toBe(
'version1',
);
});

it('sortVersionsByPathDepth', () => {
function createVersion(name: string, path: string, isLast: boolean) {
return {
name,
label: name,
path,
isLast,
docs: [],
mainDocId: '???',
draftIds: [],
};
}
const test1 = [
createVersion('current', '/docs', false),
createVersion('version2', '/docs/version2', true),
createVersion('version1', '/docs/version1', false),
];
const test2 = [
createVersion('current', '/docs/', false),
createVersion('version2', '/docs/version2/', true),
createVersion('version1', '/docs/version1/', false),
];

// docs only website
const test3 = [
createVersion('current', '/', false),
createVersion('version2', '/version2', true),
createVersion('version1', '/version1', false),
];

// docs only with trailing slash
const test4 = [
createVersion('current', '/', false),
createVersion('version2', '/version2/', true),
createVersion('version1', '/version1/', false),
];

expect(sortVersionsByPathDepth(test1)).toEqual([
test1[2], // version1
test1[1], // version2
test1[0], // current
]);
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
expect(sortVersionsByPathDepth(test2)).toEqual([
test2[2], // version1
test2[1], // version2
test2[0], // current
]);
expect(sortVersionsByPathDepth(test3)).toEqual([
test3[2], // version1
test3[1], // version2
test3[0], // current
]);
expect(sortVersionsByPathDepth(test4)).toEqual([
test4[2], // version1
test4[1], // version2
test4[0], // current
]);
});

it('getActiveDocContext', () => {
const versionNext: GlobalVersion = {
name: 'next',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,38 @@ export function getActivePlugin(
export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
data.versions.find((version) => version.isLast)!;

export function sortVersionsByPathDepth(
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
versions: GlobalVersion[],
): GlobalVersion[] {
return [...versions].sort((a, b) => {
const getDepth = (path: string): number => {
if (path === '/') {
return 0;
}
const trimmedPath = path.replace(/^\/|\/$/g, '');
return trimmedPath ? trimmedPath.split('/').length : 0;
};

const depthA = getDepth(a.path);
const depthB = getDepth(b.path);

// Sort by depth (descending order)
if (depthA !== depthB) {
return depthB - depthA;
}

// else sort alphabetically
return a.path.localeCompare(b.path);
});
}
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved

export function getActiveVersion(
data: GlobalPluginData,
pathname: string,
): GlobalVersion | undefined {
const lastVersion = getLatestVersion(data);
// Last version is a route like /docs/*,
// we need to match it last or it would match /docs/version-1.0/* as well
const orderedVersionsMetadata = [
...data.versions.filter((version) => version !== lastVersion),
lastVersion,
];
// Sort paths by depth, deepest first
const orderedVersionsMetadata = sortVersionsByPathDepth(data.versions);

return orderedVersionsMetadata.find(
(version) =>
!!matchPath(pathname, {
Expand Down