|
| 1 | +import fs from 'fs' |
| 2 | +import matter from 'gray-matter' |
| 3 | +import hydrate from 'next-mdx-remote/hydrate' |
| 4 | +import renderToString from 'next-mdx-remote/render-to-string' |
| 5 | +import dynamic from 'next/dynamic' |
| 6 | +import Head from 'next/head' |
| 7 | +import Link from 'next/link' |
| 8 | +import path from 'path' |
| 9 | +import CustomLink from '../../components/CustomLink' |
| 10 | +import Layout from '../../components/Layout' |
| 11 | +import { postFilePaths, POSTS_PATH } from '../../utils/mdxUtils' |
| 12 | + |
| 13 | +// Custom components/renderers to pass to MDX. |
| 14 | +// Since the MDX files aren't loaded by webpack, they have no knowledge of how |
| 15 | +// to handle import statements. Instead, you must include components in scope |
| 16 | +// here. |
| 17 | +const components = { |
| 18 | + a: CustomLink, |
| 19 | + // It also works with dynamically-imported components, which is especially |
| 20 | + // useful for conditionally loading components for certain routes. |
| 21 | + // See the notes in README.md for more details. |
| 22 | + TestComponent: dynamic(() => import('../../components/TestComponent')), |
| 23 | + Head, |
| 24 | +} |
| 25 | + |
| 26 | +export default function PostPage({ source, frontMatter }) { |
| 27 | + const content = hydrate(source, { components }) |
| 28 | + return ( |
| 29 | + <Layout> |
| 30 | + <header> |
| 31 | + <nav> |
| 32 | + <Link href="/"> |
| 33 | + <a>👈 Go back home</a> |
| 34 | + </Link> |
| 35 | + </nav> |
| 36 | + </header> |
| 37 | + <div className="post-header"> |
| 38 | + <h1>{frontMatter.title}</h1> |
| 39 | + {frontMatter.description && ( |
| 40 | + <p className="description">{frontMatter.description}</p> |
| 41 | + )} |
| 42 | + </div> |
| 43 | + <main>{content}</main> |
| 44 | + |
| 45 | + <style jsx>{` |
| 46 | + .post-header h1 { |
| 47 | + margin-bottom: 0; |
| 48 | + } |
| 49 | +
|
| 50 | + .post-header { |
| 51 | + margin-bottom: 2rem; |
| 52 | + } |
| 53 | + .description { |
| 54 | + opacity: 0.6; |
| 55 | + } |
| 56 | + `}</style> |
| 57 | + </Layout> |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +export const getStaticProps = async ({ params }) => { |
| 62 | + const postFilePath = path.join(POSTS_PATH, `${params.slug}.mdx`) |
| 63 | + const source = fs.readFileSync(postFilePath) |
| 64 | + |
| 65 | + const { content, data } = matter(source) |
| 66 | + |
| 67 | + const mdxSource = await renderToString(content, { |
| 68 | + components, |
| 69 | + // Optionally pass remark/rehype plugins |
| 70 | + mdxOptions: { |
| 71 | + remarkPlugins: [], |
| 72 | + rehypePlugins: [], |
| 73 | + }, |
| 74 | + scope: data, |
| 75 | + }) |
| 76 | + |
| 77 | + return { |
| 78 | + props: { |
| 79 | + source: mdxSource, |
| 80 | + frontMatter: data, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export const getStaticPaths = async () => { |
| 86 | + const paths = postFilePaths |
| 87 | + // Remove file extensions for page paths |
| 88 | + .map((path) => path.replace(/\.mdx?$/, '')) |
| 89 | + // Map the path into the static paths object required by Next.js |
| 90 | + .map((slug) => ({ params: { slug } })) |
| 91 | + |
| 92 | + return { |
| 93 | + paths, |
| 94 | + fallback: false, |
| 95 | + } |
| 96 | +} |
0 commit comments