oops, include prebuilder
This commit is contained in:
parent
9e1431d554
commit
6653e6f5b7
2 changed files with 75 additions and 0 deletions
23
sites/shared/hooks/use-dynamic-mdx.mjs
Normal file
23
sites/shared/hooks/use-dynamic-mdx.mjs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { components } from 'shared/components/mdx/index.mjs'
|
||||||
|
import { Loading } from 'shared/components/spinner.mjs'
|
||||||
|
import { useState, useEffect } from 'react'
|
||||||
|
|
||||||
|
export const useDynamicMdx = (loader, site = 'org') => {
|
||||||
|
// State
|
||||||
|
const [frontmatter, setFrontmatter] = useState({ title: `freeSewing.${site}` })
|
||||||
|
const [MDX, setMDX] = useState(<Loading />)
|
||||||
|
|
||||||
|
/* Load MDX dynamically */
|
||||||
|
useEffect(() => {
|
||||||
|
const loadMDX = async () => {
|
||||||
|
loader().then((mod) => {
|
||||||
|
setFrontmatter(mod.frontmatter)
|
||||||
|
const Component = mod.default
|
||||||
|
setMDX(<Component components={components(site)} />)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (loader) loadMDX()
|
||||||
|
}, [loader, site])
|
||||||
|
|
||||||
|
return { MDX, frontmatter }
|
||||||
|
}
|
52
sites/shared/prebuild/posts.mjs
Normal file
52
sites/shared/prebuild/posts.mjs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import { loadMdxForPrebuild, header } from './docs.mjs'
|
||||||
|
import fs from 'fs/promises'
|
||||||
|
import path from 'path'
|
||||||
|
import rdir from 'recursive-readdir'
|
||||||
|
import orderBy from 'lodash.orderby'
|
||||||
|
|
||||||
|
const types = ['blog', 'showcase', 'newsletter']
|
||||||
|
|
||||||
|
export const prebuildPosts = async (site) => {
|
||||||
|
if (site !== 'org') return {}
|
||||||
|
|
||||||
|
// Languages
|
||||||
|
const locales = ['en', 'fr', 'es', 'nl', 'de', 'uk']
|
||||||
|
const results = await Promise.all(
|
||||||
|
types.map((t) =>
|
||||||
|
loadMdxForPrebuild(site, path.resolve('..', '..', 'markdown', site, t), locales)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
const writeOps = []
|
||||||
|
const pages = {}
|
||||||
|
for (var i = 0; i < types.length; i++) {
|
||||||
|
const writePath = path.resolve('..', site, 'prebuild', types[i])
|
||||||
|
|
||||||
|
const sorted = {}
|
||||||
|
const resultPages = results[i]
|
||||||
|
|
||||||
|
pages[types[i]] = resultPages
|
||||||
|
for (const lang in resultPages) {
|
||||||
|
sorted[lang] = Object.keys(resultPages[lang]).sort(
|
||||||
|
(a, b) => resultPages[lang][a].o - resultPages[lang][b].o
|
||||||
|
)
|
||||||
|
// get rid of the index page
|
||||||
|
sorted[lang].shift()
|
||||||
|
}
|
||||||
|
|
||||||
|
writeOps.push(
|
||||||
|
fs.writeFile(
|
||||||
|
path.resolve('..', site, 'prebuild', `${types[i]}-paths.mjs`),
|
||||||
|
`${header}export const order = ${JSON.stringify(
|
||||||
|
sorted,
|
||||||
|
2,
|
||||||
|
null
|
||||||
|
)}\nexport const postInfo = ${JSON.stringify(resultPages, 2, null)}`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(writeOps)
|
||||||
|
|
||||||
|
return pages
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue