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 1 commit
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
Prev Previous commit
Next Next commit
add tests and refactor logic
  • Loading branch information
OzakIOne committed Jul 22, 2024
commit 2ac906d6a82e76461716b7095dd9793ef5aab9a5
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
]);
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,20 +59,37 @@ export function getActivePlugin(
export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
data.versions.find((version) => version.isLast)!;

export function sortVersionsByPathDepth(
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);
});
}

export function getActiveVersion(
data: GlobalPluginData,
pathname: string,
): GlobalVersion | undefined {
// Sort versions: non-root paths first (by length, descending), then root path
const orderedVersionsMetadata = [...data.versions].sort((a, b) => {
if (a.path === `${data.path}/`) {
return 1;
}
if (b.path === `${data.path}/`) {
return -1;
}
return b.path.length - a.path.length;
});
// Sort paths by depth, deepest first
const orderedVersionsMetadata = sortVersionsByPathDepth(data.versions);

return orderedVersionsMetadata.find(
(version) =>
Expand Down