feat: add MDXLayoutRenderer component

This commit is contained in:
Timothy Lin 2021-05-26 00:11:20 +08:00
parent 37527331d4
commit 8914349256
5 changed files with 23 additions and 11 deletions

View File

@ -1,11 +1,20 @@
import { MDXRemote } from 'next-mdx-remote'
import Image from 'next/image'
import CustomLink from './Link'
import Pre from './Pre'
const MDXComponents = {
export const MDXComponents = {
Image,
a: CustomLink,
pre: Pre,
}
export default MDXComponents
export const MDXLayoutRenderer = ({ layout, mdxSource, ...rest }) => {
const LayoutComponent = require(`../layouts/${layout}`).default
return (
<LayoutComponent {...rest}>
<MDXRemote {...mdxSource} components={MDXComponents} />
</LayoutComponent>
)
}

View File

@ -1,4 +1,4 @@
import MDXComponents from '@/components/MDXComponents'
import { MDXComponents } from '@/components/MDXComponents'
import fs from 'fs'
import matter from 'gray-matter'
import { serialize } from 'next-mdx-remote/serialize'

View File

@ -7,7 +7,7 @@ import Head from 'next/head'
import { SEO } from '@/components/SEO'
import LayoutWrapper from '@/components/LayoutWrapper'
import MDXComponents from '@/components/MDXComponents'
import { MDXComponents } from '@/components/MDXComponents'
export default function App({ Component, pageProps }) {
return (

View File

@ -1,5 +1,5 @@
import { MDXRemote } from 'next-mdx-remote'
import MDXComponents from '@/components/MDXComponents'
import { MDXComponents } from '@/components/MDXComponents'
import AuthorLayout from '@/layouts/AuthorLayout'
import { getFileBySlug } from '@/lib/mdx'

View File

@ -1,9 +1,7 @@
import fs from 'fs'
import { MDXRemote } from 'next-mdx-remote'
import MDXComponents from '@/components/MDXComponents'
import PageTitle from '@/components/PageTitle'
import PostLayout from '@/layouts/PostLayout'
import generateRss from '@/lib/generate-rss'
import { MDXLayoutRenderer } from '@/components/MDXComponents'
import { formatSlug, getAllFilesFrontMatter, getFileBySlug, getFiles } from '@/lib/mdx'
export async function getStaticPaths() {
@ -44,9 +42,14 @@ export default function Blog({ post, authorDetails, prev, next }) {
return (
<>
{frontMatter.draft !== true ? (
<PostLayout frontMatter={frontMatter} authorDetails={authorDetails} prev={prev} next={next}>
<MDXRemote {...mdxSource} components={MDXComponents} />
</PostLayout>
<MDXLayoutRenderer
layout={frontMatter.layout || 'PostLayout'}
mdxSource={mdxSource}
frontMatter={frontMatter}
authorDetails={authorDetails}
prev={prev}
next={next}
/>
) : (
<div className="mt-24 text-center">
<PageTitle>