Skip to content

Commit

Permalink
sort by weight is back #33
Browse files Browse the repository at this point in the history
  • Loading branch information
MattReimer committed Jan 3, 2025
1 parent ee74be6 commit 8162b64
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: SubPage
banner: true
menuWeight: 1
---

Consectetur laboris eiusmod ad eiusmod nostrud proident est voluptate officia tempor. Exercitation eiusmod deserunt occaecat elit. Voluptate sunt sint veniam velit cillum magna occaecat ut cillum eiusmod pariatur ullamco ad. Excepteur culpa veniam duis nostrud Lorem duis aliquip elit adipisicing mollit consequat. Est excepteur amet cupidatat excepteur nostrud. Sunt sunt aliquip excepteur ea consequat.
Expand Down
1 change: 1 addition & 0 deletions sites/devsite/content/page/contentPages/interiorPage.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: Interior Page
menuWeight: 4
---

Culpa ipsum eiusmod labore in in nulla duis incididunt labore. Amet ullamco minim enim aliqua nostrud velit aute sunt nostrud duis enim dolore. Reprehenderit anim aute voluptate officia duis ea laborum eiusmod consectetur ex.
Expand Down
7 changes: 7 additions & 0 deletions sites/devsite/content/page/contentPages/noWeight.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: No Weight
---

Culpa ipsum eiusmod labore in in nulla duis incididunt labore. Amet ullamco minim enim aliqua nostrud velit aute sunt nostrud duis enim dolore. Reprehenderit anim aute voluptate officia duis ea laborum eiusmod consectetur ex.

Ea consectetur ex dolore est aliqua ad cupidatat dolore. Eu ad labore cupidatat anim adipisicing velit. Nostrud duis et Lorem occaecat enim exercitation ut ad. Magna nostrud ipsum esse ex enim adipisicing sunt cillum consequat. Sunt nulla exercitation est sunt.
6 changes: 5 additions & 1 deletion sites/devsite/content/page/index.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
title: Home
title: Gatsby Theme Test Site
description: "Riverscapes website"
banner: true
---

This is not really a Riverscapes site. It is a test for the Gatsby theme.

Please go to [riverscapes.net](https://riverscapes.net) to learn more about the riverscapes consortium.

This is the homepage. hello [this is an internal link to fair](./About_Us/fair).

- [LINK1](./About_Us/fair).
Expand Down
1 change: 1 addition & 0 deletions theme/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ exports.createSchemaCustomization = ({ actions }) => {
}
type MdxFrontmatter {
title: String
menuWeight: Int
description: String
blurb: String
date(formatString: String): String
Expand Down
46 changes: 31 additions & 15 deletions theme/src/components/menus/allPagesMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ const AllPagesMenu: React.FC<AllPagesMenuProps> = ({
showHeading = false,
}) => {
// Function to group entries based on slug levels and convert to new object structure
const convertToStructure = (entries) => {
return Object.keys(entries).map((key) => {
const entry = entries[key]
const newItem = {
title: entry.title || key,
url: entry.url || '',
items: [],
}
if (entry.items) {
newItem.items = convertToStructure(entry.items)
}
return newItem
})
const convertToStructure = (groupedItems) => {
const result = []

const traverse = (items, parent = null) => {
Object.keys(items).forEach((key) => {
const item = items[key]
const newItem = {
title: item.title || key,
url: item.url || '',
menuWeight: item.menuWeight || 999,
items: [],
}
if (item.items) {
traverse(item.items, newItem.items)
}
parent.push(newItem)
})
// Sort the items array first by weight and then alphabetically by title
parent.sort((a, b) => {
if (a.menuWeight !== b.menuWeight) {
return a.menuWeight - b.menuWeight
}
return a.title.localeCompare(b.title)
})
}

traverse(groupedItems, result)
return result
}

// Function to group entries based on slug levels
Expand All @@ -44,14 +59,15 @@ const AllPagesMenu: React.FC<AllPagesMenuProps> = ({

slugLevels.forEach((level, index) => {
if (!currentGroup[level]) {
currentGroup[level] = {}
currentGroup[level] = { items: [] }
}
if (index === slugLevels.length - 1) {
currentGroup[level].url = entry.fields.slug || ''
currentGroup[level].title = entry.frontmatter.title
currentGroup[level].menuWeight = entry.frontmatter.menuWeight || 999
} else {
if (!currentGroup[level].items) {
currentGroup[level].items = {}
currentGroup[level].items = []
}
currentGroup = currentGroup[level].items
}
Expand Down
3 changes: 3 additions & 0 deletions theme/src/templates/PageTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const pageQuery = graphql`
body
frontmatter {
title
menuWeight
date(formatString: "MMMM DD, YYYY")
description
banner
Expand All @@ -140,6 +141,7 @@ export const pageQuery = graphql`
frontmatter {
description
title
menuWeight
blurb
isHome
imageAlt
Expand All @@ -155,6 +157,7 @@ export const pageQuery = graphql`
nodes {
frontmatter {
title
menuWeight
}
fields {
slug
Expand Down

0 comments on commit 8162b64

Please sign in to comment.