2022-10-07 13:55:39 +08:00
|
|
|
/* eslint-disable react/display-name */
|
2022-10-17 23:37:01 +08:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import { ComponentMap, getMDXComponent } from 'mdx-bundler/client';
|
|
|
|
import Image from './Image';
|
|
|
|
import CustomLink from './Link';
|
|
|
|
import TOCInline from './TOCInline';
|
|
|
|
import Pre from './Pre';
|
|
|
|
import { BlogNewsletterForm } from './NewsletterForm';
|
2022-10-07 13:55:39 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
const Wrapper: React.ComponentType<{ layout: string }> = ({
|
|
|
|
layout,
|
|
|
|
...rest
|
|
|
|
}) => {
|
|
|
|
const Layout = require(`../layouts/${layout}`).default;
|
|
|
|
return <Layout {...rest} />;
|
|
|
|
};
|
2022-10-07 13:55:39 +08:00
|
|
|
|
|
|
|
export const MDXComponents: ComponentMap = {
|
|
|
|
Image,
|
|
|
|
//@ts-ignore
|
|
|
|
TOCInline,
|
|
|
|
a: CustomLink,
|
|
|
|
pre: Pre,
|
|
|
|
wrapper: Wrapper,
|
|
|
|
//@ts-ignore
|
|
|
|
BlogNewsletterForm,
|
2022-10-17 23:37:01 +08:00
|
|
|
};
|
2022-10-07 13:55:39 +08:00
|
|
|
|
|
|
|
interface Props {
|
2022-10-17 23:37:01 +08:00
|
|
|
layout: string;
|
|
|
|
mdxSource: string;
|
|
|
|
[key: string]: unknown;
|
2022-10-07 13:55:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const MDXLayoutRenderer = ({ layout, mdxSource, ...rest }: Props) => {
|
2022-10-17 23:37:01 +08:00
|
|
|
const MDXLayout = useMemo(() => getMDXComponent(mdxSource), [mdxSource]);
|
2022-10-07 13:55:39 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
return <MDXLayout layout={layout} components={MDXComponents} {...rest} />;
|
|
|
|
};
|