11import { Banner , PriorityBoardingBanner } from "@/components/Banner" ;
22import { CodeBlock } from "@/components/CodeBlock" ;
33import Layout from "@/mdxLayouts/index" ;
4- import { FrontMatter } from "@/types" ;
5- import { postFilePaths , POSTS_PATH } from "@/utils/mdxUtils" ;
6- import fs from "fs" ;
7- import matter from "gray-matter" ;
8- import { GetStaticProps } from "next" ;
9- import { MDXRemote , MDXRemoteProps } from "next-mdx-remote" ;
10- import { serialize } from "next-mdx-remote/serialize" ;
4+ import { allPages , Page } from "contentlayer/generated" ;
5+ import { GetStaticPaths , GetStaticProps } from "next" ;
6+ import { useMDXComponent } from "next-contentlayer/hooks" ;
117import { default as NextImage , ImageProps } from "next/legacy/image" ;
128import Link from "next/link" ;
13- import path from "path" ;
14- import remarkAutolinkHeadings from "remark-autolink-headings" ;
15- import remarkGfm from "remark-gfm" ;
16- import remarkSlug from "remark-slug" ;
179
1810const Image = ( props : ImageProps ) => (
1911 < a
@@ -34,46 +26,38 @@ const components = {
3426 PriorityBoardingBanner,
3527} ;
3628
37- export default function PostPage ( {
38- source,
39- frontMatter,
40- } : {
41- source : Omit < MDXRemoteProps , "components" > ;
42- frontMatter : FrontMatter ;
43- } ) {
29+ export default function PostPage ( { page } : { page : Page } ) {
30+ const MDXContent = useMDXComponent ( page . body . code ) ;
31+
4432 return (
45- < Layout frontMatter = { frontMatter } >
46- < MDXRemote { ...source } components = { components } />
33+ < Layout
34+ frontMatter = { {
35+ title : page . title ,
36+ } }
37+ >
38+ < MDXContent components = { components } />
4739 </ Layout >
4840 ) ;
4941}
5042
5143export const getStaticProps : GetStaticProps = async ( { params } ) => {
52- const postFilePath = path . join (
53- POSTS_PATH ,
54- `${ ( params ?. slug as string [ ] ) . join ( "/" ) } .md` ,
44+ const page = allPages . find (
45+ page =>
46+ page . _raw . flattenedPath ===
47+ ( params ?. slug as string [ ] | undefined ) ?. join ( "/" ) ,
5548 ) ;
56- const source = fs . readFileSync ( postFilePath ) ;
57-
58- const { content, data } = matter ( source ) ;
5949
60- const mdxSource = await serialize ( content , {
61- // Optionally pass remark/rehype plugins
62- mdxOptions : {
63- remarkPlugins : [ remarkAutolinkHeadings , remarkSlug , remarkGfm ] ,
64- rehypePlugins : [ ] ,
65- } ,
66- scope : data ,
67- } ) ;
6850 return {
6951 props : {
70- source : mdxSource ,
71- frontMatter : data ,
52+ page,
7253 } ,
7354 } ;
7455} ;
7556
76- export const getStaticPaths = async ( ) => {
77- const paths = postFilePaths . map ( path => path . replace ( / \. m d x ? $ / , "" ) ) ;
78- return { paths, fallback : false } ;
57+ export const getStaticPaths : GetStaticPaths = async ( ) => {
58+ const paths = allPages . map ( page => page . url ) ;
59+ return {
60+ paths,
61+ fallback : false ,
62+ } ;
7963} ;
0 commit comments