1
0
Fork 0
freesewing/sites/dev/lib/load-navigation.mjs

39 lines
923 B
JavaScript
Raw Normal View History

2023-03-26 06:50:59 +02:00
import get from 'lodash.get'
import { prebuildNavigation as pbn } from 'site/prebuild/navigation.mjs'
import orderBy from 'lodash.orderby'
2023-03-26 06:56:43 +02:00
const createCrumbs = (path) =>
path.map((crumb, i) => {
const entry = get(pbn.en, path.slice(0, i + 1))
2023-03-26 06:50:59 +02:00
const val = { t: entry.t, s: entry.s }
if (entry.o) val.o = entry.o
return val
})
const createSections = () => {
const sections = {}
for (const slug of Object.keys(pbn.en)) {
const entry = pbn.en[slug]
const val = { t: entry.t, s: entry.s }
if (entry.o) val.o = entry.o
sections[slug] = val
}
return orderBy(sections, 'o')
}
2023-03-26 06:56:43 +02:00
export const loadNavigation = (path = []) => {
2023-03-26 06:50:59 +02:00
// Creat crumbs array
2023-03-26 06:56:43 +02:00
const crumbs = createCrumbs(path)
2023-03-26 06:50:59 +02:00
return {
2023-03-26 06:56:43 +02:00
path,
slug: path.join('/'),
2023-03-26 06:50:59 +02:00
crumbs,
sections: createSections(),
2023-03-26 06:56:43 +02:00
nav: path.length > 1 ? get(pbn.en, path[0]) : pbn.en[path[0]],
2023-03-26 06:50:59 +02:00
title: crumbs.slice(-1)[0].t,
}
}