1
0
Fork 0
freesewing/sites/shared/components/mdx/read-more.mjs

31 lines
994 B
JavaScript
Raw Normal View History

2021-12-24 18:17:02 +01:00
import get from 'lodash.get'
import orderBy from 'lodash.orderby'
import Link from 'next/link'
// Helper method to filter out the real children
const order = (obj) => orderBy(obj, ['__order', '__title'], ['asc', 'asc'])
const currentChildren = (current) =>
Object.values(order(current)).filter((entry) => typeof entry === 'object')
2021-12-24 18:17:02 +01:00
export const ReadMore = (props) => {
2022-05-30 15:36:39 +02:00
// Don't bother if we don't have the navigation tree in app
if (!props.app) return null
2021-12-24 18:17:02 +01:00
2023-03-24 17:43:38 +01:00
const root = get(props.app.navigation, props.app.state.slug.split('/'))
2021-12-24 18:17:02 +01:00
const list = []
for (const page of currentChildren(root)) {
list.push(
<li key={page.__slug} className={props.recurse ? 'ont-bold' : ''}>
<Link
href={`/${page.__slug}`}
className={props.recurse ? 'inline-block font-bold pt-3 pb-1' : ''}
>
{page.__title}
</Link>
{props.recurse && <ReadMore app={props.app} slug={page.__slug} />}
</li>
)
2021-12-24 18:17:02 +01:00
}
return <ul>{list}</ul>
}