Skip to content

Commit 8efbb7a

Browse files
authored
[docs] simplify main documentation page component (expo#20503)
1 parent fb57090 commit 8efbb7a

File tree

9 files changed

+221
-274
lines changed

9 files changed

+221
-274
lines changed

docs/common/routes.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { isReferencePath } from '~/common/routes';
2+
3+
describe(isReferencePath, () => {
4+
it('returns true for unversioned pathname', () => {
5+
expect(isReferencePath('/versions/unversioned')).toBe(true);
6+
});
7+
8+
it('returns true for sdk pathname', () => {
9+
expect(isReferencePath('/versions/latest/sdk/notifications')).toBe(true);
10+
});
11+
12+
it('returns true for react-native pathname', () => {
13+
expect(isReferencePath('/versions/v44.0.0/react-native/stylesheet/')).toBe(true);
14+
});
15+
16+
it('returns false for non-versioned pathname', () => {
17+
expect(isReferencePath('/build-reference/how-tos/')).toBe(false);
18+
});
19+
});

docs/common/routes.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as Utilities from '~/common/utilities';
2+
import { PageApiVersionContextType } from '~/providers/page-api-version';
3+
import navigation from '~/public/static/constants/navigation.json';
4+
import { NavigationRoute } from '~/types/common';
5+
6+
export const getRoutes = (
7+
path: string,
8+
version: PageApiVersionContextType['version']
9+
): NavigationRoute[] => {
10+
if (isReferencePath(path)) {
11+
return navigation.reference[version] as NavigationRoute[];
12+
} else {
13+
return navigation[getPageSection(path)] as NavigationRoute[];
14+
}
15+
};
16+
17+
export const isArchivePath = (path: string) => {
18+
return Utilities.pathStartsWith('archive', path);
19+
};
20+
21+
export const isReferencePath = (path: string) => {
22+
return Utilities.pathStartsWith('versions', path);
23+
};
24+
25+
export const isGeneralPath = (path: string) => {
26+
return navigation.generalDirectories.some(name => Utilities.pathStartsWith(name, path));
27+
};
28+
29+
export const isFeaturePreviewPath = (path: string) => {
30+
return navigation.featurePreview.some(name => Utilities.pathStartsWith(name, path));
31+
};
32+
33+
export const isPreviewPath = (path: string) => {
34+
return navigation.previewDirectories.some(name => Utilities.pathStartsWith(name, path));
35+
};
36+
37+
export const isEasPath = (path: string) => {
38+
return navigation.easDirectories.some(name => Utilities.pathStartsWith(name, path));
39+
};
40+
41+
export const getPageSection = (path: string) => {
42+
if (isReferencePath(path)) {
43+
return 'reference';
44+
} else if (isEasPath(path)) {
45+
return 'eas';
46+
} else if (isGeneralPath(path)) {
47+
return 'general';
48+
} else if (isFeaturePreviewPath(path)) {
49+
return 'featurePreview';
50+
} else if (isPreviewPath(path)) {
51+
return 'preview';
52+
} else if (isArchivePath(path)) {
53+
return 'archive';
54+
}
55+
56+
return 'general';
57+
};

docs/common/utilities.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const generateSlug = (slugger: GithubSlugger, node: React.ReactNode, leng
3131
return slugger.slug(stringToSlug);
3232
};
3333

34+
/**
35+
* Replace the version in the pathname from the URL.
36+
*/
3437
export const replaceVersionInUrl = (url: string, replaceWith: string) => {
3538
const urlArr = url.split('/');
3639
urlArr[2] = replaceWith;
@@ -65,3 +68,7 @@ export const stripVersionFromPath = (path: string) => {
6568
}
6669
return path.replace(/\/versions\/[\w.]+/, '');
6770
};
71+
72+
export const pathStartsWith = (name: string, path: string) => {
73+
return path.startsWith(`/${name}`);
74+
};

0 commit comments

Comments
 (0)