Skip to content

Commit

Permalink
Sort by order field (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin committed Dec 19, 2022
1 parent 55b2bca commit c3693ca
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions utils/getManualToc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ function getManualToc(tree, pageUrl) {
return []
}

let currentChildren = tree.children
let tableOfContentArrForSet = []
let currentPageChildren

// eslint-disable-next-line no-restricted-syntax
for (const currentPageUrl of pageUrl) {
if (currentPageChildren) {
currentPageChildren =
currentPageChildren.find((obj) => obj.url === currentPageUrl)?.children || []
}

currentChildren = currentChildren?.find(
const manualSectionsRaw = tree.children?.find(
(obj) => obj?.properties?.pageUrl?.url === currentPageUrl
)?.children

const b = currentChildren?.map((obj) => ({
url: obj?.properties?.pageUrl?.url,
title: obj?.properties?.Name?.title[0]?.text?.content,
children: [],
}))
const manualSections = manualSectionsRaw
?.map((obj) => ({
url: obj?.properties?.pageUrl?.url,
order: obj?.properties?.order?.number,
title: obj?.properties?.Name?.title[0]?.text?.content,
children: [],
}))
// BUG Remove `order` field and sorting after https://github.com/ekaterinburgdev/guides-cache-service/issues/13
?.sort(({ order: orderA }, { order: orderB }) => orderA - orderB)

if (!currentPageChildren) {
tableOfContentArrForSet = b
tableOfContentArrForSet = manualSections
currentPageChildren = tableOfContentArrForSet
} else {
currentPageChildren = b
currentPageChildren = manualSections
}
}
return tableOfContentArrForSet || []
Expand Down

0 comments on commit c3693ca

Please sign in to comment.