Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import config from '../config' | ||
const forcedNavOrder = config.sidebar.forcedNavOrder | ||
|
||
// TODO(bmc) this entire file makes me sad - need to refactor this when I have more time | ||
// most of this was forked from the open source repo I cloned and quickly banged into shape | ||
export default function getSortedNavItems(allMdx) { | ||
const allFields = allMdx.edges.map(({ node }) => node.fields || {}) | ||
const navItems = allMdx.edges | ||
.map(({ node }) => node.fields) | ||
.reduce( | ||
(acc, fields) => { | ||
const { slug: cur, title } = fields | ||
|
||
const containingSlug = allFields.find(({ slug: otherSlug }) => { | ||
// is this slug contained within another one (e.g. is this a 'header' slug?) | ||
return cur !== '/' && cur !== otherSlug && otherSlug.indexOf(cur) === 0 | ||
}) | ||
|
||
const newItem = { | ||
title, | ||
hasChildren: Boolean(containingSlug), | ||
url: cur, | ||
slug: cur, | ||
filename: fields.filename.name, | ||
} | ||
if (forcedNavOrder.find(url => url === cur)) { | ||
return { ...acc, [cur]: [newItem] } | ||
} | ||
|
||
const prefix = cur.split('/')[1] | ||
|
||
if (prefix && forcedNavOrder.find(url => url === `/${prefix}`)) { | ||
return { ...acc, [`/${prefix}`]: [...acc[`/${prefix}`], { ...newItem }] } | ||
} else { | ||
return { ...acc, items: [...acc.items, newItem] } | ||
} | ||
}, | ||
{ items: [] } | ||
) | ||
|
||
const sortedTree = forcedNavOrder | ||
.reduce((acc, cur) => { | ||
const things = navItems[cur].sort((a, b) => { | ||
if (!a.hasChildren && !b.hasChildren) { | ||
return a.filename.localeCompare(b.filename) | ||
} | ||
}) | ||
return acc.concat(things) | ||
}, []) | ||
.concat(navItems.items) | ||
return sortedTree | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters