|
| 1 | +import { GetStaticPaths, GetStaticProps, NextPage } from "next"; |
| 2 | +import Layout from "@layout/layout-01"; |
| 3 | +import SEO from "@components/seo/page-seo"; |
| 4 | +import matter from "gray-matter"; |
| 5 | +import fs from "fs"; |
| 6 | +import path from "path"; |
| 7 | +import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote"; |
| 8 | +import { serialize } from "next-mdx-remote/serialize"; |
| 9 | +import { getAllMediaPosts } from "../../lib/mdx-pages"; |
| 10 | + |
| 11 | +interface ProgramPageProps { |
| 12 | + frontmatter: { |
| 13 | + title: string; |
| 14 | + description: string; |
| 15 | + slug: string; |
| 16 | + }; |
| 17 | + mdxSource: MDXRemoteSerializeResult; |
| 18 | +} |
| 19 | + |
| 20 | +const ProgramPage: NextPage<ProgramPageProps> & { Layout: typeof Layout } = ({ |
| 21 | + frontmatter, |
| 22 | + mdxSource, |
| 23 | +}) => { |
| 24 | + return ( |
| 25 | + <> |
| 26 | + <SEO |
| 27 | + title={`${frontmatter.title} | Vets Who Code`} |
| 28 | + description={frontmatter.description} |
| 29 | + /> |
| 30 | + <div className="tw-container tw-py-10 md:tw-py-15"> |
| 31 | + <h1 className="tw-mb-6 tw-text-3xl tw-font-bold md:tw-text-4xl"> |
| 32 | + {frontmatter.title} |
| 33 | + </h1> |
| 34 | + <div className="prose md:prose-lg max-w-none"> |
| 35 | + <MDXRemote {...mdxSource} /> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + </> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +ProgramPage.Layout = Layout; |
| 43 | + |
| 44 | +export const getStaticPaths: GetStaticPaths = async () => { |
| 45 | + const programs = getAllMediaPosts<{ slug: string }>(["slug"], "programs"); |
| 46 | + const paths = programs.map((program) => ({ |
| 47 | + params: { slug: program.slug }, |
| 48 | + })); |
| 49 | + return { paths, fallback: false }; |
| 50 | +}; |
| 51 | + |
| 52 | +export const getStaticProps: GetStaticProps = async ({ params }) => { |
| 53 | + const slug = params?.slug as string; |
| 54 | + const filePath = path.join(process.cwd(), "src/data/programs", `${slug}.mdx`); |
| 55 | + if (!fs.existsSync(filePath)) { |
| 56 | + return { notFound: true }; |
| 57 | + } |
| 58 | + const fileContents = fs.readFileSync(filePath, "utf8"); |
| 59 | + const { data: frontmatter, content: mdxContent } = matter(fileContents); |
| 60 | + const mdxSource = await serialize(mdxContent, { scope: frontmatter }); |
| 61 | + return { |
| 62 | + props: { |
| 63 | + frontmatter, |
| 64 | + mdxSource, |
| 65 | + }, |
| 66 | + }; |
| 67 | +}; |
| 68 | + |
| 69 | +export default ProgramPage; |
0 commit comments