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 6 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 All @@ -20,6 +21,18 @@ import type {
GlobalDoc,
} from '@docusaurus/plugin-content-docs/client';

function createVersion(name: string, path: string, isLast: boolean) {
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
return {
name,
label: name,
path,
isLast,
docs: [],
mainDocId: '???',
draftIds: [],
};
}

describe('docsClientUtils', () => {
it('getActivePlugin', () => {
const data: {[key: string]: GlobalPluginData} = {
Expand Down Expand Up @@ -184,12 +197,68 @@ 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 without trailing slash', () => {
const test = [
createVersion('current', '/docs', false),
createVersion('version2', '/docs/version2', true),
createVersion('version1', '/docs/version1', false),
];

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[0], // current
]);
});

it('sortVersionsByPathDepth with trailing slash', () => {
const test = [
createVersion('current', '/docs/', false),
createVersion('version2', '/docs/version2/', true),
createVersion('version1', '/docs/version1/', false),
];

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[0], // current
]);
});

it('sortVersionsByPathDepth docs only without trailing slash', () => {
const test = [
createVersion('current', '/', false),
createVersion('version2', '/version2', true),
createVersion('version1', '/version1', false),
];

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[0], // current
]);
});

it('sortVersionsByPathDepth docs only with trailing slash', () => {
const test = [
createVersion('current', '/', false),
createVersion('version2', '/version2/', true),
createVersion('version1', '/version1/', false),
];

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[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,28 @@ 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) => {
if (a.path.includes(b.path)) {
return -1;
}
if (b.path.includes(a.path)) {
return 1;
}

return 0;
});
}

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