tailwind-nextjs-blog/components/MDXComponents.tsx

40 lines
1002 B
TypeScript
Raw Permalink Normal View History

/* 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-17 23:37:01 +08:00
const Wrapper: React.ComponentType<{ layout: string }> = ({
layout,
...rest
}) => {
const Layout = require(`../layouts/${layout}`).default;
return <Layout {...rest} />;
};
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
};
interface Props {
2022-10-17 23:37:01 +08:00
layout: string;
mdxSource: string;
[key: string]: unknown;
}
export const MDXLayoutRenderer = ({ layout, mdxSource, ...rest }: Props) => {
2022-10-17 23:37:01 +08:00
const MDXLayout = useMemo(() => getMDXComponent(mdxSource), [mdxSource]);
2022-10-17 23:37:01 +08:00
return <MDXLayout layout={layout} components={MDXComponents} {...rest} />;
};