Skip to content

Commit

Permalink
hack: Enable lexicographical sorting for AIA (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jun 10, 2024
1 parent 1c06eed commit 57e6919
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export class ApiReferenceNavigationConverter {
baseSlug: string,
parentSlug: string,
idgen?: NodeIdGenerator,
lexicographic?: boolean,
) {
return new ApiReferenceNavigationConverter(
apiSection,
api,
baseSlug,
parentSlug,
idgen ?? new NodeIdGenerator(),
lexicographic,
).convert();
}

Expand All @@ -41,6 +43,7 @@ export class ApiReferenceNavigationConverter {
private baseSlug: string,
private apiDefinitionParentSlug: string,
idgen: NodeIdGenerator,
private lexicographic: boolean = false,
) {
this.apiDefinitionId = FernNavigation.ApiDefinitionId(api.id);
this.#holder = ApiDefinitionHolder.create(api);
Expand Down Expand Up @@ -229,7 +232,17 @@ export class ApiReferenceNavigationConverter {

this.#visitedSubpackages.add(subpackageId);

return this.mergeEndpointPairs(children);
const toRet = this.mergeEndpointPairs(children);

if (this.lexicographic) {
toRet.sort((a, b) => {
const aTitle = a.type === "endpointPair" ? a.nonStream.title : a.title;
const bTitle = b.type === "endpointPair" ? b.nonStream.title : b.title;
return aTitle.localeCompare(bTitle);
});
}

return toRet;
}

private convertApiNavigationItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export class NavigationConfigConverter {
private config: DocsV1Read.NavigationConfig,
private apis: Record<string, APIV1Read.ApiDefinition>,
private basePath: string | undefined,
private lexicographic?: boolean,
) {}

public static convert(
config: DocsV1Read.NavigationConfig,
apis: Record<string, APIV1Read.ApiDefinition>,
basePath: string | undefined,
lexicographic?: boolean,
): FernNavigation.RootNode {
return new NavigationConfigConverter(config, apis, basePath).convert();
return new NavigationConfigConverter(config, apis, basePath, lexicographic).convert();
}

#idgen = new NodeIdGenerator();
Expand Down Expand Up @@ -250,7 +252,14 @@ export class NavigationConfigConverter {
if (api == null) {
throw new Error(`API ${apiSection.api} not found}`);
}
return ApiReferenceNavigationConverter.convert(apiSection, api, baseSlug, parentSlug, this.#idgen);
return ApiReferenceNavigationConverter.convert(
apiSection,
api,
baseSlug,
parentSlug,
this.#idgen,
this.lexicographic,
);
},
changelog: (changelog) =>
ChangelogNavigationConverter.convert(changelog, baseSlug, parentSlug, this.#idgen),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ export function convertLoadDocsForUrlResponse(response: DocsV2Read.LoadDocsForUr
response.definition.config.navigation,
hackReorderApis(response.definition.apis, response.baseUrl.domain),
response.baseUrl.basePath,
isLexicographicSortEnabled(response.baseUrl.domain),
);
}

function isLexicographicSortEnabled(domain: string): boolean {
// HACKHACK: This is a temporary solution to enable lexicographic sorting for AIA docs.
// Vercel's edge config UI is broken right now so we can't modify it there.
return domain.toLowerCase().includes("aia.docs.buildwithfern.com");
}

function hackReorderApis(
apis: Record<string, APIV1Read.ApiDefinition>,
domain: string,
Expand Down

0 comments on commit 57e6919

Please sign in to comment.