Skip to content

Commit

Permalink
Sidebar.js: sort sidebar items
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWilliams2 committed Apr 11, 2024
1 parent 5c45fd6 commit b7bc6b5
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions sidebar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
const sidebarLabelOrder = {
'Getting Started': 0, // First
'Vocdoni SDK': 1,
'Vocdoni API': 2,
'UI Components': 3,
'Vocdoni Protocol': 4 // Last
}

const sdkSidebarIDOrder = {
'sdk/reference/sdk-reference': -1, // First
// All other elements are 0 by default
'sdk/reference/changelog': 1 // Last
}

// Removes autogenerated API sidebar- this is broken.
function skipAPI (items) {
return items.filter(({label}) => {
Expand All @@ -8,6 +22,15 @@ function skipAPI (items) {
})
}

function sortItems (items) {
// Sort sidebar items by label according to sidebarLabelOrder
return items.sort((i1, i2) => {
return (
(sidebarLabelOrder[i1.label] || 0) - (sidebarLabelOrder[i2.label] || 0)
)
})
}

// Adds api sidebar items from './docs/vocdoni-api/sidebar.js', filters out index file
function addAPI (items) {
// Define root api sidebar item
Expand Down Expand Up @@ -46,7 +69,7 @@ function modifySDKReference (items) {
SDKCategory.items[index2]?.type == 'category' &&
SDKCategory.items[index2]?.label == 'reference'
) {
// Modify reference category title
// Modify reference category: add link and capitalize label
referenceCategory = {
...SDKCategory.items[index2],
label: 'Reference',
Expand All @@ -55,37 +78,20 @@ function modifySDKReference (items) {
id: 'sdk/reference/sdk-reference'
}
}
let changelogIdx,
referenceIdx = 0
// Capitalize category labels
for (var index3 in referenceCategory?.items) {
// Get index of changelog file
if (
referenceCategory.items[index3].id === 'sdk/reference/changelog'
) {
changelogIdx = index3
}
// Get index of reference file
if (
referenceCategory.items[index3].id ===
'sdk/reference/sdk-reference'
) {
referenceIdx = index3
}
// Capitalize category labels
if (referenceCategory.items[index3].label != null) {
referenceCategory.items[index3].label = toTitleCase(
referenceCategory.items[index3].label
)
}
}
// Swap positions of changelog and reference links
;[
referenceCategory.items[referenceIdx],
referenceCategory.items[changelogIdx]
] = [
referenceCategory.items[changelogIdx],
referenceCategory.items[referenceIdx]
]
// Sort sidebar items by ID according to sdkSidebarIDOrder
referenceCategory.items = referenceCategory.items.sort((i1, i2) => {
return (
(sdkSidebarIDOrder[i1.id] || 0) - (sdkSidebarIDOrder[i2.id] || 0)
)
})
SDKCategory.items[index2] = referenceCategory
}
}
Expand All @@ -100,5 +106,5 @@ module.exports = async function sidebarItemsGenerator ({
...args
}) {
const sidebarItems = await defaultSidebarItemsGenerator(args)
return modifySDKReference(addAPI(skipAPI(sidebarItems)))
return modifySDKReference(addAPI(skipAPI(sortItems(sidebarItems))))
}

0 comments on commit b7bc6b5

Please sign in to comment.