Advertisement
Guest User

Untitled

a guest
Nov 9th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Determines order of nodes for display. display_order is nullable and has
  2. // higher priority over title
  3. const compareDisplayOrder = (a, b) => {
  4.   const a_displayOrder = a.frontmatter.display_order
  5.   const b_displayOrder = b.frontmatter.display_order
  6.  
  7.   if (a_displayOrder === b_displayOrder) {
  8.     const a_title = a.frontmatter.title
  9.     const b_title = b.frontmatter.title
  10.  
  11.     if (a_title > b_title) {
  12.       return 1
  13.     }
  14.  
  15.     if (a_title < b_title) {
  16.       return -1
  17.     }
  18.   }
  19.  
  20.   if (a_displayOrder === null) {
  21.     return 1
  22.   }
  23.  
  24.   if (b_displayOrder === null) {
  25.     return -1
  26.   }
  27.  
  28.   if (a_displayOrder > b_displayOrder) {
  29.     return 1
  30.   }
  31.  
  32.   if (a_displayOrder < b_displayOrder) {
  33.     return -1
  34.   }
  35.  
  36.   return 0
  37. }
  38.  
  39. const buildNavigationTree = edges => {
  40.   const navigationTree = {}
  41.   let currentBranch
  42.  
  43.   for (const edge of edges) {
  44.     // resets to top of tree
  45.     currentBranch = navigationTree
  46.  
  47.     // create tree based off each directory segment in slug for each node
  48.     for (const slugSegment of edge.node.fields.slug.split("/")) {
  49.       if (slugSegment !== "") {
  50.         if (!currentBranch.children) {
  51.           currentBranch.children = []
  52.         }
  53.  
  54.         let currentBranchChildIndexForSlugSegment = currentBranch.children.findIndex(
  55.           child => child.slugSegment === slugSegment
  56.         )
  57.  
  58.         // if currentBranch does not have a child matching the current
  59.         // slugSegment, create a new branch
  60.         if (currentBranchChildIndexForSlugSegment === -1) {
  61.           edge.node.slugSegment = slugSegment
  62.           currentBranch.children.push(edge.node)
  63.           currentBranchChildIndexForSlugSegment = currentBranch.children.length - 1
  64.         }
  65.  
  66.         // change currentBranch to the branch we either just found or created
  67.         currentBranch = currentBranch.children[currentBranchChildIndexForSlugSegment]
  68.       }
  69.     }
  70.   }
  71.  
  72.   // remove root-level children property as it is unnecessary
  73.   return navigationTree.children
  74. }
  75.  
  76. const sortNavigationTree = navigationTree => {
  77.   navigationTree.sort(compareDisplayOrder)
  78.  
  79.   for (let i = 0; i < navigationTree.length; i++) {
  80.     // iterates through each node on current level and recursively runs for all
  81.     // children on those nodes until each node and all their children are sorted
  82.     if (navigationTree[i].children instanceof Array) {
  83.       sortNavigationTree(navigationTree[i].children)
  84.     }
  85.   }
  86.  
  87.   return navigationTree
  88. }
  89.  
  90. const data = useStaticQuery(graphql`
  91.   query {
  92.     allMarkdownRemark {
  93.       edges {
  94.         node {
  95.           id
  96.           frontmatter {
  97.             title
  98.             display_order
  99.           }
  100.           fields {
  101.             slug
  102.           }
  103.         }
  104.       }
  105.     }
  106.   }
  107. `)
  108.  
  109. let navigationTree = buildNavigationTree(data.allMarkdownRemark.edges)
  110. navigationTree = sortNavigationTree(navigationTree)
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement