Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Jul 22, 2024
1 parent 9434e8c commit ffca31d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ import {
getActiveDocContext,
getActiveVersion,
getDocVersionSuggestions,
sortVersionsByPathDepth,
} from '../docsClientUtils';
import type {
GlobalPluginData,
GlobalVersion,
ActivePlugin,
GlobalDoc,
} from '@docusaurus/plugin-content-docs/client';
import type {VersionMetadata} from '@docusaurus/plugin-content-docs';

function createVersion(name: string, path: string, isLast: boolean) {
function createVersion(
partialVersion: Partial<VersionMetadata>,
): GlobalVersion {
return {
name,
label: name,
path,
isLast,
name: partialVersion.label ?? '???',
label: partialVersion.label ?? '???',
path: partialVersion.path ?? '???',
isLast: partialVersion.isLast ?? false,
docs: [],
mainDocId: '???',
draftIds: [],
Expand Down Expand Up @@ -203,60 +205,76 @@ describe('docsClientUtils', () => {
);
});

it('sortVersionsByPathDepth without trailing slash', () => {
const test = [
createVersion('current', '/docs', false),
createVersion('version2', '/docs/version2', true),
createVersion('version1', '/docs/version1', false),
];
it('getActiveVersion without trailing slash', () => {
const test: GlobalPluginData = {
path: 'docs',
versions: [
createVersion({label: 'current', path: '/docs', isLast: false}),
createVersion({
label: 'version2',
path: '/docs/version2',
isLast: true,
}),
createVersion({
label: 'version1',
path: '/docs/version1',
isLast: false,
}),
],
breadcrumbs: true,
};

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[0], // current
]);
expect(getActiveVersion(test, '/docs')?.name).toBe('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('getActiveVersion with trailing slash', () => {
const test: GlobalPluginData = {
path: 'docs',
versions: [
createVersion({label: 'current', path: '/docs/', isLast: false}),
createVersion({
label: 'version2',
path: '/docs/version2/',
isLast: true,
}),
createVersion({
label: 'version1',
path: '/docs/version1/',
isLast: false,
}),
],
breadcrumbs: true,
};
expect(getActiveVersion(test, '/docs')?.name).toBe('current');
});

it('sortVersionsByPathDepth docs only without trailing slash', () => {
const test = [
createVersion('current', '/', false),
createVersion('version2', '/version2', true),
createVersion('version1', '/version1', false),
];
it('getActiveVersion docs only without trailing slash', () => {
const test: GlobalPluginData = {
path: 'docs',
versions: [
createVersion({label: 'current', path: '/', isLast: false}),
createVersion({label: 'version2', path: '/version2', isLast: true}),
createVersion({label: 'version1', path: '/version1', isLast: false}),
],

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

expect(getActiveVersion(test, '/')?.name).toBe('current');
});

it('sortVersionsByPathDepth docs only with trailing slash', () => {
const test = [
createVersion('current', '/', false),
createVersion('version2', '/version2/', true),
createVersion('version1', '/version1/', false),
];
it('getActiveVersion docs only with trailing slash', () => {
const test: GlobalPluginData = {
path: 'docs',
versions: [
createVersion({label: 'current', path: '/', isLast: false}),
createVersion({label: 'version2', path: '/version2/', isLast: true}),
createVersion({label: 'version1', path: '/version1/', isLast: false}),
],
breadcrumbs: true,
};

expect(sortVersionsByPathDepth(test)).toEqual([
test[1], // version2
test[2], // version1
test[0], // current
]);
expect(getActiveVersion(test, '/')?.name).toBe('current');
});

it('getActiveDocContext', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ 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) => {
export function getActiveVersion(
data: GlobalPluginData,
pathname: string,
): GlobalVersion | undefined {
console.log('data:', data);
// Sort paths by depth, deepest first
const orderedVersionsMetadata = [...data.versions].sort((a, b) => {
if (a.path.includes(b.path)) {
return -1;
}
Expand All @@ -72,14 +75,6 @@ export function sortVersionsByPathDepth(

return 0;
});
}

export function getActiveVersion(
data: GlobalPluginData,
pathname: string,
): GlobalVersion | undefined {
// Sort paths by depth, deepest first
const orderedVersionsMetadata = sortVersionsByPathDepth(data.versions);

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

0 comments on commit ffca31d

Please sign in to comment.