2023-09-26 11:13:36 +02:00
|
|
|
import { nsMerge } from 'shared/utils.mjs'
|
2023-07-20 18:27:59 +02:00
|
|
|
import { pages as posts } from 'site/prebuild/blog.mjs'
|
2023-07-18 21:27:36 -06:00
|
|
|
import { getPostSlugPaths } from 'site/components/mdx/posts/utils.mjs'
|
2023-06-17 16:57:17 -05:00
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
2023-07-17 20:10:08 -05:00
|
|
|
import { useDynamicMdx } from 'shared/hooks/use-dynamic-mdx.mjs'
|
|
|
|
import { useCallback } from 'react'
|
2023-07-18 21:27:36 -06:00
|
|
|
import { PostArticle, ns as postNs } from 'site/components/mdx/posts/article.mjs'
|
|
|
|
import { PageWrapper, ns as pageNs } from 'shared/components/wrappers/page.mjs'
|
2023-06-17 16:57:17 -05:00
|
|
|
|
2023-09-26 11:13:36 +02:00
|
|
|
const namespaces = nsMerge(postNs, pageNs)
|
2023-06-17 16:57:17 -05:00
|
|
|
|
2023-07-18 21:27:36 -06:00
|
|
|
/*
|
|
|
|
* Each page MUST be wrapped in the PageWrapper component.
|
|
|
|
* You also MUST spread props.page into this wrapper component
|
|
|
|
* when path and locale come from static props (as here)
|
|
|
|
* or set them manually.
|
|
|
|
*/
|
2023-07-21 19:22:10 +02:00
|
|
|
const BlogPage = ({ locale, dir, page }) => {
|
2023-07-18 21:27:36 -06:00
|
|
|
// function to load the correct markdown
|
2023-07-21 19:22:10 +02:00
|
|
|
const loader = useCallback(() => import(`orgmarkdown/blog/${dir}/${locale}.md`), [dir, locale])
|
2023-07-18 21:27:36 -06:00
|
|
|
// load the markdown
|
2023-07-17 20:10:08 -05:00
|
|
|
const { frontmatter, MDX } = useDynamicMdx(loader)
|
2023-07-18 21:27:36 -06:00
|
|
|
|
2023-07-17 20:10:08 -05:00
|
|
|
return (
|
2023-09-26 10:12:55 +02:00
|
|
|
<PageWrapper {...page} title={frontmatter.title}>
|
2023-08-08 10:17:01 +02:00
|
|
|
<PostArticle {...{ frontmatter, MDX }} imgId={`blog-${dir}`} />
|
2023-07-18 21:27:36 -06:00
|
|
|
</PageWrapper>
|
2023-07-17 20:10:08 -05:00
|
|
|
)
|
2023-06-17 16:57:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getStaticProps() is used to fetch data at build-time.
|
|
|
|
*
|
2023-07-18 21:27:36 -06:00
|
|
|
* On this page, it passes the name of the bundle to be loaded on the client.
|
2023-06-17 16:57:17 -05:00
|
|
|
*
|
|
|
|
* This, in combination with getStaticPaths() below means this
|
|
|
|
* page will be used to render/generate all blog content.
|
|
|
|
*
|
|
|
|
* To learn more, see: https://nextjs.org/docs/basic-features/data-fetching
|
|
|
|
*/
|
|
|
|
export async function getStaticProps({ params, locale }) {
|
2023-07-21 19:22:10 +02:00
|
|
|
const { dir } = params
|
2023-06-17 16:57:17 -05:00
|
|
|
|
2023-07-21 19:22:10 +02:00
|
|
|
// if the dir isn't present in the prebuilt posts, return 404
|
|
|
|
if (!Object.keys(posts[locale]).includes(`blog/${dir}`)) return { notFound: true }
|
2023-07-19 21:11:59 -06:00
|
|
|
|
2023-06-17 16:57:17 -05:00
|
|
|
return {
|
|
|
|
props: {
|
2023-07-21 19:22:10 +02:00
|
|
|
dir,
|
2023-07-17 20:10:08 -05:00
|
|
|
locale,
|
2023-07-18 21:27:36 -06:00
|
|
|
...(await serverSideTranslations(locale, namespaces)),
|
2023-07-03 13:28:41 -05:00
|
|
|
page: {
|
|
|
|
locale,
|
2023-07-21 19:22:10 +02:00
|
|
|
path: ['blog', dir],
|
2023-07-03 13:28:41 -05:00
|
|
|
},
|
2023-06-17 16:57:17 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:11:59 -06:00
|
|
|
/*
|
|
|
|
* getStaticPaths() is used to specify for which routes (think URLs)
|
|
|
|
* this page should be used to generate the result.
|
|
|
|
*
|
|
|
|
* On this page, it is returning a truncated list of routes (think URLs) for all
|
|
|
|
* the mdx blog (markdown) content.
|
|
|
|
* That list comes from prebuild/blog-paths.mjs, which is built in the prebuild step
|
|
|
|
* and contains paths, titles, imageUrls, and intro for all blog posts.
|
|
|
|
*
|
|
|
|
* the fallback: 'blocking' property means that
|
|
|
|
* any pages that haven't been pre-generated
|
|
|
|
* will generate and cache the first time someone visits them
|
|
|
|
*
|
|
|
|
* To learn more, see: https://nextjs.org/docs/basic-features/data-fetching
|
|
|
|
*/
|
2023-07-18 21:27:36 -06:00
|
|
|
export const getStaticPaths = async () => {
|
|
|
|
return {
|
2023-07-20 18:27:59 +02:00
|
|
|
paths: getPostSlugPaths(posts),
|
2023-07-18 21:27:36 -06:00
|
|
|
fallback: 'blocking',
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 16:57:17 -05:00
|
|
|
|
2023-07-18 21:27:36 -06:00
|
|
|
export default BlogPage
|