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

35 lines
1 KiB
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
2023-04-16 16:19:49 +02:00
const order = (obj) => orderBy(obj, ['o', 't'], ['asc', 'asc'])
const currentChildren = (current) =>
Object.values(order(current)).filter((entry) => typeof entry === 'object')
2021-12-24 18:17:02 +01:00
2023-04-16 16:19:49 +02:00
export const ReadMore = ({ app, recurse = 0, slug = false }) => {
2022-05-30 15:36:39 +02:00
// Don't bother if we don't have the navigation tree in app
2023-04-16 16:19:49 +02:00
if (!app) return null
// Deal with recurse not being a number
if (recurse) {
if (typeof recurse === 'number') recurse--
else recurse = 1
}
2023-04-16 19:17:00 +02:00
const root =
slug && slug !== 'docs' ? get(app.state.nav, slug.split('/').slice(1)) : app.state.nav
console.log(root)
2021-12-24 18:17:02 +01:00
const list = []
for (const page of currentChildren(root)) {
list.push(
2023-04-16 16:19:49 +02:00
<li key={page.s}>
<Link href={`/${page.s}`}>{page.t}</Link>
{recurse > 0 ? <ReadMore app={app} slug={page.s} recurse={recurse} /> : null}
</li>
)
2021-12-24 18:17:02 +01:00
}
return <ul>{list}</ul>
}