1
0
Fork 0
freesewing/sites/shared/prebuild/navigation.mjs

63 lines
1.6 KiB
JavaScript
Raw Normal View History

import path from 'path'
import fs from 'fs'
import set from 'lodash.set'
// Some arbitrary future time
const future = new Date('10-12-2026').getTime()
/*
* Main method that does what needs doing
*/
export const prebuildNavigation = (mdxPages, strapiPosts, site) => {
2023-03-26 06:50:59 +02:00
/*
* Since this is written to disk and loaded as JSON, we minimize
* the data to load by using the following 1-character keys:
*
* t: title
* l: link title (shorter version of the title, optional
* o: order, optional
* s: slug without leading or trailing slash (/)
*/
const nav = {}
for (const lang in mdxPages) {
nav[lang] = {}
// Handle MDX content
for (const slug of Object.keys(mdxPages[lang]).sort()) {
const page = mdxPages[lang][slug]
const chunks = slug.split('/')
2023-03-26 06:50:59 +02:00
const val = {
t: page.t,
s: slug,
}
if (page.o) val._o = page.o
set(nav, [lang, ...chunks], val)
}
// Handle strapi content
2022-05-26 19:33:51 +02:00
for (const type in strapiPosts) {
set(nav, [lang, type], {
2023-03-26 06:50:59 +02:00
t: type,
l: type,
s: type,
o: type,
})
2022-05-26 19:33:51 +02:00
for (const [slug, page] of Object.entries(strapiPosts[type][lang])) {
const chunks = slug.split('/')
set(nav, [lang, type, ...chunks], {
2023-03-26 06:50:59 +02:00
t: page.title,
l: page.linktitle,
s: type + '/' + slug,
o: (future - new Date(page.date).getTime()) / 100000,
})
}
}
}
fs.writeFileSync(
2023-02-05 16:39:18 +01:00
path.resolve('..', site, 'prebuild', `navigation.mjs`),
`export const prebuildNavigation = ${JSON.stringify(nav, null, 2)}`
)
2022-01-20 09:07:38 +01:00
return true
}