Skip to content

Commit

Permalink
Fix sidebar ordering for files with no explicit position
Browse files Browse the repository at this point in the history
  • Loading branch information
Sporiff committed Sep 24, 2024
1 parent 29d1e77 commit 76ed9da
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/utils/helpers/navigation/buildSidebarHierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ export const buildSidebarHierarchy = (entries: ContentCollectionEntry[]): [Langu
if ((isSpecialCase || isDirectChild) && potentialChildSlug.startsWith(entry.slug + '/') && potentialChildSlug !== entry.slug) {
// Set the parent for the child
potentialChild.parent = entry.slug;

// Insert the child in the correct position or alphabetically
if (potentialChild.position) {
structuredEntry.children?.splice(potentialChild.position - 1, 0, potentialChild,)
structuredEntry.children?.splice(potentialChild.position - 1, 0, potentialChild);
} else {
structuredEntry.children?.push(potentialChild);
}
Expand All @@ -114,6 +116,23 @@ export const buildSidebarHierarchy = (entries: ContentCollectionEntry[]): [Langu
}
});

// Sort the children alphabetically if they have no position
structuredEntry.children?.sort((a, b) => {
if (a.position && b.position) {
return 0;
}
if (a.position) {
return -1;
}
if (b.position) {
return 1;
}
// Sort alphabetically by file name (last part of the id)
const aFileName = a.id.split('/').pop()?.toLowerCase();
const bFileName = b.id.split('/').pop()?.toLowerCase();
return aFileName?.localeCompare(bFileName!);
});

// If the entry has no parent, nest it directly under the type array
if (!structuredEntry.parent) {
if (type === "sdk") {
Expand Down

0 comments on commit 76ed9da

Please sign in to comment.